ListView自定义过滤器在过滤时选择了错误的项目[Android]

时间:2013-11-12 10:21:03

标签: android listview listview-adapter

我是Android编程的新手,我有一个ListView,当过滤总是返回列表中的第一项时,我该如何解决这个问题?

例如,我的清单包含A.A,A.B,A.C,B.C,B.D。 当我想在列表中搜索以B开头的内容时,我会得到B.C,B.D但是当我点击B.C时,它会返回A.A和B.D,它会返回给我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

        }
    });
}

}

1 个答案:

答案 0 :(得分:16)

因为您过滤适配器使位置0的B.C和位置1的B.D.但是在onClickListener的实现中,您使用listview的位置直接从源ArrayList获取。

String a = patientArrayList.get(position);

而是从过滤后的适配器中获取它:

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

String a = parent.getItemAtPosition(position);