如何修复使用#2nd 记录而不是#1st(#3rd 而不是#2nd 等)的游标?

布局只是 main.xml,它在将新信息添加到 sqlite 数据库时显示多个 row.xml。单击按钮时,将显示带有数据的 AlertDialog。问题是,当我单击行中的按钮时,AlertDialog 正在从数据库中的下一条记录(下一行)获取数据(当我单击第一个按钮时,AD 正在获取第二条记录等),而不是从按钮所在的位置获取数据。我为按钮和 toast 消息设置标签,单击它时显示良好的 ID。


MyCursorAdapter.java


(...)


@Override

public void bindView(View view, Context context, Cursor cursor) {


TextView name = (TextView) view.findViewById(R.id.name);

TextView id = (TextView) view.findViewById(R.id.id);

TextView quantity = (TextView) view.findViewById(R.id.quantity);

TextView mu = (TextView) view.findViewById(R.id.mu);

Button bt = (Button) view.findViewById(R.id.rowalertdialog);

CheckBox cb = (CheckBox)view.findViewById(R.id.checkboxmain2);


name.setText(cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_NAME)));

id.setText(cursor.getString(cursor.getColumnIndex(SQLiteAdapter._id)));

quantity.setText(cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_QUANTITY)));

mu.setText(cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_MU)));

bt.setTag(cursor.getString(cursor.getColumnIndex(SQLiteAdapter._id)));

cb.setChecked(cursor.getInt(cursor.getColumnIndex(SQLiteAdapter.KEY_CHECKED)) > 0);

cb.setTag(cursor.getString(cursor.getColumnIndex(SQLiteAdapter._id)));

}

安卓SQLite.java


(...)


private void manageListView() {

cursor = mySQLiteAdapter.queueAll();

if(myadapter == null){

    myadapter = new MyCursorAdapter(this,cursor);

    listContent.setAdapter(myadapter);

}else {

    myadapter.swapCursor(cursor);

}

}


HUH函数
浏览 85回答 2
2回答

PIPIONE

