0

好吧,這很煩人,但我已經很久了。 該按鈕沒有更新值如何使用內容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); 



     } 
    }); 
} 

}

每次我按一下按鈕,它不更新。我不知道爲什麼。我已經瀏覽了谷歌的所有文檔並刷新了stackoverflow。仍然沒有線索。它始終返回0。 在我已添加的評論,直到它的工作部分。請幫忙。

回答

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在URI結束在我更新數量的代碼中。

相關問題