0

I am creating a pop up menu dynamically, where the items will be populated from the web service.

The situation is, the parsing has been in the MainActivity.java, but the Pop up menu is inside the BaseAdapter.java class. I am adding all the menu items inside an array inside the MainActivity.java.Please refer the code below:

try {
        JSONArray jsonArray = new JSONArray(menuItemsResponse.toString());
        for (int i = 0; i < jsonArray.length(); i++){
        JSONObject object = jsonArray.getJSONObject(i);
        String strMenuItemNames = object.getString("Name");

        listMenuItems.add(strMenuItemNames);
                }

     } catch (Exception e) {
         e.printStackTrace();
     }

I am adding items inside List<String> listMenuItems with listMenuItems.add(strMenuItemNames).

Now, I want to access this listMenuitems from BaseAdapter class. Below is my code for Pop Up menu inside getView() method in BaseAdapter class:

PopupMenu popupMenu = new PopupMenu(activity, imgDropDown);
popupMenu.getMenu().add()  // array to be added here


popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener({
   // on click events for each item
});

Anyone with solutions please respond.

Thanks in advance!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Sarthak Sharma
  • 137
  • 3
  • 10
  • there is a method called getItem(int position) you can return same instance from this method and called adapter.getItem(). – Drup Desai Oct 18 '17 at 09:27

2 Answers2

0

1.Add the list information you need to use in the construction method .

List<String> listMenuItems;
public MyAdapter(List<String> listMenuItems, Context context) {
   ...
}

2.use it in your code .

popupMenu.getMenu().add(listMenuItems.get(position));

Try this .

public class MyAdapter extends BaseAdapter {

    List<String> listMenuItems;
    private LayoutInflater inflater;
    private Context context;

    public MyAdapter(List<String> listMenuItems, Context context) {
        this.context = context;
        this.listMenuItems = listMenuItems;
        this.inflater = LayoutInflater.from(context);
    }

    ...

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = inflater.inflate(R.layout.your_layout, null);
        ...

        PopupMenu popupMenu = new PopupMenu(activity, imgDropDown);
        // edited here 
        popupMenu.getMenu().add(listMenuItems.get(position));  // array to be added here
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener({
                // on click events for each item
        });
        return view;
    }
}
KeLiuyue
  • 8,149
  • 4
  • 25
  • 42
0

You can define an interface like below

interface MenuItemProvider {
     getMenuItems(); // add parameters, returntype based on your need.
}

Activity implements this interface and implements getMenuItems() method by returning the listItems that are prepared in activity.

Now, your adapter should have a setter method to set this interface like below.

class yourActivity extends <> implements MenuItemProvider {

// other implementation.

// pass this implementation to your base adapter.
baseadapterinstance.setMenuItemProvider(this);


getMenuItems(){
 // return list items.
}



    }


/*** BaseAdpater class. ***/
private MenuItemProvider menuItemProviderImpl;

void setMenuItemProvider(MenuItemProvider menuItemProviderImpl){
this. menuItemProviderImpl = menuItemProviderImpl;
}

// when you need to get the list, call 
menuItemProviderImpl.getMenuItems();

Hope this helps!!

GRK
  • 204
  • 1
  • 6