将单击的Listview的特定值传递给另一个活动

时间:2015-12-22 09:28:32

标签: android listview

我有两个一级是ReadData,另一个是Read2,我希望将ReadData类列表视图的点击条的ITEM_ID 值传递给另一个活动Read2.class < / strong>,我已将我的代码放在下面。我应该在代码中做什么才能成功完成任务。

 public class ReadData extends ListActivity {

            globalClass gc=new globalClass();
             String id=gc.getid();
            String url = "http://xyz.php?id="+ id;
            ArrayList<HashMap<String, String>> Item_List;
            ProgressDialog PD;
            ListAdapter adapter;

            // JSON Node names
            public static final String ITEM_ID = "id";
            public static final String ITEM_NAME = "item";

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

                Item_List = new ArrayList<HashMap<String, String>>();

                PD = new ProgressDialog(this);
                PD.setMessage("Loading.....");
                PD.setCancelable(false);

                getListView().setOnItemClickListener(new ListitemClickListener());

                ReadDataFromDB();
            }

            private void ReadDataFromDB() {
                PD.show();
                JsonObjectRequest jreq = new JsonObjectRequest(Method.GET, url, null,
                        new Response.Listener<JSONObject>() {

                            @Override
                            public void onResponse(JSONObject response) {
                                try {
                                    int success = response.getInt("success");

                                    if (success == 1) {
                                        JSONArray ja = response.getJSONArray("orders");

                                        for (int i = 0; i < ja.length(); i++) {

                                            JSONObject jobj = ja.getJSONObject(i);

                                            HashMap<String, String> item = new HashMap<String, String>();


                                            /*------------------particular order id ----------------*/

                                            item.put(ITEM_ID, jobj.getString(ITEM_ID));

                                            /*----------------------------------*/



                                            item.put(ITEM_NAME,jobj.getString(ITEM_NAME));

                                            Item_List.add(item);

                                        } // for loop ends

                                        String[] from = { ITEM_ID, ITEM_NAME };
                                        int[] to = { R.id.item_id, R.id.item_name };

                                        adapter = new SimpleAdapter(
                                                getApplicationContext(), Item_List,
                                                R.layout.list_items, from, to);

                                        setListAdapter(adapter);

                                        PD.dismiss();

                                    } // if ends

                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }
                        }, new Response.ErrorListener() {

                            @Override
                            public void onErrorResponse(VolleyError error) {
                                PD.dismiss();
                            }
                        });

                // Adding request to request queue
                MyApplication.getInstance().addToReqQueue(jreq);

            }

            class ListitemClickListener implements ListView.OnItemClickListener {

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position,
                        long id) {

                    Intent modify_intent = new Intent(ReadData.this,Read2.class);

                    modify_intent.putExtra("item", Item_List.get(position));

                    startActivity(modify_intent);

                }

            }
        }

1 个答案:

答案 0 :(得分:1)

试试这个:

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

Read2.java

{{1}}