Android save Checkbox State in ListView with Cursor Adapter

后端 未结 5 1946
忘掉有多难
忘掉有多难 2020-11-27 19:48

I cant find a way to save the checkbox state when using a Cursor adapter. Everything else works fine but if i click on a checkbox it is repeated when it is recycled. Ive see

5条回答
  •  野性不改
    2020-11-27 20:26

    I had the same issue myself: how to toggle multiple select CheckedTextView in a custom layout (i.e not using android.R.layout.simple_list_item_multiple_choice)

    The following worked for me. The example I have is a custom view that is provided an adaptor extended from SimpleCursorAdapter.

    My custom view (row_contact.xml):

    
    
    
      
    
      
    
    
    

    The adaptor is created in ListActivity.OnCreate, which calls setupViews():

      private void setupViews() {
        bOk       = (Button) findViewById(R.id.ButtonOK);
        bCancel   = (Button) findViewById(R.id.ButtonCancel);
        FListView = getListView(); 
        //
        bOk.setText("Select");
        //
        FListView.setItemsCanFocus(false);
        FListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        //
        bOk.setOnClickListener(this);
        bCancel.setOnClickListener(this);
        //
        ContentResolver content = getContentResolver();
        Cursor cursor = ApplicationHelper.MobilePhoneContacts(content);
        startManagingCursor(cursor);
    
        ListAdapter adapter = new CheckedCursorAdapter( SelectContacts.this, R.layout.row_contact, cursor,                
            new String[] {People.NAME, People.NUMBER},               
            new int[] {android.R.id.text1, R.id.tvNumber});          
        setListAdapter(adapter);
      }
    

    The Custom adaptor:

      public class CheckedCursorAdapter extends SimpleCursorAdapter {
    
        Activity context;
        Cursor c;
    
        public CheckedCursorAdapter(Activity context, int rowContact, Cursor cursor, String[] strings, int[] is) {
          super(context, rowContact, cursor, strings, is);
          this.context = context;
          this.c = cursor;
    
        }
    
        public View getView(int position, View convertView, ViewGroup parent) {
          View row = convertView;
          ContactRowWrapper wrapper;
    
          if (row == null) {
            LayoutInflater inflater=context.getLayoutInflater();
            row = inflater.inflate(R.layout.row_contact, null);
            //
            wrapper = new ContactRowWrapper(row);
            row.setTag(wrapper);
          } else {
            wrapper = (ContactRowWrapper)row.getTag();
          }
          c.moveToPosition(position);
          wrapper.getTextView().setText( c.getString(c.getColumnIndex(Contacts.People.NUMBER)) );
          wrapper.getcheckBox().setText( c.getString(c.getColumnIndex(Contacts.People.NAME)) );
          wrapper.getcheckBox().setChecked(getListView().isItemChecked(position));
          //
          return row;
        }
    
      }
    

    The crucial bit of code for for me was to get check boxes working was:

    wrapper.getcheckBox().setChecked(getListView().isItemChecked(position));
    

    Hope this helps you or anyone else who stumbles onto this question.

    Also, pardon my Java noobness... I've only started Java a few weeks ago.

提交回复
热议问题