使用SimpleAddapter在listview中过滤后没有获得正确的位置

时间:2013-05-25 06:20:53

标签: android performance android-listview simpleadapter

创建一个arraylist

public ArrayList<HashMap<String,Object>> list=new ArrayList<HashMap<String,Object>>();

添加simpleAddapter以显示4个fielsd 现在我想搜索列表中的项目,所以我使用了

search.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        ((SimpleAdapter)getListAdapter()).getFilter().filter(s); 
                        }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub

    }

    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub

    }
});

它正确过滤listView,但是当我尝试访问listview项时,它将不会返回正确的位置,即如果文件后显示两个项目,则位置与旧列表项相同。 那么如何从onListItemClick获取正确的数据?

3 个答案:

答案 0 :(得分:0)

将过滤后的ArrayList发送到自定义适配器,在其构造函数中初始化它,然后在适配器的getItem()函数中使用ArrayList,如:

public Object getItem(int position) {
    return list.get(position).get("key");
}

答案 1 :(得分:0)

这对我有用

mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, final View view,
                                    int position, long id) {

                //abrirActividadDetallada(position, id);
                Object MyObject=(Object) parent.getAdapter().getItem(position);
                CustomMethod(MyObject);
            }
        });

答案 2 :(得分:0)

    #get actual position of list after filter listview
    1.
      @Override// use in adapter
        public long getItemId(int position) {
            return list.get(position).getID();
        }
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.d(“actual position”,String.valueof((int)id))
                }enter code here
            });