滚动Android时,listview中的复选框不保存状态

时间:2013-06-17 12:26:20

标签: java android listview checkbox scroll

我有一个带有复选框的SQLite列表视图.. 一切正常,但是当我点击一个或多个复选框并更改其状态然后向下滚动...复选框状态将更改回原始值(当列表视图首次创建时)

这是我的代码..希望你能告诉我如何解决它:

     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);


       }





    }};

2 个答案:

答案 0 :(得分:2)

ListView内部CheckBox的问题在于视图因视图回收而被回收。

要将状态保持为CheckBox,必须存在可以存储Checkbox状态的内容。

参考this链接,这是非常好的例子。

Link 1

答案 1 :(得分:0)

你必须创建一个类来获取和设置复选框的状态。将用作arraylist来保存复选框的状态。并用它来显示检查项目。