【问题标题】:How to update rows in SQLiteDB(android) inside a cursor adapter on button click, using content uri?如何使用内容uri在单击按钮时更新光标适配器内的SQLiteDB(android)中的行?
【发布时间】:2017-04-18 18:42:52
【问题描述】:

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

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

`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);



        }
    });
}

}

每次我点击按钮时,它都不会更新。我不知道为什么。我浏览了谷歌的所有文档并搜索了stackoverflow。还是没有头绪。它始终返回 0。 在我添加的 cmets 中,直到它起作用的部分。请帮忙。

【问题讨论】:

    标签: android android-sqlite android-adapter android-cursoradapter


    【解决方案1】:

    想通了。如果您注意到我使用 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);
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多