使用onClick视图搜索Listview显示错误

时间:2017-10-22 15:46:17

标签: android list listview search

任何人都可以帮我解决如何从listview的searchview中修复错误的结果。例如,我有列表视图中的数据(1,2,3,4,5)当我从searchview搜索数据2时,我点击它,总是数据1显示它,当我没有搜索来自searchview的数据时结果如果为真,我点击数据1显示1,2显示2

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    listView = (ListView) findViewById(R.id.listview);

    MaterialSearchBar searchBar = (MaterialSearchBar) findViewById(R.id.searchBar);
    searchBar.setHint("Search..");
    searchBar.setSpeechMode(true);
    final String[] values= new String[]{"cemento","muro","puerta"};
    final ArrayAdapter<String> adapter  = new ArrayAdapter<String>(this,
            android.R.layout.activity_list_item,android.R.id.text1,values);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view,
                                int position, long id) {
            if(position==0){
                Intent myintent = new Intent(view.getContext(),cemento.class);
                startActivityForResult(myintent,0);
            }
            if(position==1){
                Intent myintent = new Intent(view.getContext(),muro.class);
                startActivityForResult(myintent,1);
            }
            if(position==2){
                Intent myintent = new Intent(view.getContext(),puerta.class);
                startActivityForResult(myintent,2);
            }
        }
    });



    searchBar.addTextChangeListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            adapter.getFilter().filter(charSequence);
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });


}}

1 个答案:

答案 0 :(得分:0)

过滤后,点击侦听器的位置不能与if语句中的位置相同。

可能的解决方案:使用数据集获取点击项目的位置,并将其内容与您的值进行比较。