Unable to check/uncheck CheckedTextView inside getView

后端 未结 3 1339
悲哀的现实
悲哀的现实 2020-12-15 21:13

I\'m loading phone contacts in a custom ListView. Each row is a checkable LinearLayout containing a CheckedTextView and another TextView.

I\'m feeding the list view

相关标签:
3条回答
  • 2020-12-15 22:01

    Did you set a checkMark property for your CheckedTextView in your layout xml?

    For example: android:checkMark="?android:attr/listChoiceIndicatorMultiple

    CheckedTextView is not just a checkbox with a text. You must also note, that CheckedTextView is not focusable, or clickable without some manipulation (since it was designed for ListView and therefore it's state must be controlled by ListView's setOnItemClickListener)

    setChoiceMode should be set for a ListView. And checking of the row inside adapter's getView should be done via: listView.setItemChecked(position, value)

    0 讨论(0)
  • 2020-12-15 22:05

    @Pier-Luc Gendreau and @fox mentioned the solution in comments but not in answer, so posting answer on their behalf.

    As mentioned by @AlexOrlov, CheckedTextView is not focusable, or clickable without some manipulation. So what you have to do is set item check using ListView, you can either do it from the adapter itself or from activity/fragment where you have ListView.

    To do it from adapter,

    public View getView(int position, View convertView, ViewGroup parent) 
    { 
    if (//logic) {
    ((ListView) parent).setItemChecked(position, true);
    }
    }
    

    From activity/fragment

    if (//logic) {
    listView.setItemChecked(position, true);
    }
    
    0 讨论(0)
  • 2020-12-15 22:05

    When ever you scroll list, every time it calls its getview() method. So, if you have any checkbox or any editbox in listcell it will reinitialize it.

    My idea is to store the status (checked or unchecked) of checkbox. So here I used ArrayList first I filled it with false value then on its click event i used to store it actual status.

     class MyAdapter extends BaseAdapter  implements OnClickListener   {
    
        private ArrayList<Boolean> checks=new ArrayList<Boolean>();//Boolean type array to   manage check box
    
      private static LayoutInflater inflater=null;
    
        public MyAdapter(Context context, ArrayList<HashMap<String, String>> d)
        {
                  inflater =     (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
          //fill with false values
        for (int i = 0; i < d.size(); i++) 
        {
                checks.add(i, false);
        }
        }
    
        public int getCount()
        {
            return data.size();
        }
    
        public Object getItem(int position)
        {
            return position;
        }
    
        public long getItemId(int position) 
        {
            return position;
        }
    
    
    
    
            public View getView(int position, View convertView, ViewGroup parent)
           {
    
            View vi=convertView;
    
            if(convertView==null)
    
              vi = inflater.inflate(R.layout.<your layout>, null);
    
              //Checkbox is of button type----android:button="@drawable/btn_check"
              //make a selector xml for checkbox       
    
              checkBox=(CheckBox)vi.findViewById(R.id.check_box);
    
    
              checkBox.setTag(Integer.valueOf(position));
              checkBox.setOnClickListener(this);
              checkBox.setChecked(checks.get(position));
    
    
            return vi;
        }
    
            @Override
            public void onClick(View v) 
            {
              int viewId=v.getId();
              if(viewId== R.id.check_box)
              {
                Integer index = (Integer)v.getTag();
                boolean state = checks.get(index.intValue());
                checks.set(index.intValue(), !state);
    
              }
            }
         }
    

    Update: Solution 2nd: You can put a boolean variable in your ViewHolder class. This boolean variable will used to define wether item is selected or not.

    Hope this should help you.

    0 讨论(0)
提交回复
热议问题