Click here to Skip to main content
15,920,053 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have populated listView with json data and have set an onClick listener to it. when i click on an item in the listView it should open up a new activity and show the item that was clicked.
but when i click on any item in the listView it always shows the first item in the list.

the code that i used is as follows

Adapter.java

Java
private Activity activity;
    private static ArrayList prefrenceid,partnerid;
    private static LayoutInflater inflater = null;
    public Adapter(Activity a, ArrayList pref, ArrayList part) {
        activity = a;
        this.prefrenceid = pref;
        this.partnerid=part;


        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {
        return prefrenceid.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if (convertView == null)
            vi = inflater.inflate(R.layout.adapter, null);

        TextView preferences = (TextView) vi.findViewById(R.id.textView43);
        String song = prefrenceid.get(position).toString();
        preferences.setText(song);


        TextView partner = (TextView) vi.findViewById(R.id.textView44);
        String song2 = partnerid.get(position).toString();
        partner.setText(song2);

        return vi;

    }/pre>



MainActivity.java

<pre lang="java">try {
                    Toast.makeText(getApplicationContext(), response, Toast.LENGTH_SHORT).show();
                    JSONObject obj3 = new JSONObject(response);
                    JSONArray jsonArray3 = obj3.getJSONArray("MyREST method");
                    int length3 = jsonArray3.length();
                    if (length3 != 0) {
                        for (int i = 0; i < length3; i++) {
                            JSONObject user3 = jsonArray3.getJSONObject(i);
                            offer_array.add(user3.getString("CITY").toString());
                            description_array.add(user3.getString("ZIP_CODE").toString());
                        }
                        adapter = new Adapter(MainActivity.this, offer_array, description_array);
                        listView.setAdapter(adapter);
                        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                String itemValue = ((TextView) findViewById(R.id.textView43)).getText().toString();
                                //String  itemValue    = (String) listView.getItemAtPosition(position);
                                Intent result = new Intent(getApplicationContext(), ResultClass.class);
                                result.putExtra("itemValue", itemValue);
                                startActivity(result);
                            }
                        });
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
Posted
Updated 16-Nov-15 21:59pm
v2
Comments
Richard MacCutchan 17-Nov-15 4:52am    
You need to pass the correct item to the new intent.
Rahul Ramakrishnan 17-Nov-15 4:56am    
can you please suggest how to do it
Richard MacCutchan 17-Nov-15 5:10am    
Use the position value in the onClick handler to find the correct item, and pass the item reference to the new intent.

1 solution

You should use the position in listView onItemClick

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

               Cursor cursor = (Cursor) listView.getItemAtPosition(position);
               iD = cursor.getLong(cursor.getColumnIndexOrThrow("_id"));
               //Toast.makeText(getActivity(), iD + "", Toast.LENGTH_LONG).show();
               Intent result = new Intent(getApplicationContext(), ResultClass.class);
              // intent.putExtra("ID", iD);
               startActivity(result);

           }
       });
 
Share this answer
 
v6

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900