There is an Activity that displays listItems (through a cursorAdapter).
The listItem's XML contains some buttons. In the Cursor Adapter's newView()
method, these buttons get the onClickListener
, not by an anonymous declaration, there's a class that implements the listener. If there's a click on a certain button, the activity where all that happens, should finish.
I'm not surprised that calling finish()
in the button class doesn't work. activityContext.finish
doesn't work either.
So how can I manage that?
public class DetailActvityActionBtn implements View.OnClickListener {
private Context context;
@Override
public void onClick(View view){
context = view.getContext();
System.out.println("CONTEXT:" + context);
///Itemroot
LinearLayout root =(LinearLayout) view.getRootView().findViewById(R.id.detailRoot);
///Tag that stores data
ItemViewAndDataHolder holder = (ItemViewAndDataHolder) root.getTag();
System.out.println("HOLDER: " + holder.toString());
//Get id of item
int id = holder.getId();
//Get quantity of item
int quantity = Integer.parseInt(holder.getQuantity().getText().toString().replaceAll("[^0-9]",""));
///Append id to URI
Uri updateItemUri = ContentUris.withAppendedId(InventoryDB_Contract.entries.CONTENT_URI, id);
///To determine the clicked button, get ID as String
String btnIDasString = context.getResources().getResourceName(view.getId());
System.out.println(btnIDasString);
ContentValues values = new ContentValues();
int updatedRow;
switch (btnIDasString){
case "com.example.android.inventoryapp:id/plusBtn":
System.out.println("plus");
values.put(InventoryDB_Contract.entries.COLUMN_PRODUCT_QUANTITY_IN_STOCK, quantity + 1);
context.getContentResolver().update(updateItemUri, values, null, null);
//CRcaller.saleItem(1);
break;
case "com.example.android.inventoryapp:id/minusBtn":
System.out.println("mins");
values.put(InventoryDB_Contract.entries.COLUMN_PRODUCT_QUANTITY_IN_STOCK, quantity - 1);
updatedRow = context.getContentResolver().update(updateItemUri, values, null, null);
break;
case "com.example.android.inventoryapp:id/deleteItemBtn":
System.out.println("delete");
context.getContentResolver().delete(updateItemUri, null, null);
context.finish();
break;
}
}
}