0

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;

    }



}

}

Dayan
  • 7,634
  • 11
  • 49
  • 76
Reinmarius
  • 114
  • 11
  • Try getActivity().finish(); See this : https://stackoverflow.com/questions/4594996/activity-finish-called-but-activity-stays-loaded-in-memory – MatiRC Jul 11 '17 at 14:46

3 Answers3

2

Typecast your activity context into an activity. And then call finish method

Activity act=(Activity)context;
act.finish();
Dayan
  • 7,634
  • 11
  • 49
  • 76
1

public class DetailActvityActionBtn implements View.OnClickListener

You are not extending Activity or Fragment or anything along those lines, you have no context to execute context.finish(); from because finish() is a method from Activity.

If this class is utilized from an Activity then pass in that activity's reference to the class constructor, like so:

public MainActivity extends Activity{
  //You standard onCreate() blah...
   DetailActvityActionBtn yourHandlerClass = new DetailActvityActionBtn(this);

}

public class DetailActvityActionBtn implements View.OnClickListener {

private Activity activity;

public DetailActvityActionBtn(Activity activity){
    this.activity = activity;
}

@Override
public void onClick(View view){
    //You can now call activity.finish() to close the calling activity...
    activity.finish();
}
Dayan
  • 7,634
  • 11
  • 49
  • 76
0

Personally, I would suggest decoupling the button's reliance upon the existence of an activity. Rather than setting the functionality of onClick within the Button class, why not define the functionality within your controller (Activity)? You can define one onClick method and use your listview logic to determine which buttons should have this functionality.

If you were simply controlling business logic that would be one thing, but I personally think it's convoluted to have the view define the controller life cycle. The controller can give permission to the button to dismiss itself, but any other way and the button starts talking to things it probably shouldn't. Maybe you're following a different paradigm than MVC, so I could be wrong!

I was thinking something along the lines of:

@Override
  public View getView(int position, View v, ViewGroup parent) {
        if(condition1){
           v.button.setOnClickListener(locallyDefinedOnClickForCondition1);
        }
        else if(condition2){
           v.button.setOnClickListener(locallyDefinedOnClickForCondition2);
        }
     }

Would definitely not say it's the best solution, but maybe this could put you in the right direction. Anyone have any criticism?

Zack Matthews
  • 409
  • 2
  • 11