ListView中的搜索过滤器未按预期工作

时间:2014-09-19 04:12:23

标签: android listview android-listview android-edittext android-search

我们有一个列表视图片段,其中包含列表视图,编辑文本(用于搜索)和复选框(全选)。如上所述,我们使用“编辑文本”来搜索列表视图中的值。

问题:

  1. 我们有列表视图显示10个项目(按字母顺序排序的列表视图)。
  2. 当我们输入' W'在编辑文本中,我们显示两个值,即WF和WH。
  3. 选择WF(显示第一项)。
  4. 清除编辑文本中的字符后,现在会显示列表视图的完整列表。
  5. 但现在选择AHMD列表中的第一项。 (我们打算选择WF)
  6. 基本上,在我们执行搜索后,列表视图中数据的位置不会刷新。

    代码:

     public static class ListViewFragment extends Fragment {
    
        private ListView myListView;
        private EditText editsearch;
        private CheckBox selectAll;
        private String SELECTED_FILTER;
        private ArrayList<String> SelectedFilter1;
        private ArrayList<String> checkekprev=new ArrayList<String>();
        private ArrayList<String> CheckedFitlers=new ArrayList<String>();
    
        ArrayAdapter<String> adapter_list;
    
        OnCheckedItemsLister onCheckedItemsLister;     
    
        public ListViewFragment() {
            // Empty constructor required for fragment subclasses
        }
    
        @Override
        public void onAttach(Activity activity) {
          super.onAttach(activity);
              try {
                  onCheckedItemsLister = (OnCheckedItemsLister) activity;
              } catch (ClassCastException e) {
                  throw new ClassCastException(activity.toString() + " must implement onSomeEventListener");
              }
        }
    
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);  
            setRetainInstance(true);
            SelectedFilter1=getArguments().getStringArrayList("ArrayList");
            SELECTED_FILTER=getArguments().getString("SELECTED_FILTER");
            checkekprev=getArguments().getStringArrayList("checkedprevlist");
    
    
    
    
            if(checkekprev.size()==SelectedFilter1.size())
                selectAll.setChecked(true);
                else
                selectAll.setChecked(false);
    
            adapter_list = new ArrayAdapter<String>(this.getActivity(),
                    android.R.layout.simple_list_item_multiple_choice,SelectedFilter1);
    
            adapter_list.notifyDataSetChanged();
    
            myListView.setAdapter(adapter_list);
    
            myListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    
            myListView.setTextFilterEnabled(true);
    
            try{
                if(checkekprev.isEmpty()==false){
                    Log.i("Adapter set", "Yes");
                    for(int j=0;j<checkekprev.size();j++){
                        Log.i("Adapter set", "Yes:"+j);
                    int pos=adapter_list.getPosition(checkekprev.get(j));
                    Log.i("Adapter set", "Pos:"+pos);
                    myListView.setItemChecked(pos,true);
                    }
    
                }           
            }catch(NullPointerException  e){
                Log.i("Null set", "Yes");
            }
    
            editsearch.addTextChangedListener(new TextWatcher() {
    
                @Override
                public void afterTextChanged(Editable arg0) {
                    // TODO Auto-generated method stub
                    String text = editsearch.getText().toString().toLowerCase(Locale.getDefault());
                    adapter_list.getFilter().filter(text);
                }
    
                @Override
                public void beforeTextChanged(CharSequence arg0, int arg1,
                        int arg2, int arg3) {
                    // TODO Auto-generated method stub
                }
    
                @Override
                public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                        int arg3) {
                    // TODO Auto-generated method stub
                }
            });
    
            selectAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    
                @Override
                public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
    
                    Log.i("CheckBOX","Is Checked:"+isChecked);
                    if(isChecked)
                    {
                        for ( int i=0; i< myListView.getCount(); i++ ) 
                            myListView.setItemChecked(i, true);
    
                    }else
                    {
                        for ( int i=0; i< myListView.getCount(); i++ ) 
                            myListView.setItemChecked(i, false);
                    }
                    SparseBooleanArray checked = myListView.getCheckedItemPositions();
                    CheckedFitlers.clear();
                    for(int i=0;i<=checked.size();i++){
                        if (checked.get(i)){
                            CheckedFitlers.add(SelectedFilter1.get(i));
                        }
                    }
                    onCheckedItemsLister.someEvent(CheckedFitlers,SELECTED_FILTER);
                }
            }
         );
    
            myListView.setOnItemClickListener(new OnItemClickListener() { 
    
                public void onItemClick(AdapterView<?> parent, View view,int position, long id){
                    setItemChecked(position);
                    onCheckedItemsLister.someEvent(CheckedFitlers,SELECTED_FILTER); 
    
                }               
            });
    
        }
    
        public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
    
            if(savedInstanceState!=null){
                checkekprev=savedInstanceState.getStringArrayList("Checked_Values");
            }
        }
    
        public void setItemChecked(int position){
    
            //if(SelectedFilter1.get(position).)
    
                //int len = myListView.getCount();
                SparseBooleanArray checked = myListView.getCheckedItemPositions();
    
                for(int i=0;i<=checked.size();i++){
                    Log.i("Check Sparse Array","Check Sparse Array: "+checked.get(i));
                }
    
                //for (int i = 0; i < len; i++){
                    Log.i("SelectedFilter1.get(position)","SelectedFilter1.get(position):::"+SelectedFilter1.get(position));
    
                    Log.i("checked.get(position))","checked.get(position)):::"+checked.get(position));
    
                    for (int i=0; i<CheckedFitlers.size();i++){
                        Log.i("CheckedFitlers","CheckedFitlers:::" + CheckedFitlers.get(i));
                    }
                    for (int i=0; i<checkekprev.size();i++){
                        Log.i("checkekprev","checkekprev:::" + checkekprev.get(i));
                    }
                    Log.i("CheckedFitlers","CheckedFitlers:::" + CheckedFitlers.size());
                    Log.i("checkekprev","checkekprev:::" + checkekprev.size());
    
                    if(CheckedFitlers.size() == 0 && checkekprev.size() != 0){
                        CheckedFitlers = checkekprev;
                    }
    
                    Log.i("CheckedFitlers","CheckedFitlers:::" + CheckedFitlers.size());
    
                    if (checked.get(position)) {
                        Log.i("Adding","Adding::"+checked.get(position));
    
                        CheckedFitlers.add(SelectedFilter1.get(position));
                     for(int i=0;i<checkekprev.size();i++){
                            //Log.i("Checked Filters FOr loop","Checked Filters:"+CheckedFitlers.get(i));
                        }
                   }    else
                   {
                    Log.i("Removing","Removing::"+checked.get(position)+":"+SelectedFilter1.get(position));
                    CheckedFitlers.remove(SelectedFilter1.get(position));
                        for(int i=0;i<checkekprev.size();i++){
                            //Log.i("Checked Filters for loop","Checked Filters:"+CheckedFitlers.get(i));
                        }
                   }
                    Log.i("CheckedFitlers","CheckedFitlers:::" + CheckedFitlers.size());
    
        }
    
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {        
    
            View rootView = inflater.inflate(R.layout.fragment_list_view, container, false);
    
            myListView=(ListView) rootView.findViewById(R.id.listView_brand);
    
            editsearch = (EditText) rootView.findViewById(R.id.search);
    
            selectAll=(CheckBox) rootView.findViewById(R.id.checkBox1);
    
            selectAll.setFocusable(false);
    
            return rootView;
        }
    
        public void onSaveInstanceState(Bundle savedInstanceState){
            super.onSaveInstanceState(savedInstanceState);
    
            Log.i("savedInstanceState in listfragment","savedInstanceState in listfragment");
            //onCheckedItemsLister.someEvent(CheckedFitlers,SELECTED_FILTER);
            onCheckedItemsLister.getSelectedFilter(SELECTED_FILTER);
    
        }
    
    }
    

