Searchview过滤gridview,并获得switch结果而不是第一个switch case

时间:2016-05-15 08:45:20

标签: java android gridview

我已经实现了一个searchview来过滤我的gridview但是我还设置了一个switch case,当你点击不同的对象时,它会打开一个新的活动。发生的事情的例子;

This is the home screen, When you click on the first picture you get the corresponding activity

但是,当您在搜索栏中输入内容时,会过滤结果。我希望能够单击此图片并转到相应的相应活动。情况并非如此,而是转到第一个“切换案例”。活性;

Filtered results, Instead of showing the 3rd activity, its shows the first one

所以是的,我理解为什么会这样做,所以我只需要有人让我走上一条能给我解决方案的道路。

这是我的代码,用于处理gridview的开关案例;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    GridView gv = (GridView) findViewById(R.id.gridView);
    gv = (GridView) findViewById(R.id.gridView);
    sv = (SearchView) findViewById(R.id.searchView);


    final Adapter adapter = new Adapter(this, this.getChampions());
    gv.setAdapter(adapter);


    sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String arg0) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String query) {

            adapter.getFilter().filter(query);

            return false;
        }
    });

    gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            switch( position )
            {
                case 0:  Intent newActivity = new Intent(MainActivity.this, Aatrox.class);
                    startActivity(newActivity);
                    break;

                case 1:  Intent newActivity2 = new Intent(MainActivity.this, Ahri.class);
                    startActivity(newActivity2);
                    break;

                case 2:  Intent newActivity3 = new Intent(MainActivity.this, Akali.class);
                    startActivity(newActivity3);
                    break;

            }
        }
    });

}

private ArrayList<Champions> getChampions() {
    ArrayList<Champions> champions = new ArrayList<Champions>();
    Champions p;

    for (int i = 0; i < Champions.length; i++) {
        p = new Champions(Champions[i], Champimgs[i]);
        champions.add(p);
    }
    return champions;
}

}

我已经发布了这个试图澄清我的上一篇文章,这被标记为重复,问一个完全不同的问题的问题,谢谢。

1 个答案:

答案 0 :(得分:0)

移除您的开关并使用以下代码

if(ItemClicked.getName.equalIgnorecase("Aatrox")){
Intent newActivity = new Intent(MainActivity.this, Aatrox.class);
                startActivity(newActivity);

 }elseif(ItemClicked.getName.equalIgnorecase("Ahri")){
Intent newActivity2 = new Intent(MainActivity.this, Ahri.class);
                startActivity(newActivity2);
}else if(ItemClicked.getName.equalIgnorecase("Akali")){
Intent newActivity3 = new Intent(MainActivity.this, Akali.class);
                startActivity(newActivity3);
}

注意:保留其余代码