过滤listView后如何启动正确的活动

时间:2017-03-16 23:48:15

标签: android listview arraylist

我的应用在ActionBar中具有搜索功能,可以在MainActivity中过滤listView。不幸的是,在我过滤listView并单击列表中的第一个元素后,app会在过滤之前打开列表中第一个元素的活动,而不是什么也不做(因为我还没有创建其他活动)。我想保持原始listView的位置。我的代码是具体的:

ArrayAdapter<String> adapter;

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

    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayShowHomeEnabled(true);
    actionBar.setIcon(R.mipmap.ic_launcher_round);

    final ListView listView = (ListView)findViewById(R.id.listViewElements);
    ArrayList<String> arrayElements = new ArrayList<>();
    arrayElements.addAll(Arrays.asList(getResources().getStringArray(R.array.array_elements)));
    adapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, arrayElements);
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            if (id == 0) {
                Intent myIntent = new Intent(MainActivity.this, wodor.class);
                startActivityForResult(myIntent, 0);
            }
        }
    });
}

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

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

        @Override
        public boolean onQueryTextChange(String newText) {
            adapter.getFilter().filter(newText);

            return false;
        }
    });

    return super.onCreateOptionsMenu(menu);
}

0 个答案:

没有答案