Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ListView自定义筛选器在筛选[Android]时提供了错误的选定项_Android_Listview_Listview Adapter - Fatal编程技术网

ListView自定义筛选器在筛选[Android]时提供了错误的选定项

ListView自定义筛选器在筛选[Android]时提供了错误的选定项,android,listview,listview-adapter,Android,Listview,Listview Adapter,我是Android编程新手,我有一个列表视图,过滤后总是返回列表中的第一项,那么我该如何解决这个问题呢 例如,我的列表包含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;

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

例如,我的列表包含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

        }
    });
}
公共类PatientList扩展活动{
PatientDB PatientDB;
语境;
ListView LVU患者;
EditText et_患者;
阵列适配器;
ArrayList患者列表;
静态字符串值;
字符串[]patientarray=新字符串[]{“缩写”};
ArrayList patientArrayList=新建ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u patient\u list);
myContext=这个;
patientDB=新的patientDB(myContext);
patientDB.open();
光标-患者-光标;
patientCursor=patientDB.retrieveAllEntriesCursor();
if(patientCursor!=null&&patientCursor.getCount()>0)
{
细绳;
patientCursor.moveToFirst();
做
{
patientCursor.getString(patientDB.COLUMN_KEY_ID);//+“”+
patientInitials=patientCursor.getString(patientDB.COLUMN\u缩写\u ID);
Log.i(“FromCursor”,patientInitials);
patientArrayList.add(patientInitials);
}while(patientCursor.moveToNext());
}
lv_患者=(ListView)findViewById(R.id.lv_患者);
et_患者=(EditText)findViewById(R.id.et_患者);
lv_患者。setOnItemClickListener(新的OnientemClickListener(){
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
//TODO自动生成的方法存根
字符串a=patientArrayList.get(位置);
Toast.makeText(PatientList.this,a+“selected”,Toast.LENGTH_SHORT).show();
Intent returnIntent=newintent(PatientList.this,PatientInfo.class);
returnIntent.putExtra(“值”,a);
setResult(结果正常,返回意图);
完成();
}
});
//向listview添加项目
适配器=新阵列适配器(此,R.layout.list_患者,R.id.patient_名称,patientArrayList);
lv_患者。设置适配器(适配器);
//启用搜索筛选器
et_patients.addTextChangedListener(新的TextWatcher(){
public void onTextChanged(字符序列cs、int arg1、int arg2、int arg3){
//当用户更改文本时
PatientList.this.adapter.getFilter().filter(cs);
}
更改前的公共void(字符序列arg0、int arg1、int arg2、int arg3){
//TODO自动生成的方法存根
}
public void PostTextChanged(可编辑arg0){
//TODO自动生成的方法存根
}
});
}

}

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

String a = patientArrayList.get(position);
相反,要从筛选的适配器获取它,请执行以下操作:

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


我正在尝试做一些类似的事情。请检查下面的链接以获取我的代码。如果“cust”对象与适配器中的对象相同,您应该可以。否则,我将引用适配器数据结构,而不是使用此解决方案中概述的方法。在使用RecycleServiceWgood解决方案时,您能给我同样的指导吗!谢谢字符串aa=(字符串)parent.getAdapter().getItem(位置);最好的答案!!我不应该忽视这一点
String a = parent.getItemAtPosition(position);