简体   繁体   中英

Pass json data of listview to next activity on click of row item

I need to pass the json data of listview to the next activity on the click of row item.

here is my first Activity

public class DrSearch extends Activity implements OnClickListener,
    OnItemClickListener {
    ListView listView;

    ArrayList<RowItem> rowDoctors;

    DrSearchListViewAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dr_search);

        iv.setOnClickListener(this);
        rowDoctors = new ArrayList<RowItem>();
        new DoctorList().execute();

        listView = (ListView) findViewById(R.id.list1);
        adapter = new DrSearchListViewAdapter(getApplicationContext(), R.layout.single_list_dr_search, rowDoctors);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        int actualDataPosition=adapter.getActualPosition(position, rowDoctors);


        if(actualDataPosition>=0)
        {
            String data=rowDoctors.get(actualDataPosition).getId()+","+rowDoctors.get(actualDataPosition).getImageId()+","+rowDoctors.get(actualDataPosition).getTitle();

            Intent newActivity = new Intent(DrSearch.this, DocProfileForUser.class);   

            newActivity.putExtra("Position", actualDataPosition);
            newActivity.putExtra("data", data);
             startActivity(newActivity);
        }

    }

    }
    class DoctorList extends AsyncTask<String, Void, Void> {

        private final HttpClient Client = new DefaultHttpClient();
        private String Content;
        private String Error = null;
        private ProgressDialog Dialog = new ProgressDialog(DrSearch.this);
        String data = "";

        protected void onPreExecute() {
            // TODO Auto-generated method stub
            Dialog.setMessage("Please wait..");
            Dialog.show();
        }

        protected Void doInBackground(String... urls) {




            // To retrieve value from shared preference in another activity
            SharedPreferences sp = getApplicationContext()

                    .getSharedPreferences("sharedPrefName", 0); 

            String user_id = sp.getString("key_name", "defaultvalue"); 

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("access_token",
                    "testermanishrahul234142test"));
            nameValuePairs.add(new BasicNameValuePair("user_id", user_id));

            Content = new ServiceHandler().makeServiceCall(
                    AppConstant.GET_DOCTOR_LIST, 2, nameValuePairs);
            return null;

        }

        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            Dialog.dismiss();



            String OutputData = "";
            JSONObject jsonResponse;

            try {


                jsonResponse = new JSONObject(Content);


                JSONObject jsonMainNode = jsonResponse
                        .getJSONObject("document");

                JSONObject response = jsonMainNode.getJSONObject("response");

                JSONArray jarray = response.getJSONArray("data");

                for (int i = 0; i < jarray.length(); i++) {
                    JSONObject object = jarray.getJSONObject(i);

                    RowItem list = new RowItem();

                    list.setAvailability(object.getString("morning_appointment_start"));
                    list.setAvailability1(object.getString("morning_appointment_end"));
                    list.setAvailability2(object.getString("eve_appointment_start"));
                    list.setAvailability3(object.getString("eve_appointment_end"));
                    list.setTitle(object.getString("firstName"));
                    list.setSpeciality(object.getString("speciality"));
                    list.setImageId(object.getString("image"));
                    list.setId(object.getString("id"));

                    rowDoctors.add(list);


                }


                String status = response.getString("status");
                if (status != null && status.equalsIgnoreCase("1")) {
                }
                String message = response.getString("message");

                Toast.makeText(getApplicationContext(),
                        "" + status + ":" + message, Toast.LENGTH_SHORT).show();





            } catch (JSONException e) {

                e.printStackTrace();
            }




        }
    }


}

Passing Data:

 Intent newActivity = new Intent(DrSearch.this, DocProfileForUser.class);   
        newActivity.putExtra("data", data);
        startActivity(newActivity);

Receiving Data in Second Activity:

    String dataString;
    Bundle extras = getIntent().getExtras();
      if(extras != null) {
        dataString= extras.getString("data");
      }

TextView tvData = (TextView) findViewById(R.id.tv_data);
tvData.setText(dataString);

Convert your data to Json format and then send it to second activity like this:

String data = "{'id : '"+rowDoctors.get(actualDataPosition).getId()+"', 'image_id':'"+rowDoctors.get(actualDataPosition).getImageId()+"', 'title':'"+rowDoctors.get(actualDataPosition).getTitle()+"'}";

Intent newActivity = new Intent(DrSearch.this, DocProfileForUser.class);
newActivity.putExtra("data", data);
startActivity(newActivity);

Get the JsonObject back from second activity like this:

String data;
Bundle extras = getIntent().getExtras();
if(extras != null) {
data= extras.getString("data");
}

JSONObject jsonObj = new JSONObject(data);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM