0

これは迷惑なことですが、私はこれまでずっとそれをしてきました。 ボタンは値を更新していませんコンテンツURLを使用して、ボタンクリックでカーソルアダプタ内のSQLiteDB(アンドロイド)の行を更新するには?

これはカーソルアダプタクラスです。コードがたくさんあることはわかっていますが、ボタンを宣言した後はバインドビューだけを見る必要があります。それは他の誰かが他のものを実装することができます場合は、私は、全体のコードを与えた -

パブリッククラス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を返します。 コメントの中で、私はそれが何の役目を果たすまで追加しました。助けてください。

答えて

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

注 - - マイコンテンツプロバイダは、全体ではなく、テーブルの1行のみへの書き込みの機能を持っているので、あなたが気づけば、私はURIの末尾にIDを付加している修正とコードの取り付け 私が量を更新するコードで。

関連する問題