簡體   English   中英

滾動Android時,listview中的復選框不保存狀態

[英]checkbox in listview doesn't save status when scrolling Android

我有一個帶有復選框的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);


       }





    }};

ListView內部CheckBox的問題在於視圖因回收視圖而被回收。

為了將狀態保持為CheckBox,必須存在可以存儲Checkbox狀態的東西。

請參閱鏈接,這是一個非常好的例子。

鏈接1

你必須創建一個類來獲取和設置復選框的狀態。 將用作arraylist來保存復選框的狀態。 並用它來顯示選中的項目。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM