좋아, 이건 짜증나.하지만 지금은 오래 있었어. 버튼이 값을 업데이트하지 않습니다.콘텐츠 uri를 사용하여 버튼 클릭시 커서 어댑터 안의 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을 반환합니다. 의견에서 나는 그것이 작동하는 부분까지 추가했습니다. 도와주세요.