点击行项目将listview的json数据传递给下一个活动

时间:2016-04-18 11:05:04

标签: android json listview

我需要将listview的json数据传递给点击行项目的下一个活动。

这是我的第一个活动

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();
            }




        }
    }


}

2 个答案:

答案 0 :(得分:0)

传递数据:

oriAddress

接收第二项活动中的数据:

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

答案 1 :(得分:0)

将您的数据转换为Json格式,然后将其发送到第二个活动:

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);

从第二个活动中获取JsonObject,如下所示:

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

JSONObject jsonObj = new JSONObject(data);