ListView custom filter gives the wrong item selected when filtering [Android]

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

For example, my list contains AA, AB, AC, BC, BD. When I want to search the list of things starting with B, I will get BC, BD, but when I press BC, it will return AA and BD to me, it will return AB to me

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 } }); } 

}

+6
source share
1 answer

Because you are filtering your adapter to have BC at positions 0 and BD at position 1. But in your implementation of your onClickListener, you use the listview position to directly get from the original ArrayList.

 String a = patientArrayList.get(position); 

Instead, to get it from a filtered adapter:

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

or

 String a = parent.getItemAtPosition(position); 
+15
source

Source: https://habr.com/ru/post/957898/


All Articles