如何使用内容URI在按钮上单击“光标适配器”中的sqlitedb(android)中的行更新行



好吧,这很烦人,但是我已经很久了。该按钮没有更新值

这是光标适配器类,我知道这是很多代码,但是在我声明按钮后,您只需要查看绑定视图即可。我给了整个代码,以防其他人实施其他事情 -

`公共类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的所有文档,并搜寻了Stackoverflow。仍然不知道。它一直返回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);

注意 - 我的内容提供商具有仅写入一行而不是整个表的功能,因此,如果您注意到我已经在我更新数量的代码末端附加了ID。

最新更新