过滤器搜索后列表视图中的复选框更改位置

Posted

技术标签:

【中文标题】过滤器搜索后列表视图中的复选框更改位置【英文标题】:Checkbox in listview changes position after filter search 【发布时间】:2015-09-23 11:35:14 【问题描述】:

我的checkbox在过滤器搜索后改变了位置,我不明白为什么 我将在过滤后的列表中再检查一项:

现在我将清除过滤器,结果将是这样的:

过滤器和checkbox 工作正常,只有当我过滤结果并单击checkbox 时它仍然在顶部??

这是我的代码:

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

        getAllContacts(this.getContentResolver());
        friends = (ListView) findViewById(R.id.listView);
        ma = new MyAdapter();
        friends.setAdapter(ma);
        friends.setOnItemClickListener(this);
        friends.setItemsCanFocus(false);
        friends.setTextFilterEnabled(true);
        // adding
        toolbar = (Toolbar) findViewById(R.id.tool_bar4);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        Intent i = getIntent();
        groupname = i.getStringExtra("name");

        search = (EditText) findViewById(R.id.editText12);

        but = (Button) toolbar.findViewById(R.id.buttonz);
        but.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                StringBuilder checkedcontacts = new StringBuilder();
                System.out.println("..." + ma.mCheckStates.size());
                for (int i = 0; i < phno1.size(); i++) 
                    if (ma.mCheckStates.get(i) == true) 
                        checkedcontacts.append(phno1.get(i).toString());
                        checkedcontacts.append("\n");
                     else 
//                        System.out.println("Not Checked.." + phno1.get(i).toString());
                    
                
                Toast.makeText(Choosegroup.this, checkedcontacts, Toast.LENGTH_LONG).show();
            
        );
        friends.setEmptyView(findViewById(R.id.emptyElement5));
        friends.setOnTouchListener(new View.OnTouchListener() 
            @Override
            public boolean onTouch(View v, MotionEvent event) 
                int action = event.getAction();
                switch (action) 
                    case MotionEvent.ACTION_DOWN:
                        // Disallow ScrollView to intercept touch events.
                        v.getParent().requestDisallowInterceptTouchEvent(true);
                        break;

                    case MotionEvent.ACTION_UP:
                        // Allow ScrollView to intercept touch events.
                        v.getParent().requestDisallowInterceptTouchEvent(false);
                        break;
                
                return false;
            
        );
        search.addTextChangedListener(new TextWatcher() 

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) 

            

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                          int arg3) 
            

            @Override
            public void afterTextChanged(Editable s) 
                Log.d("tag", "*** Search value changed: " + s.toString());
                ma.getFilter().filter(s.toString());
                ma.notifyDataSetChanged();
            
        );
    
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
        // TODO Auto-generated method stub
        ma.toggle(arg2);
    

    public  void getAllContacts(ContentResolver cr) 

        String sortOrder = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
        Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, sortOrder);
        while (phones.moveToNext())
        
            String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            System.out.println("..."+phoneNumber);
            name1.add(name);
            phno1.add(phoneNumber);
        
        phones.close();

    
    class MyAdapter extends BaseAdapter implements CompoundButton.OnCheckedChangeListener
      private SparseBooleanArray mCheckStates;
        private Context mContext;
        List<String> mOriginalValues; // Original Values
        LayoutInflater mInflater;
        TextView tv1,tv;
        CheckBox cb;
        MyAdapter()
        
            mCheckStates = new SparseBooleanArray();
            mInflater = (LayoutInflater)Choosegroup.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        
        @Override
        public int getCount() 
            // TODO Auto-generated method stub
            return name1.size();
        

        @Override
        public Object getItem(int position) 
            // TODO Auto-generated method stub
            return name1.get(position);
        

        @Override
        public long getItemId(int position) 
            // TODO Auto-generated method stub
            return position;
        
        class ViewHolder 
            protected TextView tv1,tv;
            protected CheckBox cb;
        
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) 
            // TODO Auto-generated method stub
            ViewHolder viewHolder;
            if(convertView==null)
                convertView = mInflater.inflate(R.layout.groupuser, null);
                viewHolder = new ViewHolder();
                viewHolder.tv = (TextView) convertView.findViewById(R.id.textView32);
                viewHolder.tv1 = (TextView) convertView.findViewById(R.id.textView33);
                viewHolder.cb = (CheckBox) convertView.findViewById(R.id.checkBox);
                convertView.setTag(viewHolder);
             else 
                viewHolder = (ViewHolder) convertView.getTag();
            

            viewHolder.tv.setText(name1.get(position));
            viewHolder.tv1.setText(phno1.get(position));
            viewHolder.cb.setTag(position);
            viewHolder.cb.setChecked(mCheckStates.get(position));
            viewHolder.cb.setOnCheckedChangeListener(this);

            return convertView;
        
        public boolean isChecked(int position) 
            return mCheckStates.get(position, false);
        

        public void setChecked(int position, boolean isChecked) 
            mCheckStates.put(position, isChecked);
            System.out.println("hello...");
            ma.notifyDataSetChanged();
        

        public void toggle(int position) 
            setChecked(position, !isChecked(position));
        
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
            // TODO Auto-generated method stub
            mCheckStates.put((Integer) buttonView.getTag(), isChecked);
        

        public android.widget.Filter getFilter() 
            return new android.widget.Filter() 
                @Override
                protected FilterResults performFiltering(CharSequence constraint) 
                    FilterResults results = new FilterResults();        // Holds the results of a filtering operation in values
                    List<String> FilteredArrList = new ArrayList<String>();

                    if (mOriginalValues == null)
                    
                        System.out.println("");
                        mOriginalValues = new ArrayList<String>(name1); // saves the original data in mOriginalValues
                    

                    /********
                     *
                     *  If constraint(CharSequence that is received) is null returns the mOriginalValues(Original) values
                     *  else does the Filtering and returns FilteredArrList(Filtered)
                     *
                     ********/
                    if (constraint == null || constraint.length() == 0)
                    

                        // set the Original result to return
                        results.count = mOriginalValues.size();
                        results.values = mOriginalValues;
                    
                    else
                    
                        constraint = constraint.toString().toLowerCase();
                        for (int i = 0; i < mOriginalValues.size(); i++)
                        
                            String data = mOriginalValues.get(i);
                            if (data.toLowerCase().startsWith(constraint.toString().toLowerCase()))
                            
                                FilteredArrList.add(mOriginalValues.get(i));
                            
                        
                        // set the Filtered result to return
                        results.count = FilteredArrList.size();
                        results.values = FilteredArrList;
                    
                    return results;
                

                @Override
                protected void publishResults(CharSequence constraint, FilterResults results) 
                    name1 = (List<String>) results.values; // has the filtered values
                    ma.notifyDataSetChanged();
                
            ;
        
    

【问题讨论】:

【参考方案1】:

浏览此链接并尝试这样的代码,

http://www.mysamplecode.com/2012/07/android-listview-checkbox-example.html

【讨论】:

感谢 ashish 的回复,我已经查看了示例,但我仍然卡住了,您有基于代码本身的建议吗?

以上是关于过滤器搜索后列表视图中的复选框更改位置的主要内容,如果未能解决你的问题,请参考以下文章

使用导致崩溃的搜索结果更新表视图

检索数据表单数据库后列表视图不更新

当我在表格视图的搜索栏中搜索时如何检索复选框按钮位置更改

隐藏搜索结果中的部分标签

在视图之间导航时更改表视图的位置

使用没有搜索栏的获取结果控制器过滤表视图?