Tekosher Dilshad
Tekosher Dilshad

Reputation: 3

(onItemClick) show Json data from listview - Android

I had finish parsing json and get it to ListView , but i have a problem When click on the item from listview only showing the last parsed Json data , exatctly i want when click on the item showing correct data by position :

for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject c = jsonArray.getJSONObject(i);
                            final String fname = c.getString("fname");
                            final String lname = c.getString("lname");
                            String username = c.getString("username");
                            //String user_id = c.getString("user_id");
                            //String gender = c.getString("gender");

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

                            contact.put("fname", fname);
                            contact.put("lname", lname);
                            contact.put("username", username);

                            users.add(contact);

                            ListAdapter adapter = new SimpleAdapter(FindPeopleActivity.this, users,
                                    R.layout.list_item, new String[]{"fname", "lname", "username"},
                                    new int[]{R.id.fname, R.id.lname, R.id.username});
                            lv.setAdapter(adapter);

                            lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
                                @Override
                                public void onItemClick(AdapterView<?> parent , View view , int position , long id){
                                    Intent i = new Intent(FindPeopleActivity.this,UserProfileActivity.class);
                                    Toast.makeText(getApplicationContext(),fname,Toast.LENGTH_SHORT).show();
                                }
                            });
                        }

Upvotes: 0

Views: 372

Answers (1)

king_abu1918
king_abu1918

Reputation: 284

Set the setOnItemClickListener after the for loop.

   lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {


              //i = row/item clicked in listview

             //Get the variable from the array you passed to the adapter
             newItem.variable1 = jsonArrayData[i].variable1;
             newItem.variable2 = jsonArrayData[i].variable2;

            //Pass the selected json info...
             yourMethod(newItem);




            Toast.makeText(getApplicationContext(),fname,Toast.LENGTH_SHORT)
            .show();

        }
    });

Put some test values in your toast, like i for example to see what value is being passed.

Upvotes: 1

Related Questions