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

前端之家收集整理的这篇文章主要介绍了ListView自定义过滤器在过滤时选择了错误的项目[Android]前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是 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 arg3) {
            // TODO Auto-generated method stub

        }

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

        }
    });
}

}

解决方法

因为您过滤适配器使位置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);

猜你在找的Android相关文章