Listview在listview onclick中返回原始位置

时间:2016-12-02 19:34:25

标签: java android listview filter

我已经花了好几个小时在网上找到这个答案,但是我看起来越混乱。有人可以帮我弄这个吗?

这是我搜索项目的类,可以单击搜索到的项目。但是它总是返回未过滤的列表视图中的第一个位置,在下一个意图中给出错误的变量。

    db = new DBAdapter(this);
    db.open();

    final ArrayList<String> list = new ArrayList<String>();
    final ArrayList<Integer> listId = new ArrayList<>();

    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(ListChosenCategory.this);
    final int categoryId = settings.getInt("CategoryId",0);
    Log.d("hehehe","categoryid"+categoryId);


    //Fills out the listview with items
    Cursor cur = db.listAllItems(categoryId);
    if(cur.moveToFirst())
    {
        do
        {
            list.add(cur.getString(0));
            listId.add(cur.getInt(cur.getColumnIndex(db.ITEMSID)));
        }
        while(cur.moveToNext());
    }
    cur.close();

    final ListView listItems = (ListView) findViewById(R.id.listAllCategoryItems);
    final StableArrayAdapter adapter = new StableArrayAdapter(ListChosenCategory.this,android.R.layout.simple_list_item_1,list,listId);
    listItems.setAdapter(adapter);
    adapter.notifyDataSetChanged();
    listItems.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
        public void onItemClick(final AdapterView<?> parent, final View view, final int position, long id)
        {
            int itemId = adapter.getActualId(position);
            Intent intent = new Intent(ListChosenCategory.this, ListItemDetails.class);
            PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putInt("ItemId",itemId).apply();
            startActivityForResult(intent,1);
        }
    });

    SearchView searchView = (SearchView)findViewById(R.id.SearchForItemText);
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String text) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String text) {
            adapter.notifyDataSetChanged();
            adapter.getFilter().filter(text);
            adapter.notifyDataSetChanged();
            return false;
        }
    });

这是我的arrayadapter:

私有类StableArrayAdapter扩展了ArrayAdapter

    HashMap<String, Integer> mIdMap = new HashMap<>();
    List<Integer> trialId;

    public StableArrayAdapter(Context context, int textViewResourceId, List<String> objects, List<Integer> objectId)
    {
        super(context, textViewResourceId, objects);
        for (int i = 0; i < objects.size(); i++)
            mIdMap.put(objects.get(i), i);
        trialId = objectId;
    }

    public long getItemId(int position)
    {
        String item = getItem(position);
        return mIdMap.get(item);
    }

    public int getActualId(int position)
    {
        return trialId.get(position);
    } 

非常感谢任何帮助

// EDIT

当我点击过滤后的列表中的33时,它显示了这一点,这是未过滤列表中的第一行。

Filtered List On Click

当我在未过滤的列表中单击33时,它会显示正确的变量等

Unfiltered List On Click

0 个答案:

没有答案