工作示例这是一个基于您之前的问题和这个问题的工作示例应用程序。然而,按钮单击处理,在不使用布局的onCLick(如 XML 中编码)的情况下进行处理,但在自定义CursorAdapter&nbsp;(&nbsp;MyCursorAdapter.java ) 的bindView方法中设置。它演示了 3 种获取与按钮关联的 id 的方法item_id_fromcursor显示了如何从作为列表源的 Cursor 获取值(在本例中为该 id),该列表的位置应适当。item_id_fromtag展示了如何从标签中获取 id(有些人不赞成这种技术)。item_id_fromview展示了如何根据传递给按钮的视图从视图层次结构中获取值。该处理允许删除和更新行(尽管更新非常基本,只是附加一个值而不是使用自定义对话框布局(您似乎掌握了这方面的知识))。与前面的答案不同,还实现了处理添加的行(尽管值有限)。生成的应用程序看起来像:-A表示行已被删除(即 id 为 5-7 的行不存在)B显示已更新的行(即已附加更新(如果更新更新则该行已更新两次))C显示已添加的行。如果单击编辑或删除按钮,则会出现对话框,例如:-单击取消从对话框中返回,什么也不做。单击删除将删除相应的行(如对话框消息中所述)。单击 EDIT 通过附加更新的值来编辑名称(编辑已经编辑的行将添加进一步更新的行)显然这可能不是所需的操作,它只是为了演示。编码activity_main.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="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; <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:inputType="number"&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></LinearLayout>行.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:visibility="gone"/>&nbsp; &nbsp; <Button&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/editordelete"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:text="EDIT or DELETE"/></LinearLayout>注意 CheckBox 已隐藏SQLiteHelper.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 onConfigure(SQLiteDatabase db) {&nbsp; &nbsp; &nbsp; &nbsp; super.onConfigure(db);&nbsp; &nbsp; &nbsp; &nbsp; db.setForeignKeyConstraintsEnabled(true);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {&nbsp; &nbsp; }&nbsp; &nbsp; public 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; }}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; }&nbsp; &nbsp; public Cursor queueOneById(long id) {&nbsp; &nbsp; &nbsp; &nbsp; String whereclause = _id + "=?";&nbsp; &nbsp; &nbsp; &nbsp; String[] whereargs = new String[]{String.valueOf(id)};&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; return sqLiteDatabase.query(MYDATABASE_TABLE,columns,whereclause,whereargs,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; null,null,null);&nbsp; &nbsp; }&nbsp; &nbsp; public int delete(long id) {&nbsp; &nbsp; &nbsp; &nbsp; String whereclause = SQLiteAdapter._id + "=?";&nbsp; &nbsp; &nbsp; &nbsp; String[] wherargs = new String[]{String.valueOf(id)};&nbsp; &nbsp; &nbsp; &nbsp; return sqLiteDatabase.delete(MYDATABASE_TABLE,whereclause,wherargs);&nbsp; &nbsp; }&nbsp; &nbsp; public long updateById(long id, String column_to_change, String newvalue) {&nbsp; &nbsp; &nbsp; &nbsp; ContentValues cv = new ContentValues();&nbsp; &nbsp; &nbsp; &nbsp; cv.put(column_to_change,newvalue);&nbsp; &nbsp; &nbsp; &nbsp; String whereclause = SQLiteAdapter._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; }}增加了一些方法MyCursorAdapter.javapublic class MyCursorAdapter extends CursorAdapter {&nbsp; &nbsp; SQLiteAdapter sqliteAdapter;&nbsp; &nbsp; MyCursorAdapter thisCursorAdapter;&nbsp; &nbsp; public MyCursorAdapter(Context context, Cursor c) {&nbsp; &nbsp; &nbsp; &nbsp; super(context, c, true);&nbsp; &nbsp; &nbsp; &nbsp; sqliteAdapter = new SQLiteAdapter(context);&nbsp; &nbsp; &nbsp; &nbsp; thisCursorAdapter = this;&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, final 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; Button eod = (Button) view.findViewById(R.id.editordelete);&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; &nbsp; eod.setTag(cursor.getString(cursor.getColumnIndex(SQLiteAdapter._id)));&nbsp; &nbsp; &nbsp; &nbsp; // dynamically add the listeners as opposed to coding onCLick in layout XML&nbsp; &nbsp; &nbsp; &nbsp; eod.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // get the id from the id TextView, within the view hierarchy rather than from the buttons tag&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // NOTE assumes the Button's parent is a LinearLayout (for simplicity)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // This in theory is the recommended way rather than setting the tag&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LinearLayout ll = (LinearLayout) v.getParent();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TextView id = ll.findViewById(R.id.id), name = ll.findViewById(R.id.name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final long item_id_fromview = Long.valueOf(id.getText().toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final String item_name = name.getText().toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // get the id from the tag&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; long item_id_fromtag = Long.valueOf(v.getTag().toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // get the if from the cursor that is the source of the Listview, it should be positioned accordingly&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; long item_id_fromcursor = cursor.getLong(cursor.getColumnIndex(SQLiteAdapter._id));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Show both&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(v.getContext(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "The id (from the view hierarchy) is " + String.valueOf(item_id_fromview) +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; " or (from the tag) is " + String.valueOf(item_id_fromtag) +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; " or (from the cursor) is" + String.valueOf(item_id_fromcursor)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AlertDialog.Builder mydialog = new AlertDialog.Builder(v.getContext());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mydialog.setMessage("EDIT or DELETE Row:- ID: " + String.valueOf(item_id_fromview) + "Name: " + item_name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mydialog.setPositiveButton("DELETE", new DialogInterface.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(DialogInterface dialog, int which) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sqliteAdapter.delete(item_id_fromview);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; refreshList();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mydialog.setNeutralButton("EDIT", new DialogInterface.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(DialogInterface dialog, int which) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sqliteAdapter.updateById(item_id_fromview,SQLiteAdapter.KEY_NAME,item_name + " Updated");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; refreshList();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mydialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(DialogInterface dialog, int which) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mydialog.show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; private void refreshList() {&nbsp; &nbsp; &nbsp; &nbsp; thisCursorAdapter.swapCursor(sqliteAdapter.queueAll());&nbsp; &nbsp; &nbsp; &nbsp; thisCursorAdapter.notifyDataSetChanged();&nbsp; &nbsp; }}查看绑定视图时如何设置 onCLickListener查看获取id的 3 种不同方法请参阅类似于 AndroidSQLite 活动中使用的 ManageListView 方法的 refreshList 方法。AndroidSQlite.java(活动)public class AndroidSQLite extends AppCompatActivity {&nbsp; &nbsp; ListView listContent;&nbsp; &nbsp; Button buttonAdd;&nbsp; &nbsp; Cursor cursor;&nbsp; &nbsp; SQLiteAdapter mySQLiteAdapter;&nbsp; &nbsp; EditText name, quantity;&nbsp; &nbsp; Spinner mu;&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; listContent = (ListView) findViewById(R.id.contentlist);&nbsp; &nbsp; &nbsp; &nbsp; name = (EditText) findViewById(R.id.name);&nbsp; &nbsp; &nbsp; &nbsp; quantity = (EditText) findViewById(R.id.quantity);&nbsp; &nbsp; &nbsp; &nbsp; buttonAdd = (Button) findViewById(R.id.add);&nbsp; &nbsp; &nbsp; &nbsp; mu = (Spinner) findViewById(R.id.mu);&nbsp; &nbsp; &nbsp; &nbsp; handleAddButton();&nbsp; &nbsp; &nbsp; &nbsp; mySQLiteAdapter = new SQLiteAdapter(this);&nbsp; &nbsp; &nbsp; &nbsp; mySQLiteAdapter.openToWrite();&nbsp; &nbsp; &nbsp; &nbsp; manageListView(); //<<<<<<<<<< ADDED&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; private void handleAddButton() {&nbsp; &nbsp; &nbsp; &nbsp; buttonAdd.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((name.getText().toString()).length() < 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(v.getContext(),"Name cannot be empty",Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name.requestFocus();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((quantity.getText().toString()).length() < 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(v.getContext(),"Quantity cannot be empty",Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; quantity.requestFocus();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mySQLiteAdapter.sqLiteHelper.addRow(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name.getText().toString(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Arbritary values for testing&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "2019-01-01",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "The Shop",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 100.33,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Integer.valueOf(quantity.getText().toString()),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mu.getSelectedItem().toString(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mySQLiteAdapter.sqLiteDatabase&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; manageListView();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &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; }}

慕工程0101907

Cursor cursor = (Cursor) parent.getItemAtPosition(id); 将根据它在列表中的位置获取项目,但您使用的是 id(从按钮的标签中提取)作为它的位置。这将很少是正确的。列表中的第一个位置是 0,第一个 id 将是 1(假设没有删除任何行并且您没有手动设置 id),因此您报告的症状。如果行已被删除,那么每个缺失的 id 都会增加 id 和 position 之间的差异。如果行按 id 以外的任何其他顺序排序,则位置 v id 可能会出路。如果 where 子句不包括行,那么 id 和位置也会有所不同。简单的事实是您不能依赖 id 和列表中的位置之间的任何关系。您可以做的是使用标签中的 id 来查询数据库以提取特定行。该查询将与用于获取列表光标的查询相同,只是它将使用第三个 ( SQLiteAdapter._id + "=?") 和第四个参数 ( new String[]{String.valueOf(id)}) (如果使用查询便利方法)。注释已更正(缺少S添加到 QLiteAdapter),因为评论。已修改我尝试添加我在上一条评论中写的代码,但这是错误的。我不知道如何实施您的更改。–您的代码可能是:-public void ListViewButtonHandler(View v){&nbsp; &nbsp; Button bt = v.findViewById(R.id.rowalertdialog);&nbsp; &nbsp; Toast.makeText(this, "You clicked the Button for ID " + (String)bt.getTag(), Toast.LENGTH_SHORT).show(); //TODO not needed&nbsp; &nbsp; int id = Integer.valueOf((String) bt.getTag());&nbsp; &nbsp; ListView parent = (ListView)findViewById(R.id.contentlist); //<<<<<<<<<< NOT NEEDED (I think)&nbsp; &nbsp; String whereclause = SQLiteAdapter._id _ "=?"; //<<<<<<<<<< ADDED&nbsp; &nbsp; String[] whereargs = new String[]{bt.getTag()}; //<<<<<<<<<< ADDED&nbsp; &nbsp; Cursor cursor = mySQLiteAdapter.getWitableDatabase().query(SQLiteAdapter.MYDATABASE_TABLE,null,whereclause,whereargs,null,null,null); //<<<<<<<<<< ADDED&nbsp; &nbsp; //Cursor cursor = (Cursor) parent.getItemAtPosition(id); //<<<<<<<<<< REMOVED/COMMENTED OUT&nbsp; &nbsp; //<<<<<<<<<<< ADDED to move to the extracted row, will toast if no such row&nbsp; &nbsp; if (!cursor.moveToFirst) {&nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(this, "Unable to retrieve row for ID " + (String)bt.getTag(), Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; final int item_id = cursor.getInt(cursor.getColumnIndex(SQLiteAdapter._id));&nbsp; &nbsp; String item_name = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_NAME));&nbsp; &nbsp; String item_quantity = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_QUANTITY))&nbsp; &nbsp; cursor.close(); //<<<<<<<<<< ADDED should always close a cursor when done with it&nbsp; &nbsp; .......... the rest of the code请注意评论请注意,您可能需要将SQLiteAdapter.MYDATABASE_TABLE更改为适当的值。注意上面的代码是in-principal code,它没有经过测试或运行,因此可能有一些错误选择如果您将以下方法(基于上一个问题)添加到 SQLiteAdapter.java :-public Cursor queueOneById(long id) {&nbsp; &nbsp; String whereclause = _id + "=?";&nbsp; &nbsp; String[] whereargs = new String[]{String.valueOf(id)};&nbsp;&nbsp; &nbsp; String[] columns = new String[]{_id, KEY_NAME, KEY_PRICE,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; KEY_QUANTITY, KEY_MU,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; KEY_PDATE, KEY_SHOP, KEY_CHECKED};&nbsp; &nbsp; return sqLiteDatabase.query(MYDATABASE_TABLE,columns,whereclause,whereargs,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; null,null,null);}然后你可以改变&nbsp; &nbsp; Cursor cursor = mySQLiteAdapter.getWitableDatabase().query(SQLiteAdapter.MYDATABASE_TABLE,null,whereclause,whereargs,null,null,null);至&nbsp; &nbsp; Cursor cursor = mySQLiteAdapter.queueOneById(id);请注意,这仍然需要上述其他更改。那就是你需要:-if (!cursor.moveToFirst) { Toast.makeText(this, "无法检索 ID 行" + (String)bt.getTag(), Toast.LENGTH_SHORT).show(); 返回; }没有上面的代码。光标将位于位置-1(在第一行之前)。所以cursor.moveToFirst需要定位到提取的行(应该只有一行)。如果没有行,则 moveToFirst 将返回 false(无法完成移动)并且将发出 Toast,然后 onClick 方法将返回。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java