在Android的字符串类型listview上实现searchview [英] Implement searchview on string type listview Android

查看:285
本文介绍了在Android的字符串类型listview上实现searchview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个items列表,我想基于item开始其他活动,当我单击它时,它会打开正确的活动,但是当我尝试从搜索视图栏中搜索列表项时,它会打开错误活动.

I have a list of items and i want to start different activity on the basis of item, when i click it opens the correct activity but when i try to search list items from search view bar then it opens wrong activities.

    listView = (ListView) findViewById(R.id.listView);
    sv=(SearchView) findViewById(R.id.searchView1);
    String[] values = new String[]{item1,item2,item3,item4,}
    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_2, android.R.id.text1, values);
    listView.setAdapter(adapter);
    //linking from 1 item to other activity stars with if options//
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            if (position == 0) {
                Intent myIntent = new Intent(view.getContext(), activity1.class);
                startActivityForResult(myIntent,0);
            }

            if (position == 4) {
                Intent myIntent = new Intent(view.getContext(), aactivity4.class);
                startActivityForResult(myIntent,0);

            }
        }
    });

    sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String text) {
            // TODO Auto-generated method stub
            return false;
        }
        @Override
        public boolean onQueryTextChange(String text) {
            adapter.getFilter().filter(text);
            return false;
        }
    });

我对编码不太了解,但是任何人都可以解决我的问题.

I don't know much about coding but can any one solve my problem..

推荐答案

您正在基于position开始活动,但是在进行搜索时position将会更改,因为列表会缩小并且positions将会更改,以获取与列表中指定位置相关联的数据,请使用 getItemAtPosition

you are starting your activity on the basis of position but the position will be changed when you do the search because list will shrink and positions will change so to get data associated with the specified position in the list use getItemAtPosition

因此根据数据更改条件

 if (parent.getItemAtPosition(position).equals("item1")) {
                Intent myIntent = new Intent(view.getContext(), activity1.class);
                startActivityForResult(myIntent,0);
            }

else if (parent.getItemAtPosition(position).equals("item2")) { // use any item value here you want 
                Intent myIntent = new Intent(view.getContext(), aactivity4.class);
                startActivityForResult(myIntent,0);
            }

注意:您也可以使用switch代替长的ifelse-if梯形图

Note : you can use switch as well instead of long if or else-if ladder

例如,您有三个字符串

item 1   position 0
item 2   position 1
item 3   position 2

搜索完第2项后,您列表中的两个值接近搜索

after searching item 2 you have two values in your list close to your search

item 2   position 0
item 3   position 1

因此位置会发生变化,因此请不要使用它,而要使用数据

so position will change so don't use it instead use the data

代码

       listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Intent intent = null;
                // global string to class
                selectedValue = String.valueOf(parent.getItemAtPosition(position));

                if (selectedValue.equals("item1")) {
                                        // ^^^  use any item value here you want
                    Intent myIntent = new Intent(view.getContext(), activity1.class);
                    startActivityForResult(myIntent,0);
                }

                else if (selectedValue.equals("item2")) {
                    Intent myIntent = new Intent(view.getContext(), aactivity4.class);
                    startActivityForResult(myIntent,0);
                }
            }
        });

这篇关于在Android的字符串类型listview上实现searchview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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