在Android中单击Listview的单个项目时选择ListView的多个项目

时间:2016-02-18 13:38:30

标签: android listview checkbox

我在ListView中有多个项目,项目列表大于屏幕尺寸。要查看所有项目,我向下滚动到ListViewListView包含一个CheckBox和两个TextView

enter image description here

当我选择ListView其他项目的第一项时,也选择了在滚动中显示的项目(未在屏幕上显示并在滚动后显示)。
当我从ListView中删除一些项目时,它可以正常工作。剩余的项目适合移动屏幕。只有在添加滚动时才会出现问题。 我从昨天开始坚持这个问题。

 ListView lv=findViewById(R.id.listOfStudents);
 Cursor cursor = dbHelper.fetchAllData();
 final String[] columns = new String[]{CountriesDbAdapter.KEY_NAME, CountriesDbAdapter.KEY_CONTACT};
 int[] to = new int[]{R.id.name, R.id.phone};
 dataAdapter = new SimpleCursorAdapter(this, R.layout.student_info, cursor, columns, to, 0);
 lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
 lv.setAdapter(dataAdapter);
 lv.setOnItemClickListener(new OnItemClickListener() {
     @Override
     public void onItemClick(AdapterView<?> listView, View view, int position, long id) {
         CheckBox checkBox = (CheckBox) listView.getAdapter().getView(position,view , null).findViewById(R.id.checkBox);
         if (checkBox.isChecked()) {
             checkBox.setChecked(false);
         } else {
             checkBox.setChecked(true);
         }
     }
 });

适配器的代码。 CountriesDbAdapter.java

package com.example.usmanasghar.studentattendancefromdatabase;

/**
 * Created by Usman Asghar on 09/02/2016.
 */

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

    public class CountriesDbAdapter {

        public static final String KEY_ROWID = "_id";
        public static final String KEY_NAME = "name";
        public static final String KEY_CONTACT = "contact";
        public static final String KEY_PRESENT = "Present";
        private static final String TAG = "CountriesDbAdapter";
        private DatabaseHelper mDbHelper;
        private SQLiteDatabase mDb;

        private static final String DATABASE_NAME = "ClassRoom";
        private static final String SQLITE_TABLE = "StudentInfo";
        private static final int DATABASE_VERSION = 1;

        private final Context mCtx;

        private static final String DATABASE_CREATE =
                "CREATE TABLE if not exists " + SQLITE_TABLE + " (" +
                        KEY_ROWID + " integer PRIMARY KEY autoincrement," +
                        KEY_NAME + "," +
                        KEY_CONTACT +
                        ");";


        private static class DatabaseHelper extends SQLiteOpenHelper {

            DatabaseHelper(Context context) {
                super(context, DATABASE_NAME, null, DATABASE_VERSION);
            }


            @Override
            public void onCreate(SQLiteDatabase db) {
                Log.w(TAG, DATABASE_CREATE);
                db.execSQL(DATABASE_CREATE);
            }

            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                        + newVersion + ", which will destroy all old data");
                db.execSQL("DROP TABLE IF EXISTS " + SQLITE_TABLE);
                onCreate(db);
            }
        }

        public CountriesDbAdapter(Context ctx) {
            this.mCtx = ctx;
        }

        public CountriesDbAdapter open() throws SQLException {
            mDbHelper = new DatabaseHelper(mCtx);
            mDb = mDbHelper.getWritableDatabase();
            return this;
        }

        public void close() {
            if (mDbHelper != null) {
                mDbHelper.close();
            }
        }

        public long createCountry(String name,
                                  String contact) {
            ContentValues initialValues = new ContentValues();
            initialValues.put(KEY_NAME, name);
            initialValues.put(KEY_CONTACT, contact);
            return mDb.insert(SQLITE_TABLE, null, initialValues);
        }

        public int deleteStudentWithId(String id) {
            int doneDelete;
            doneDelete = mDb.delete(SQLITE_TABLE, "_id= ?", new String[]{id});//Delete from table where _id=?
            return doneDelete;
        }
       public Cursor fetchContactsWithId(String id){
           Cursor mCursor = null;
           if (id == null || id.length() == 0) {
               mCursor = mDb.query(SQLITE_TABLE, new String[]{KEY_ROWID, KEY_NAME, KEY_CONTACT},
                       null, null, null, null, null);
           } else {
               mCursor = mDb.query(true, SQLITE_TABLE, new String[]{KEY_ROWID, KEY_NAME,KEY_CONTACT},
                       KEY_ROWID + " like '%" + id + "%'", null,
                       null, null, null, null);
           }
           if (mCursor != null) {
               mCursor.moveToFirst();
           }
           return mCursor;
        }
        public void UpdateData(String Name, String PhoneNumber, String id) {
            ContentValues cv = new ContentValues();
            cv.put(KEY_NAME, Name);
            cv.put(KEY_CONTACT, PhoneNumber);
            mDb.update(SQLITE_TABLE, cv, "_id = ?", new String[]{id});
        }

        public Cursor fetchCountriesByName(String inputText) throws SQLException {
            Cursor mCursor = null;
            if (inputText == null || inputText.length() == 0) {
                mCursor = mDb.query(SQLITE_TABLE, new String[]{KEY_ROWID, KEY_NAME, KEY_CONTACT},
                        null, null, null, null, null);
            } else {
                mCursor = mDb.query(true, SQLITE_TABLE, new String[]{KEY_ROWID, KEY_NAME, KEY_CONTACT},
                        KEY_NAME + " like '%" + inputText + "%'", null,
                        null, null, null, null);
            }
            if (mCursor != null) {
                mCursor.moveToFirst();
            }
            return mCursor;

        }

        public Cursor fetchAllData() {
            Cursor mCursor = mDb.query(SQLITE_TABLE, new String[]{KEY_ROWID, KEY_NAME, KEY_CONTACT},
                    null, null, null, null, null);

            if (mCursor != null) {
                mCursor.moveToFirst();
            }
            return mCursor;
        }
        public Cursor fetchOnlyContacts() {
            Cursor mCursor = mDb.query(SQLITE_TABLE, new String[]{ KEY_CONTACT},
                    null, null, null, null, null);

            if (mCursor != null) {
                mCursor.moveToFirst();
            }
            return mCursor;
        }

        public void insertSomeCountries() {
            createCountry("Jordan", "03025800077");
            createCountry("Liza", "03116111474");
            createCountry("Johny", "03321625972");
            createCountry(Xeomia", "03078434574");
            createCountry("Shina", "03400714500");


        }

    }

2 个答案:

答案 0 :(得分:1)

CheckView内部CheckView的问题在于,由于ListView的回收而导致视图被回收,并且未维护Checkbox(检查或取消选中)的值。为了将状态保持为CheckBox,必须存在可以存储Checkbox状态的东西。

@Lalit Poptani ListView with CheckBox Scrolling Issue 撰写博客

答案 1 :(得分:0)

ListView使用先前创建的视图的回收。当ListView创建时,它会创建一个View加一个可以在屏幕上显示的数量,当您滚动屏幕上一个视图通过替换它的内容获得回收时,例如 - 当屏幕上显示5行并向下滚动然后第一行视图时内容替换为6位置行,这是滚动时选择另一个项目的原因。您需要更新特定行的复选框的数据值而不是行的复选框。