单击搜索结果项时,具有可单击列表视图的搜索过滤器显示错误的布局

时间:2017-10-04 17:51:07

标签: java android listview

当我点击搜索视图结果项时显示错误的活动布局。例如,我从搜索结果中点击中国项目,但它会去澳大利亚。单击时如何设置可点击的搜索过滤器项以获取正确的活动?

  final ListView listView = (ListView)findViewById(R.id.listView);
        ArrayList<String>arrayCountry = new ArrayList<>();
        arrayCountry.addAll(Arrays.asList(getResources().getStringArray(R.array.array_country)));

        adapter= new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, arrayCountry);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

                switch (position) {
                    case 0:
                        startActivity(new Intent(MainActivity.this, ActivityAustralia.class));break;
                    case 1:
                        startActivity(new Intent(MainActivity.this, ActivityJapan.class));break;
                    case 2:
                        startActivity(new Intent(MainActivity.this, ActivityChina.class));break;

                }
            }
        });
        listView.setAdapter(adapter);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater=getMenuInflater();
        inflater.inflate(R.menu.menu_search,menu);
        MenuItem item= menu.findItem(R.id.menuSearch);
        final SearchView searchView=(SearchView)item.getActionView();

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return true;
            }
            @Override
            public boolean onQueryTextChange(String query) {
                adapter.getFilter().filter(query);
                return false;
            }
        });
        return super.onCreateOptionsMenu(menu);
    }
}

在strings.xml中

<string name="app_name">SearchViewList</string>

<string-array name="array_country">
    <item>Australia</item>
    <item>Japan</item>
    <item>China</item>
    <item>India</item>
</string-array>

在menu_search.xml

android:title="search"

android:icon="@android:drawable/ic_menu_search"

app:actionViewClass="android.widget.SearchView"

app:showAsAction="always"
  

  

0 个答案:

没有答案