1

I have an ArrayAdapter inside of a popup dialog like this:

public void selectItem() {

    AlertDialog dialog;
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
    View popup = getLayoutInflater().inflate(R.layout.inventory, null);

    dialogBuilder.setView(popup);
    dialog = dialogBuilder.create();
    dialog.setCancelable(true);

    ListView lvInventory = popup.findViewById(R.id.inventory_lv);

    ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, game.inventory.getInventory());
    lvInventory.setAdapter(adapter);
    lvInventory.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectedItem = game.inventory.getItem(position);

        }
    });
        
    dialog.show();

}

I would like to be able to make selectItem() of the type ITEM (enum) and return the selected item via the function, instead of making a global variable to store it.

public ITEM selectItem() {

    ITEM selectedItem;

    //create dialog
    //obtain selected item through clicking on ListView popup

    return selectedItem;

}

How can I return this information, if I am obtaining it via onClick() method? Thanks.

JAgüero
  • 403
  • 1
  • 4
  • 14
  • If this method is in same class you can just call your method with `selectedItem` as argument. i.e whatever you have to do after selection . if not then you can create a [Callback Interface](https://stackoverflow.com/questions/3398363/how-to-define-callbacks-in-android) or you can pass `AdapterView.OnItemClickListener` as argument to `selectItem()` method . – ADM Jan 13 '22 at 04:44

0 Answers0