猿问

如何在列表视图(包含来自 sqlite 数据库的项)中使工作复选框

我正在尝试在列表视图中制作工作复选框。布局只是主布局.xml当新信息添加到 sqlite 数据库时.xml它会显示多行。我可以在main.xml中制作有效的复选框,但我不知道如何使工作复选框在行内.xml。


安卓.java(显示主.xml和行.xml)


     public class AndroidSQLite extends Activity {

     (...)


    checkBoxMain = (CheckBox)findViewById(R.id.checkboxmain1);


    listContent = (ListView)findViewById(R.id.contentlist);


    mySQLiteAdapter = new SQLiteAdapter(this);

    mySQLiteAdapter.openToWrite();


    cursor = mySQLiteAdapter.queueAll();

    String[] from = new String[]{SQLiteAdapter._id, 

    SQLiteAdapter.KEY_NAME,

                            SQLiteAdapter.KEY_QUANTITY, 

    SQLiteAdapter.KEY_CHECKED};

    int[] to = new int[]{R.id.id, R.id.name, R.id.quantity, 

    R.id.checkboxmain};

    cursorAdapter =

            new SimpleCursorAdapter(this, R.layout.row, cursor, from, 

    to);

    listContent.setAdapter(cursorAdapter);


    listContent.setOnItemClickListener(listContentOnItemClickListener);



    buttonAdd.setOnClickListener(buttonAddOnClickListener);


    checkBoxMain.setOnClickListener(onCheckboxClicked);


}

复选框 onclickListener (AndroidSQLite.java)


CheckBox.OnClickListener onCheckboxClicked

        = new CheckBox.OnClickListener() {


    public void onClick(View v) {

        CheckBox checkBoxMain = (CheckBox) 

 findViewById(R.id.checkboxmain1);

        boolean checked = checkBoxMain.isChecked();

        if (checked) {

            Boolean data1 = checkBoxMain.isChecked();

            mySQLiteAdapter.insertChecked(data1);

            updateList();

        }

    }


};

