简体   繁体   中英

checkbox in listview doesn't save status when scrolling Android

I have a list view from SQLite with checkboxes .. It is all working fine , but when I click on one or more checkboxes and change their status then scroll down .. the checkbox status changes back to the original value (when the listview first created)

here is my code .. hope you can tell me how to solve it :

     public void showSQL(){
              /*
       *  Open the same SQLite database
       *  and read all it's content.
       */
    mySQLiteAdapter = new SQLiteAdapter(this);
    mySQLiteAdapter.openToRead();

    Cursor cursor = mySQLiteAdapter.queueAll();
    startManagingCursor(cursor);

    from = new String[]{SQLiteAdapter.NUMBER_CONTENT};



     to = new int[]{R.id.text};

    SimpleCursorAdapter cursorAdapter =
            new SimpleCursorAdapter(this, R.layout.row, cursor, from , to);

    cursorAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder(){
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
            String number ;
            String is_star ;
            if (columnIndex == cursor.getColumnIndex("numbers")) {
                // If the column is IS_STAR then we use custom view.
                number = cursor.getString(1);
                is_star = cursor.getString(cursor.getColumnIndex("status"));

                if (is_star.equals("true")) {
                    // set the visibility of the view to GONE
                    ((CheckBox) view).setText("        " + number);
                    ((CheckBox) view).setChecked(true);
                    return true;
                }else{
                    ((CheckBox) view).setText("        " + number);
                    ((CheckBox) view).setChecked(false);
                    return true;
                }
                //return true;

            }
            return false;

        }

    });
    stopManagingCursor(cursor);


    listContent.setAdapter(cursorAdapter);

    listContent.setOnItemClickListener(listContentOnItemClickListener);

    mySQLiteAdapter.getAllNumbers();

    mySQLiteAdapter.close();

}


private ListView.OnItemClickListener listContentOnItemClickListener
        = new ListView.OnItemClickListener(){

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
                            long id) {
        // TODO Auto-generated method stub
        Cursor cursor = (Cursor) parent.getItemAtPosition(position);
        int item_id = cursor.getInt(cursor.getColumnIndex(SQLiteAdapter.NUMBER_ID));
        String item_content1 = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.NUMBER_CONTENT));

        mySQLiteAdapter = new SQLiteAdapter(getBaseContext());
        item = (CheckBox) view;

        mySQLiteAdapter.openToRead();
        String check = null;
        Cursor c = mySQLiteAdapter.getNumberStatus(item_id);
        if (c.moveToFirst()){
            check = c.getString(c.getColumnIndex(SQLiteAdapter.NUMBER_STATUS));
        }
        mySQLiteAdapter.close();



        //The change color logic is here!
       if(item.isChecked()) {
           mySQLiteAdapter.openToWrite();
           mySQLiteAdapter.updateNumberStatus(item_id,"false");
           mySQLiteAdapter.close();
          // Toast.makeText(MainActivity.this, "deleted" +" "+ check, Toast.LENGTH_LONG).show();
           item.setChecked(false);

       }
       else {
           mySQLiteAdapter.openToWrite();
           mySQLiteAdapter.updateNumberStatus(item_id,"true");
           mySQLiteAdapter.close();
          // Toast.makeText(MainActivity.this, item_content1 +" "+ check , Toast.LENGTH_LONG).show();
           item.setChecked(true);


       }





    }};

The issue with CheckBox inside ListView is that the view gets recycled due to recycling of view.

To, maintain the state to CheckBox there has to be something that can store the state of Checkbox.

Refer this link,it is very nice example explained here.

Link 1

You have to make a class to get and set state of checkbox . that will be used as arraylist to save state of checkbox. and use it to show checked item.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM