8

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

例如,我的列表包含 AA、AB、AC、BC、BD 当我想在列表中搜索以 B 开头的内容时,我会得到 BC、BD 但是当我单击 BC 时,它返回给我 AA 和 BD,它返回给我AB

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

        }
    });
}

}

4

1 回答 1

18

因为您过滤了您的适配器以使 BC 位于位置 0,BD 位于位置 1。但是在您的 onClickListener 实现中,您使用列表视图的位置直接从源 ArrayList 获取。

String a = patientArrayList.get(position);

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

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

或者

String a = parent.getItemAtPosition(position);
于 2013-11-12T10:29:29.543 回答