Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView custom filter gives wrong item selected when filtered [Android]

I am new to Android programming and I have a ListView when filtered always return me the first item in the list, so how do I fix this?

For instance, my list contains A.A, A.B, A.C, B.C, B.D. When I want to search the list for things starting with B, I will get B.C, B.D but when I click B.C, it returns me A.A and B.D, it returns me A.B

public class PatientList extends Activity{

PatientDB patientDB;
Context myContext;
ListView lv_patients;    
EditText et_patients;
ArrayAdapter<String> adapter;   
ArrayList<HashMap<String, String>> patientList;
static String value;
String[] patientarray = new String[]{"initials"};
ArrayList<String> patientArrayList = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_patient_list);

    myContext = this;
    patientDB = new PatientDB(myContext);
    patientDB.open();

    Cursor patientCursor;

    patientCursor = patientDB.retrieveAllEntriesCursor();

    if(patientCursor!=null && patientCursor.getCount()>0)
    {
        String patientInitials;

        patientCursor.moveToFirst();
        do 
        {
            patientCursor.getString(patientDB.COLUMN_KEY_ID); // + " " + 
            patientInitials = patientCursor.getString(patientDB.COLUMN_INITIALS_ID);
            Log.i("FromCursor", patientInitials);
            patientArrayList.add(patientInitials);
        } while (patientCursor.moveToNext());
    }

    lv_patients = (ListView) findViewById(R.id.lv_patients);
    et_patients = (EditText) findViewById(R.id.et_patients);

    lv_patients.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // TODO Auto-generated method stub
            String a = patientArrayList.get(position);
            Toast.makeText(PatientList.this, a + " selected", Toast.LENGTH_SHORT).show();               
            Intent returnIntent = new Intent(PatientList.this, PatientInfo.class);
            returnIntent.putExtra("value", a);
            setResult(RESULT_OK, returnIntent);  
            finish();
        }
    });

    //Adding items to listview
    adapter = new ArrayAdapter<String>(this, R.layout.list_patients, R.id.patient_name, patientArrayList);
    lv_patients.setAdapter(adapter);

    //Enabling search filter
    et_patients.addTextChangedListener(new TextWatcher() {

        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text
            PatientList.this.adapter.getFilter().filter(cs);
        }

        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            // TODO Auto-generated method stub

        }

        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub

        }
    });
}

}

like image 237
Spencer Avatar asked Nov 12 '13 10:11

Spencer


1 Answers

Because you filter your adapter to have B.C in position 0 and B.D in position 1. But in your implementation of your onClickListener, you use the listview's position to get directly from the source ArrayList.

String a = patientArrayList.get(position);

Instead to get it from the filtered adapter:

String a = parent.getAdapter().getItem(position);

or

String a = parent.getItemAtPosition(position);
like image 101
frogmanx Avatar answered Oct 01 '22 19:10

frogmanx