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
}
});
}
}
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With