Search code examples
androidfilterandroid-listviewonitemclicklistener

Incorrect ids in OnItemClickListener after filtering the list


I`m using ListView to show some data from mysql table, and its populated with SimpleAdapter. I added onItemClickListener, to be able to open new activity when user press some item on list. This is working very well, but when you choose an option to search the list, textfilter is filtering entries ok but onItemClick is not sending the correct "id" to the new activity. I searched for solution and everyone solves the problem with "adapter.notifyDataSetChanged" but its not working for me. Here is the code, maybe someone can help.

list.setTextFilterEnabled(true);
                myFilter.addTextChangedListener(new TextWatcher() {
                    public void afterTextChanged(Editable s) {
                    }
                    public void beforeTextChanged(CharSequence s, int start,int count, int after) {
                    }
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                        mSchedule.getFilter().filter(s.toString());

                    }

                });



                list.setOnItemClickListener(new OnItemClickListener() {

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

                            String ajdi = jArray.getJSONObject(position).getString("id").toString();
                            Intent i = new Intent(Predlozi.this, PesmaPrikaz.class);
                            Bundle bandl = new Bundle();
                            bandl.putString("id", ajdi);
                            i.putExtras(bandl);
                            startActivity(i);

                        } catch (JSONException e) {
                            ;
                        }
                    }

                });

Solution

  • but when you choose an option to search the list, textfilter is filtering entries ok but onItemClick is not sending the correct "id" to the new activity.

    That is a normal behavior as you most likely get the id directly from the list/json structure that is used to populate the adapter. If you then filter the list and the filtering removes items than the position will be incorrect as you'll have fewer elements in the list.

    Instead you should get the row data from the adapter and get the id from there, assuming you do place it there(which you should definitely do):

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
          HashMap<Something, Something> rowData = (HashMap<Something, Something>)((SimpleAdapter)parent.getAdapter()).getItem(position); // I don't know what you pass to the adapter so edit this
          // get the id from the `HashMap` above
          // if you don't store the id in there, then edit your code to do this:
          String id = rowData.get("the_key_for_the_Id");
          // send the Intent 
    }