Adrian Geanta
Adrian Geanta

Reputation: 115

ListView setOnItemClickListener with JSONArray and JSONObject

I have a problem with a setOnItemClickListener from a listView.

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ItemClick();
            }
        });

And ItemClick:

private int ItemClick (){
    try {
        JSONArray ja=new JSONArray(data);
        JSONObject jo=null;

        for(int i=0;i<ja.length();i++)
        {
            jo=ja.getJSONObject(i);
            String nr_telefon=jo.getString("nr_telefon");

            Toast.makeText(MainActivity.this,nr_telefon,Toast.LENGTH_SHORT).show();

        }

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

    return 0;

}

The problem it's that return doesn't work (I think) When I click on a item from listView it's like I clicked on all the items. On click will show me all the toast with the nr_telefon from first item to last one. What's the problem and how to fix it?

And one item from list view it's created like this:

 String  nr_masina=jo.getString("nr_masina");
   String proprietar=jo.getString("proprietar");
   String nr_telefon=jo.getString("nr_telefon");
   names.add(nr_masina + "\n" + proprietar + "\n" + nr_telefon);

Upvotes: 0

Views: 63

Answers (2)

Wojciech Januszek
Wojciech Januszek

Reputation: 651

Currently, the ItemClick method displays the Toasts for every item in the JSONArray.

Try to add an argument int position to the method and pass the position with it:

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

Note that the for loop is gone. It used to display a Toast for each item in the JSONArray ja. Now the logic is invoked only once, for the item with position index:

    private int ItemClick(int position){
        try {
            JSONArray ja = new JSONArray(data);

            jo = ja.getJSONObject(position);
            String nr_telefon = jo.getString("nr_telefon");        
            Toast.makeText(MainActivity.this,nr_telefon,Toast.LENGTH_SHORT).show();
        }

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

        return 0;
    }

Upvotes: 2

Mayank Bhatnagar
Mayank Bhatnagar

Reputation: 1336

Because you are not passing position on the clicked item in the listview. Use this code

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            ItemClick(position);
        }
    });

And the change method to this

private int ItemClick (int pos){
try {
    JSONArray ja=new JSONArray(data);
    JSONObject jo=null;


        jo=ja.getJSONObject(pos);
        String nr_telefon=jo.getString("nr_telefon");

        Toast.makeText(MainActivity.this,nr_telefon,Toast.LENGTH_SHORT).show();



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

return 0;

}

Upvotes: 1

Related Questions