2 个答案:

答案 0 :(得分:1)

当您在编辑文本中键入“W”时,我们会显示两个值,即WF和WH。选择WF(显示第一项)。

但是您已经对列表视图的第一行执行了操作,现在检查列表视图的复选框,当清除编辑文本中的字符时,您将看到列表视图的完整列表,并选中第一个框。这是正确的。

如果您想避免此问题,请创建自定义适配器并覆盖getfilter方法。

否则,创建自己的逻辑并调用listView.setItemChecked(存储位置,true / false);在ontextchanged。

答案 1 :(得分:0)

在此示例中,假设活动标签和列表视图项具有相等的值。从WF到WZ有10个项目。 (我采样了4个项目)在编辑文本中键入“ WZ”时,“ WZ”项目显示在列表视图的第一行。单击第一行时,希望调用“ WZ”活动,而不是“ WF”活动。我已经为它创建了以下小代码,并且工作正常。

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

        String v1 = getString(R.string.title_activity_wf).toString();
        String v2 = getString(R.string.title_activity_wh).toString();
        String v3 = getString(R.string.title_activity_wm).toString();
        String v4 = getString(R.string.title_activity_wt).toString();
        .
        .
        .
        .
        .
        .

        String selectedFromList = (String) (listView.getItemAtPosition(position));

        if (selectedFromList.equalsIgnoreCase(v1)) {
            startActivity(new Intent(getApplicationContext(), Wf.class));
        } else if (selectedFromList.equalsIgnoreCase(v2)) {
            startActivity(new Intent(getApplicationContext(), Wh.class));
        } else if (selectedFromList.equalsIgnoreCase(v3)) {
            startActivity(new Intent(getApplicationContext(), Wm.class));
        } else if (selectedFromList.equalsIgnoreCase(v4)) {
            startActivity(new Intent(getApplicationContext(), Wt.class));

    }
});