ListView OnItemClickListener (AndroidSQLite.java)


  private ListView.OnItemClickListener listContentOnItemClickListener

        = new ListView.OnItemClickListener(){


    

皈依舞
浏览 116回答 1
1回答

哔哔one

以下是处理 ListView 项中视图的单击事件的一种相对简单的方法。要简单地获取已单击以将其与另一个项目区分开来的实际项目,您需要一个自定义适配器,在本例中为自定义光标适配器,您可以在其中将视图的标记设置为适当的值(id)。同时,您可以使用&nbsp;onClick&nbsp;属性来指定单击视图(复选框)时要调用的方法,该方法在“活动”中定义。下面是基于您的代码的工作示例。它使用更简单,更灵活的方法来管理列表视图,即管理列表视图请注意,为方便起见,您的某些代码已被省略(注释掉)。守则行.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; android:orientation="vertical"&nbsp; &nbsp; android:layout_width="fill_parent"&nbsp; &nbsp; android:layout_height="wrap_content">&nbsp; &nbsp; <LinearLayout&nbsp; &nbsp; &nbsp; &nbsp; android:orientation="horizontal"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="fill_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/layoutmain"&nbsp; &nbsp; &nbsp; &nbsp; >&nbsp; &nbsp; &nbsp; &nbsp; <TextView&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="fill_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:padding="2dip"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:text="M"/>&nbsp; &nbsp; &nbsp; &nbsp; <TextView&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/id"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="fill_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:padding="2dip"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:paddingRight="10dip"/>&nbsp; &nbsp; &nbsp; &nbsp; <TextView&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="fill_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:padding="2dip"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:paddingRight="10dip"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:text="-" />&nbsp; &nbsp; &nbsp; &nbsp; <TextView&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/name"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="fill_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="fill_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:padding="2dip"/>&nbsp; &nbsp; </LinearLayout>&nbsp; &nbsp; <TextView&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/quantity"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="fill_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:padding="2dip"/>&nbsp; &nbsp; <CheckBox&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/checkboxmain2"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:onClick="ListViewCheckBoxHanlder"/></LinearLayout>记下对复选框所做的更改activity_main.xml<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; android:orientation="vertical"&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; >&nbsp; &nbsp; <TextView&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/panelup"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:text="LIST SQ1"&nbsp; &nbsp; &nbsp; &nbsp; />&nbsp; &nbsp; <ListView&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/contentlist"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="fill_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="fill_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_below="@id/panelup"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_above="@id/paneldown"/>&nbsp; &nbsp; <CheckBox&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/checkboxmain1"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content" />&nbsp; &nbsp; <LinearLayout&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/paneldown"&nbsp; &nbsp; &nbsp; &nbsp; android:orientation="horizontal"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_alignParentBottom="true">&nbsp; &nbsp; &nbsp; &nbsp; <EditText&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/name"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_weight="1"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; />&nbsp; &nbsp; &nbsp; &nbsp; <EditText&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/quantity"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_weight="2"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; />&nbsp; &nbsp; &nbsp; &nbsp; <Spinner&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/mu"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:entries="@array/mu_values"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_weight="2"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; />&nbsp; &nbsp; &nbsp; &nbsp; <Button&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/add"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_weight="2"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:text="+"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; />&nbsp; &nbsp; </LinearLayout></RelativeLayout>安卓QLite.javapublic class AndroidSQLite extends AppCompatActivity {&nbsp; &nbsp; CheckBox checkBoxMain;&nbsp; &nbsp; ListView listContent;&nbsp; &nbsp; Button buttonAdd;&nbsp; &nbsp; Cursor cursor;&nbsp; &nbsp; SQLiteAdapter mySQLiteAdapter;&nbsp; &nbsp; //SimpleCursorAdapter cursorAdapter; //<<<<<<<<<< NOT USED ANYMORE&nbsp; &nbsp; MyCursorAdapter myadapter; //<<<<<<<<<< Use a custom adapter that sets the tag of the checkbox to the respective id&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.activity_main);&nbsp; &nbsp; &nbsp; &nbsp; checkBoxMain = (CheckBox)findViewById(R.id.checkboxmain1);&nbsp; &nbsp; &nbsp; &nbsp; listContent = (ListView)findViewById(R.id.contentlist);&nbsp; &nbsp; &nbsp; &nbsp; mySQLiteAdapter = new SQLiteAdapter(this);&nbsp; &nbsp; &nbsp; &nbsp; mySQLiteAdapter.openToWrite();&nbsp; &nbsp; &nbsp; &nbsp; manageListView(); //<<<<<<<<<< ADDED&nbsp; &nbsp; &nbsp; &nbsp; /* !!!!!!!!! COMMENTED OUT&nbsp; &nbsp; &nbsp; &nbsp; cursor = mySQLiteAdapter.queueAll();&nbsp; &nbsp; &nbsp; &nbsp; String[] from = new String[]{SQLiteAdapter._id,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SQLiteAdapter.KEY_NAME,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SQLiteAdapter.KEY_QUANTITY,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SQLiteAdapter.KEY_CHECKED};&nbsp; &nbsp; &nbsp; &nbsp; int[] to = new int[]{R.id.id, R.id.name, R.id.quantity,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; R.id.checkboxmain2};&nbsp; &nbsp; &nbsp; &nbsp; cursorAdapter =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new SimpleCursorAdapter(this, R.layout.row, cursor, from,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; to,0);&nbsp; &nbsp; &nbsp; &nbsp; listContent.setAdapter(cursorAdapter);&nbsp; &nbsp; &nbsp; &nbsp; listContent.setOnItemClickListener(listContentOnItemClickListener);&nbsp; &nbsp; &nbsp; &nbsp; */&nbsp; &nbsp; &nbsp; &nbsp; //buttonAdd.setOnClickListener(buttonAddOnClickListener); //<<<<<<<<<<< you want this back in&nbsp; &nbsp; }&nbsp; &nbsp; //<<<<<<<<<< ADDED >>>>>>>>>>&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onResume() {&nbsp; &nbsp; &nbsp; &nbsp; super.onResume();&nbsp; &nbsp; &nbsp; &nbsp; manageListView(); //Refresh the List when resuming e.g. returning from another activity&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onDestroy() {&nbsp; &nbsp; &nbsp; &nbsp; // TODO Auto-generated method stub&nbsp; &nbsp; &nbsp; &nbsp; super.onDestroy();&nbsp; &nbsp; &nbsp; &nbsp; cursor.close(); //<<<<<<<<<< SHOULD ALWAYS CLOSE CURSOR&nbsp; &nbsp; &nbsp; &nbsp; mySQLiteAdapter.close();&nbsp; &nbsp; }&nbsp; &nbsp; //<<<<<<<<<< NO LONGER USED >>>>>>>>>>&nbsp; &nbsp; private void updateList(){&nbsp; &nbsp; &nbsp; &nbsp; cursor = mySQLiteAdapter.queueAll();&nbsp; &nbsp; &nbsp; &nbsp; myadapter.swapCursor(cursor);&nbsp; &nbsp; }&nbsp; &nbsp; //<<<<<<<< NOTE NOT USED but you'd want this to be used&nbsp; &nbsp; CheckBox.OnClickListener onCheckboxClicked&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = new CheckBox.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CheckBox checkBoxMain = (CheckBox)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; findViewById(R.id.checkboxmain1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; boolean checked = checkBoxMain.isChecked();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (checked) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Boolean data1 = checkBoxMain.isChecked();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mySQLiteAdapter.insertChecked(data1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; manageListView(); //<<<<<<<<<< refresh the ListView&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };&nbsp; &nbsp; //<<<<<<<<<<NOT USED>>>>>>>>>>&nbsp; &nbsp; private ListView.OnItemClickListener listContentOnItemClickListener&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = new ListView.OnItemClickListener(){&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onItemClick(AdapterView<?> parent, View view, int&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; position,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; long id) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Cursor cursor = (Cursor) parent.getItemAtPosition(position);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final int item_id =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cursor.getInt(cursor.getColumnIndex(SQLiteAdapter._id));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String item_name =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_NAME));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String item_quantity =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_QUANTITY));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AlertDialog.Builder myDialog&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = new AlertDialog.Builder(AndroidSQLite.this);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // when item in row.xml is clicked alertdialog is shown&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // code of AlertDialog&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myDialog.show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; updateList();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };&nbsp; &nbsp; /**<<<<<<<<<< ADDED >>>>>>>>>>>&nbsp; &nbsp; &nbsp;* Manage the ListView building from new or refreshing the data&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private void manageListView() {&nbsp; &nbsp; &nbsp; &nbsp; cursor = mySQLiteAdapter.queueAll(); // get the source data (cursor) for the listview&nbsp; &nbsp; &nbsp; &nbsp; if (myadapter == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myadapter = new MyCursorAdapter(this,cursor);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listContent.setAdapter(myadapter);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myadapter.swapCursor(cursor);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; /**<<<<<<<<<< ADDED >>>>>>>>>>&nbsp; &nbsp; &nbsp;* Handle the CheckBox being clicked,&nbsp; &nbsp; &nbsp;* NOTE set in the layout&nbsp; &nbsp; &nbsp;* @param v&nbsp; &nbsp; &nbsp;The View&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public void ListViewCheckBoxHanlder(View v) {&nbsp; &nbsp; &nbsp; &nbsp; CheckBox cb = v.findViewById(R.id.checkboxmain2);&nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(this, "You clicked the CheckBox for ID " + (String) cb.getTag(), Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; int checked = 0;&nbsp; &nbsp; &nbsp; &nbsp; if (cb.isChecked()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checked = 1;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; long id = Long.valueOf((String) cb.getTag());&nbsp; &nbsp; &nbsp; &nbsp; mySQLiteAdapter.updateChecked(id,checked);&nbsp; &nbsp; &nbsp; &nbsp; manageListView();&nbsp; &nbsp; }}请注意,为了方便起见,一些被注释掉的代码已被替换掉,一些代码已被注释掉。我的库索适配器.javapublic class MyCursorAdapter extends CursorAdapter {&nbsp; &nbsp; public MyCursorAdapter(Context context, Cursor c) {&nbsp; &nbsp; &nbsp; &nbsp; super(context, c, true);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public View getView(int position, View convertView, ViewGroup parent) {&nbsp; &nbsp; &nbsp; &nbsp; View v = super.getView(position, convertView, parent);&nbsp; &nbsp; &nbsp; &nbsp; return v;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public View newView(Context context, Cursor cursor, ViewGroup parent) {&nbsp; &nbsp; &nbsp; &nbsp; return LayoutInflater.from(context).inflate(R.layout.row,parent,false);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void bindView(View view, Context context, Cursor cursor) {&nbsp; &nbsp; &nbsp; &nbsp; //Note Cursor will be positioned appropriately&nbsp; &nbsp; &nbsp; &nbsp; TextView name = (TextView) view.findViewById(R.id.name);&nbsp; &nbsp; &nbsp; &nbsp; TextView id = (TextView) view.findViewById(R.id.id);&nbsp; &nbsp; &nbsp; &nbsp; TextView quantity = (TextView) view.findViewById(R.id.quantity);&nbsp; &nbsp; &nbsp; &nbsp; CheckBox cb = (CheckBox) view.findViewById(R.id.checkboxmain2);&nbsp; &nbsp; &nbsp; &nbsp; name.setText(cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_NAME)));&nbsp; &nbsp; &nbsp; &nbsp; id.setText(cursor.getString(cursor.getColumnIndex(SQLiteAdapter._id)));&nbsp; &nbsp; &nbsp; &nbsp; quantity.setText(cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_QUANTITY)));&nbsp; &nbsp; &nbsp; &nbsp; cb.setChecked(cursor.getInt(cursor.getColumnIndex(SQLiteAdapter.KEY_CHECKED)) > 0);&nbsp; &nbsp; &nbsp; &nbsp; cb.setTag(cursor.getString(cursor.getColumnIndex(SQLiteAdapter._id))); //<<<<<<<<<< SET TAG to the ID&nbsp; &nbsp; }}&nbsp;请注意,以上是一个新类。SQliteadapter.javapublic class SQLiteAdapter {&nbsp; &nbsp; SQLiteDatabase sqLiteDatabase;&nbsp; &nbsp; SQLiteHelper sqLiteHelper;&nbsp; &nbsp; Context context;&nbsp; &nbsp; public static final String KEY_CHECKED = "checked";&nbsp; &nbsp; public static final String _id = BaseColumns._ID;&nbsp; &nbsp; public static final String KEY_NAME = "name";&nbsp; &nbsp; public static final String KEY_QUANTITY = "quantity";&nbsp; &nbsp; public static final String KEY_PRICE = "price";&nbsp; &nbsp; public static final String KEY_MU = "mu";&nbsp; &nbsp; public static final String KEY_PDATE = "pdate";&nbsp; &nbsp; public static final String KEY_SHOP = "shop";&nbsp; &nbsp; public SQLiteAdapter(Context context) {&nbsp; &nbsp; &nbsp; &nbsp; this.context = context;&nbsp; &nbsp; &nbsp; &nbsp; openToWrite();&nbsp; &nbsp; }&nbsp; &nbsp; public SQLiteAdapter openToWrite() throws android.database.SQLException {&nbsp; &nbsp; &nbsp; &nbsp; sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MYDATABASE_VERSION);&nbsp; &nbsp; &nbsp; &nbsp; sqLiteDatabase = sqLiteHelper.getWritableDatabase();&nbsp; &nbsp; &nbsp; &nbsp; return this;&nbsp; &nbsp; }&nbsp; &nbsp; public void close() {&nbsp; &nbsp; &nbsp; &nbsp; sqLiteHelper.close();&nbsp; &nbsp; }&nbsp; &nbsp; public long insertChecked(boolean data1) {&nbsp; &nbsp; &nbsp; &nbsp; ContentValues contentValues = new ContentValues();&nbsp; &nbsp; &nbsp; &nbsp; contentValues.put(KEY_CHECKED, data1);&nbsp; &nbsp; &nbsp; &nbsp; return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);&nbsp; &nbsp; }&nbsp; &nbsp; public int updateChecked(long id,int check) {&nbsp; &nbsp; &nbsp; &nbsp; ContentValues cv = new ContentValues();&nbsp; &nbsp; &nbsp; &nbsp; cv.put(KEY_CHECKED,check);&nbsp; &nbsp; &nbsp; &nbsp; String whereclause = _id + "=?";&nbsp; &nbsp; &nbsp; &nbsp; String[] whereargs = new String[]{String.valueOf(id)};&nbsp; &nbsp; &nbsp; &nbsp; return sqLiteDatabase.update(MYDATABASE_TABLE,cv,whereclause,whereargs);&nbsp; &nbsp; }&nbsp; &nbsp; public Cursor queueAll() {&nbsp; &nbsp; &nbsp; &nbsp; String[] columns = new String[]{_id, KEY_NAME, KEY_PRICE,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; KEY_QUANTITY, KEY_MU,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; KEY_PDATE, KEY_SHOP, KEY_CHECKED};&nbsp; &nbsp; &nbsp; &nbsp; Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; null, null, null, null, null);&nbsp; &nbsp; &nbsp; &nbsp; return cursor;&nbsp; &nbsp; }}请注意,这很可能是不同的,它是为满足测试而创建的。.javapublic class SQLiteHelper extends SQLiteOpenHelper {&nbsp; &nbsp; public static final String MYDATABASE_NAME = "mydatabase";&nbsp; &nbsp; public static final int&nbsp; MYDATABASE_VERSION = 1;&nbsp; &nbsp; public static final String MYDATABASE_TABLE = "mytable";&nbsp; &nbsp; SQLiteDatabase mDB;&nbsp; &nbsp; public SQLiteHelper(Context context, String name, SQLiteDatabase.CursorFactory factory,int version) {&nbsp; &nbsp; &nbsp; &nbsp; super(context, name, factory, version);&nbsp; &nbsp; &nbsp; &nbsp; mDB = this.getWritableDatabase();&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onCreate(SQLiteDatabase db) {&nbsp; &nbsp; &nbsp; &nbsp; String crt_tbl_sql = "CREATE TABLE IF NOT EXISTS " + MYDATABASE_TABLE + "(" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SQLiteAdapter._id + " INTEGER PRIMARY KEY, " +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SQLiteAdapter.KEY_NAME + " TEXT, " +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SQLiteAdapter.KEY_SHOP + " TEXT, " +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SQLiteAdapter.KEY_PDATE + " TEXT, " +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SQLiteAdapter.KEY_PRICE + " REAL, " +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SQLiteAdapter.KEY_QUANTITY + " INTEGER, " +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SQLiteAdapter.KEY_MU + " TEXT, " +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SQLiteAdapter.KEY_CHECKED + " INTEGER DEFAULT 0" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ")";&nbsp; &nbsp; &nbsp; &nbsp; db.execSQL(crt_tbl_sql);&nbsp; &nbsp; &nbsp; &nbsp; addSomeTestingData(db,10);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {&nbsp; &nbsp; }&nbsp; &nbsp; private long addRow(String name, String shop, String pdate, double price, int quantity, String mu, SQLiteDatabase db) {&nbsp; &nbsp; &nbsp; &nbsp; ContentValues cv = new ContentValues();&nbsp; &nbsp; &nbsp; &nbsp; cv.put(SQLiteAdapter.KEY_NAME,name);&nbsp; &nbsp; &nbsp; &nbsp; cv.put(SQLiteAdapter.KEY_SHOP,shop);&nbsp; &nbsp; &nbsp; &nbsp; cv.put(SQLiteAdapter.KEY_PDATE,pdate);&nbsp; &nbsp; &nbsp; &nbsp; cv.put(SQLiteAdapter.KEY_PRICE,price);&nbsp; &nbsp; &nbsp; &nbsp; cv.put(SQLiteAdapter.KEY_QUANTITY,quantity);&nbsp; &nbsp; &nbsp; &nbsp; cv.put(SQLiteAdapter.KEY_MU,mu);&nbsp; &nbsp; &nbsp; &nbsp; return db.insert(MYDATABASE_TABLE,null,cv);&nbsp; &nbsp; }&nbsp; &nbsp; private void addSomeTestingData(SQLiteDatabase db, int number_to_add) {&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < number_to_add;i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String suffix = String.valueOf(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String day_in_month = suffix;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i < 10) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; day_in_month = "0" + day_in_month;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; addRow(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Test" + suffix,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Shop" + suffix,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "2019-01-" + day_in_month,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 10.5 + new Double(i * 3),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i * 4,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "mu" + suffix,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; db&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}请注意,这很可能是不同的,它是为满足测试而创建的。结果以下是单击多个复选框后拍摄的屏幕截图,因此将显示 Toast:-以及重新启动应用程序后的屏幕截图(显示状态已保留,即根据复选框更新数据库):-
随时随地看视频慕课网APP

相关分类

Java
我要回答