简体   繁体   中英

Pass the particular value of clicked Listview to another activity

I have two class one is ReadData and another is Read2 and i want to pass the value of ITEM_ID of the clicked strip of the listview of ReadData class to another activity Read2.class ,i have put my code below.What should i do in the code to do my task successfully.

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

                }

            }
        }

Try this :

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

    Intent modify_intent = new Intent(ReadData.this,Read2.class);
    HashMap<String, String> selected_item = Item_List.get(position);
    modify_intent.putExtra("item", selected_item.get(ITEM_ID));
    startActivity(modify_intent);

}

Read2.java

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

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