ListView的自定义过滤器过滤提供了选择的时候错误的项目[Android版] [英] ListView custom filter gives wrong item selected when filtered [Android]

查看:154
本文介绍了ListView的自定义过滤器过滤提供了选择的时候错误的项目[Android版]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid编程和我有过滤的时候总是返回我的第一个项目列表中的一个ListView,所以我怎么解决这个问题?

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?

例如,我的列表中包含A.A,A·B,A.C,B.C,B.D.
当我要搜索的列表中开始为B的东西,我会得到B.C,B.D但是当我点击B.C,它返回我A.A和B.D,它返回我的A·B

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

        }
    });
}

}

推荐答案

由于您过滤适配器有BC在0位置和BD位置1.但在你执行你的onClickListener的,您可以使用ListView控件的位置,直接拿到从源头的ArrayList

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

String a = parent.getItemAtPosition(position);

这篇关于ListView的自定义过滤器过滤提供了选择的时候错误的项目[Android版]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