2016-12-28 2 views
0
public class MainActivity extends **strong text** 

AppCompatActivity { 

private String TAG = MainActivity.class.getSimpleName(); 

private ListView lv; 

ArrayList<HashMap<String, String>> companyList; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    companyList = new ArrayList<>(); 
    lv = (ListView) findViewById(R.id.list_row_xml); 

    new GetCompany().execute(); 
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { 
      Intent i = new Intent(MainActivity.this, SingleView.class); 

      i.putExtra("venue", venue); 
      startActivity(i); 
     } 
    }); 
} 


private class GetCompany extends AsyncTask<Void, Void, JSONObject> { 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     Toast.makeText(MainActivity.this,"Json Data is downloading",Toast.LENGTH_LONG).show(); 

    } 
    @RequiresApi(api = Build.VERSION_CODES.KITKAT) 
    @Override 
    protected JSONObject doInBackground(Void... arg0) { 
     HttpHandler sh = new HttpHandler(); 
     // Making a request to url and getting response 
     String url = "http://10.0.2.2:6060/api/v1/company/getInfo?limit=10&page=1"; 
     JSONArray company = sh.makeServiceCall(url); 

     Log.e(TAG, "Response from url: " + company); 

     if (company != null) { 
      try { 
       // JSONObject jsonObj = new JSONObject(jsonStr); 

       // Getting JSON Array node 
       //JSONArray company = new JSONArray(jsonStr); 
       // System.out.println("Reached"); 
       // looping through All Contacts 
       for (int i = 0; i < company.length(); i++) { 
        //System.out.println("Reached1"); 
        JSONObject c = company.getJSONObject(i); 
        String id = c.getString("id"); 
        String name = c.getString("companyName"); 

        // Walking Details in Json object 
        JSONObject walkingDetails=c.getJSONObject("walkingdetails"); 
        String date = walkingDetails.getString("walkingdate"); 
        String venue = walkingDetails.getString("venu"); 

        // tmp hash map for single contact 
        HashMap<String, String> companyy = new HashMap<>(); 

        // adding each child node to HashMap key => value 
        companyy.put("id",id); 
        companyy.put("date", date); 
        companyy.put("companyname", name); 
        companyy.put("venue", venue); 


        // adding contact to contact list 
        companyList.add(companyy); 
       } 
      } catch (final JSONException e) { 
       Log.e(TAG, "Json parsing error: " + e.getMessage()); 
       runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         Toast.makeText(getApplicationContext(), "Json parsing error: " + e.getMessage(), Toast.LENGTH_LONG).show(); 
        } 
       }); 

      } 
     } else { 
      Log.e(TAG, "Couldn't get json from server."); 
      runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 
        Toast.makeText(getApplicationContext(), "Couldn't get json from server. Check LogCat for possible errors!", Toast.LENGTH_LONG).show(); 
       } 
      }); 
     } 

     return null; 
    } 
/* @Override 
    protected void onPostExecute(Void result) { 
     super.onPostExecute(result); 
     ListAdapter adapter = new SimpleAdapter(MainActivity.this, companyList, 
       R.layout.list_row, new String[]{"date","companyname","venue"}, new int[]{ R.id.date, R.id.companyname, venue}); 
     lv.setAdapter(adapter);*/ 
    } 
} 

초 acivity JSON 데이터를 전달하는 방법 : 당신은 된 JSONObject를 확장하고 Serializable 인터페이스를 구현하여 자신의 클래스를 생성함으로써 달성 할 수다른 활동에

public class SingleView extends AppCompatActivity { 


EditText txtVenue; 

// String[] Venue; 
int position; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_single_view); 

Intent i = getIntent(); 
    position = i.getExtras().getInt("positi`enter code here`on"); 
    txtVenue = (EditText) findViewById(R.id.Venue); 
} 
} 

답변

1

직접 JSONObject을 다른 활동으로 전달할 수 없습니다. 하지만 json을 문자열로 변환하여 전달할 수 있습니다. 그런 다음 SecondActivity에서 json으로 다시 변환 할 수 있습니다. FirstActivity에서

시작 SecondActivity 코드 :

Intent intent = new Intent(FirstActivity.this, SecondActivity.class); 
intent.putExtra("data", json.toString()); 
startActivity(intent); 

그런 다음 SecondActivity에서이 데이터를 얻을 :

String data = getIntent().getStringExtra("data"); 
try { 
    JSONObject json = new JSONObject(data); 
} catch (JSONException e) { 
    e.printStackTrace(); 
} 

행운을 빕니다.

0

합니다. 당신이 의도를 통과 할 수 있도록

0

HashMaps는 순서를 유지하지 않습니다.

모델 클래스를 사용하면 데이터를 쉽게 검색 할 수 있습니다.

public class CompanyData { 

    public String id; 
    public String date; 
    public String name; 
    public String venue; 
} 

다음 ArrayList를 변경,

ArrayList<CompanyData> companyList; 
다음

값을 저장,

CompanyData companyy = new CompanyData(); 

companyy.id = id; 
companyy.date = date; 
companyy.name = name; 
companyy.venue = venue; 

companyList.add(companyy); 

다음 onitemclicklistener 구현,

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { 

     CompanyData data = companyList.get(position); 

     Intent i = new Intent(MainActivity.this, SingleView.class); 

     i.putExtra("venue", data.venue); 
     startActivity(i); 
    } 
}); 

*** 위치가 단순히 위치 값을 보내 얻으려면 insideItemClickL istener

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { 

     CompanyData data = companyList.get(position); 

     Intent i = new Intent(MainActivity.this, SingleView.class); 

     i.putExtra("position", position); 
     startActivity(i); 
    } 
}); 

는 또한 당신은 두 가지 옵션이 있습니다

CompanyData data = companyList.get(position); 
i.putExtra("data", data); //this will pass all data of clicked row 
0

하여 특정 위치의 모든 데이터를 보낼 수 있습니다 : 문자열 1. 변환 JSON을 문자열로 전달합니다. 2. json에 대한 모델 클래스를 만들고이를 parcelable로 만듭니다. 이 객체를 다음 활동으로 전달하십시오.

관련 문제