使用searchview从列表中检索正确的项目

时间:2017-03-10 14:00:04

标签: android listview search searchview onitemclicklistener

我有一个可搜索的ListView,其条目是Item对象,其中包含一个名称为String的字符串,以及一个可绘制资源的int。

在搜索条目并选择它时,onItemClick()方法中记录了错误的条目,因为它们的位置发生了变化。如何在搜索后引用正确的项目?

以下是我的尝试;显然提及"位置"参数是问题,但我想不出解决方案来解决这个问题。

public class ItemRelicFragment extends DialogFragment {

    Button btn;
    ListView lv;
    SearchView sv;
    String[] names={"Aegis Amulet", "Blink Rune", "Bracer of Undoing", "Cursed Ankh", "Hand of the Gods",
                    "Heavenly Wings", "Horrific Emblem", "Magic Shell", "Meditation Cloak", "Phantom Veil",
                    "Purification Beads", "Shield of Thorns", "Sundering Spear", "Teleport Glyph"};

    int[] images={R.drawable.aegis_amulet, R.drawable.blink_rune, R.drawable.bracer_of_undoing, R.drawable.cursed_ankh, R.drawable.hand_of_the_gods,
            R.drawable.heavenly_wings, R.drawable.horrific_emblem, R.drawable.magic_shell, R.drawable.meditation_cloak, R.drawable.phantom_veil,
            R.drawable.purification_beads, R.drawable.shield_of_thorns, R.drawable.sundering_spear, R.drawable.teleport_glyph};


    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View myView=inflater.inflate(R.layout.item_list, null);

        lv=(ListView) myView.findViewById(R.id.listView1);
        sv=(SearchView) myView.findViewById(R.id.searchView1);
        btn=(Button) myView.findViewById(R.id.dismiss);

        //CREATE AND SET ADAPTER TO LISTVIEW
        final Adapter adapter = new Adapter(getActivity(), this.getItems());
        lv.setAdapter(adapter);

        //SEARCH
        sv.setQueryHint("Search relics..");
        sv.setOnQueryTextListener(new OnQueryTextListener() {
            public boolean onQueryTextSubmit(String txt) {
                return false;
            }
            public boolean onQueryTextChange(String txt) {
                adapter.getFilter().filter(txt);
                return false;
            }
        });

        //Listener for itemclick on list
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
            {
                int image_id = images[position];
                mListener.onListItemClick(image_id);
                dismiss();
            }
        });
(...) 
} 

private ArrayList<Item> getItems() {

        ArrayList<Item> items = new ArrayList<Item>();
        Item item;

        for(int i = 0; i < names.length; i++)
        {
            item = new Item(names[i], images[i]);
            items.add(item);
        }

        return items;

    }

1 个答案:

答案 0 :(得分:0)

我终于找到了解决这个问题的方法!

我的onItemClick(...)方法中的image_id变量指向我实际点击的位置,而不是我选择的项目。

以下是我引用该项目的方式:

public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
            {
                int image_id = adapter.getItem(position).getImage();
                mListener.onListItemClick(image_id);
                dismiss();
            }