2 years ago

#44694

test-img

parkorian

How can I get information from an ArrayAdapter in a Dialog?

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.

java

android

onclick

dialog

android-arrayadapter

0 Answers

Your Answer

Accepted video resources