如何在单击按钮时使用内容 uri 更新游标适配器内的 SQLiteDB(android)中的行?

How to update rows in SQLiteDB(android) inside a cursor adapter on button click, using content uri?

好吧,这很烦人,但我已经用了很长时间了。 该按钮未更新值

这是光标适配器class,我知道它有很多代码,但您只需要在我声明按钮后查看绑定视图。我提供了完整的代码,以防它帮助其他人实现其他东西 -

`public class FruitsFragmentCursorAdapter 扩展了 CursorAdapter {

public FruitsFragmentCursorAdapter(Context context, Cursor cursor) {
    super(context,cursor,0);

}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return LayoutInflater.from(context).inflate(R.layout.fragment_fruits,parent,false);
}

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

    //final

    //First find all the views that I want to modify individually
    ImageView imageView = (ImageView) view.findViewById(R.id.fruit_fragment_fruit_image);
    TextView engTextView = (TextView) view.findViewById(R.id.fruit_fragment_english_name);
    TextView hindiTextView = (TextView) view.findViewById(R.id.fruit_fragment_hindi_name);
    TextView measureTextView = (TextView) view.findViewById(R.id.fruit_fragment_unit_measure);
    TextView priceTextView = (TextView) view.findViewById(R.id.fruit_fragment_unit_price);
    final TextView quantityTextView = (TextView) view.findViewById(R.id.fruit_fragment_quantity_text_view);
    TextView priceCalTextView = (TextView) view.findViewById(R.id.fruit_fragment_price_calculation);

    //Find the columns of the attributes we are interested in
    int columnImage = cursor.getColumnIndex(itemsSoldContractEntry.COLUMN_IMAGE);
    int columnEngName = cursor.getColumnIndex(itemsSoldContractEntry.COLUMN_ITEM_NAME_ENGLISH);
    int columnHinName = cursor.getColumnIndex(itemsSoldContractEntry.COLUMN_ITEM_NAME_HINDI);
    int columnMeasure = cursor.getColumnIndex(itemsSoldContractEntry.COLUMN_MEASURE);
    int columnPrice = cursor.getColumnIndex(itemsSoldContractEntry.COLUMN_UNIT_PRICE);
    final int columnQuantity = cursor.getColumnIndex(itemsSoldContractEntry.COLUMN_QUANTITY);
    final int columnItemSoldID = cursor.getColumnIndex(itemsSoldContractEntry.COLUMN_ITEM_ID);
    final int columnID = cursor.getColumnIndex(itemsSoldContractEntry._ID);

    //Read the attributes from the cursor
    final String image = cursor.getString(columnImage);
    final String engName = cursor.getString(columnEngName);
    String hinName = cursor.getString(columnHinName);
    String measure = cursor.getString(columnMeasure);
    String price = cursor.getString(columnPrice);
    String quantity = cursor.getString(columnQuantity);

    //get the string for the cal text view separately
    String calculation = quantity + " x "+price + " = " + Integer.parseInt(quantity)*Integer.parseInt(price);

    //Decode the string to create a bitmap
    byte[] decodedString = Base64.decode(image,Base64.DEFAULT);
    Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString,0,decodedString.length);

    //Update the text views with the values
    imageView.setImageBitmap(decodedByte);
    engTextView.setText(engName);
    hindiTextView.setText(hinName);
    measureTextView.setText("per "+measure);
    priceTextView.setText("₹ " + price);
    quantityTextView.setText(quantity);
    priceCalTextView.setText(calculation);

    //Define the two buttons (increment and decrement)
    Button incrementsButton = (Button) view.findViewById(R.id.fruit_fragment_increment);

    //Get the position of the cursor
    final int position = cursor.getPosition();

    //Set the onclick listener
    incrementsButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //Set up a content values object to hold the quantity when updated
            ContentValues incrementValue = new ContentValues();

            //Move the cursor to the position of the current item under operation
            cursor.moveToPosition(position);
            //Update the quantity value
            int oldQuantity = (cursor.getInt(columnQuantity));
            int newQuantity = oldQuantity +1;
            //Works till here
            //Put the value in the content values
            incrementValue.put(itemsSoldContractEntry.COLUMN_QUANTITY,newQuantity);
            //Selection claus which will point to the item_sold_id which will be updated
            String selection = itemsSoldContractEntry._ID + "=?";
            //Get the item id which should be updated
            int item_id = cursor.getInt(columnID);
            String itemIDArgs = Integer.toString(item_id);
            //Works till here

            //Selection args claus
            String[] selectionArgs = {itemIDArgs};
            //Update the value
            int something = context.getContentResolver().update(itemsSoldContractEntry.CONTENT_URI_ITEMS_SOLD,incrementValue,selection,selectionArgs);
            Log.v("Updated"," Row"+ something);


            //This is a toast to check if the correct item is being clicked
            Toast.makeText(context,something+"",Toast.LENGTH_SHORT).show();

            //New quantity
            String newQu = cursor.getString(columnQuantity);

            quantityTextView.setText(newQu);



        }
    });
}

}

每次我点击按钮,它都不会更新。我不知道为什么。我浏览了 google 的所有文档并搜索了 Whosebug。仍然没有头绪。它一直 returns 0。 在评论中,我添加了直到它起作用的部分。请帮忙

想通了。如果您注意到我使用 URI 访问数据库。我意识到 URI 未配置为处理更新,因此返回 0(更新的行数)。一旦修复它就非常简单了。 附上修复代码 -

 //Move the cursor to the position of the current item under operation
            cursor.moveToPosition(position);
            //Update the quantity value
            int oldQuantity = (cursor.getInt(columnQuantity));
            int newQuantity = oldQuantity +1;
            //Works till here
            //Put the value in the content values
            incrementValue.put(itemsSoldContractEntry.COLUMN_QUANTITY,newQuantity);
            //Selection claus which will point to the item_sold_id which will be updated
            String selection = itemsSoldContractEntry._ID + "=?";
            //Get the item id which should be updated
            int item_id = cursor.getInt(columnID);
            String itemIDArgs = Integer.toString(item_id);
            //This is a toast to check if the correct item is being clicked
            Toast.makeText(context,itemIDArgs+"",Toast.LENGTH_SHORT).show();
            //Works till here

            //Selection args claus
            String[] selectionArgs = {itemIDArgs};
            //Update the value

            int something = context.getContentResolver().update(
                    Uri.withAppendedPath(itemsSoldContractEntry.CONTENT_URI_ITEMS_SOLD,Integer.toString(item_id)),
                    incrementValue,
                    selection,selectionArgs);

注意 - 我的内容提供者具有仅写入一行而不是整个 table 的功能,因此如果您注意到我在我更新的代码中的 URI 末尾附加了 id数量。