1

I am stuck at one point. I want to add popup menu to each item in custom list view. I have tried but it is only popping up at last position or last item of list.I am getting reason why it is coming at last position but not getting the solution here i am adding the adapter class

public class InterestLevelAdapterEditable extends BaseAdapter {

    private Activity activity;
    private  TextView level;
    private InterestLevel m;
    private LayoutInflater inflater;
    private List<InterestLevel> interestLevelList;

    public InterestLevelAdapterEditable(Activity activity, List<InterestLevel> interestLevelList) {
        this.activity = activity;
        this.interestLevelList = interestLevelList;
    }

    @Override
    public int getCount() {
        return interestLevelList.size();
    }

    @Override
    public Object getItem(int location) {
        return interestLevelList.get(location);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (inflater == null)
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null)
            convertView = inflater.inflate(R.layout.interest_level_editable, null);


        TextView sports_name = (TextView) convertView.findViewById(R.id.sportsName);
        level = (TextView) convertView.findViewById(R.id.level);

         m = interestLevelList.get(position);

        sports_name.setText(m.getSports_name());

        level.setText(m.getLevel());
        level.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Creating the instance of PopupMenu
                PopupMenu popup = new PopupMenu(activity, level);
                //Inflating the Popup using xml file
                popup.getMenuInflater().inflate(R.menu.popup_level, popup.getMenu());

                //registering popup with OnMenuItemClickListener
                popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    public boolean onMenuItemClick(MenuItem item) {
                        level.setText(item.getTitle());
                        return true;
                    }
                });

                popup.show();//showing popup menu
            }
        });

        return convertView;
    }

}

I am getting reason that i will have to pass position in on click listener but not getting how should i add the position or (m object of InterestLevel which is having position).

Thanks in advance. Screenshot

Shadab Ansari
  • 7,022
  • 2
  • 27
  • 45
user3679536
  • 21
  • 2
  • 8

2 Answers2

2

I need popup menu as like listview. I try this code and it's right(reference):

private static final String TITLE = "title";
private static final String ICON = "icon";

private List<HashMap<String, Object>> data = new ArrayList<HashMap<String, 
Object>>();

// Use this to add items to the list that the ListPopupWindow will use
private void addItem(String title, int iconResourceId) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put(TITLE, title);
map.put(ICON, iconResourceId);
data.add(map);
}

// Call this when you want to show the ListPopupWindow
private void showListMenu(View anchor) {
ListPopupWindow popupWindow = new ListPopupWindow(this);

ListAdapter adapter = new SimpleAdapter(
        this,
        data,
        android.R.layout.activity_list_item, // You may want to use your own cool layout
        new String[] {TITLE, ICON}, // These are just the keys that the data 
uses
        new int[] {android.R.id.text1, android.R.id.icon}); // The view ids 
to map the data to


popupWindow.setAnchorView(anchor);
popupWindow.setAdapter(adapter);
popupWindow.setWidth(400); // note: don't use pixels, use a dimen resource
popupWindow.setOnItemClickListener(myListener); // the callback for when a list item is selected
popupWindow.show();
}

custom layout and work as listview

0

Create your PopupMenu outside setOnClickListener() and show() it in onClick():

//Creating the instance of PopupMenu
final PopupMenu popup = new PopupMenu(activity, level);
//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.popup_level, popup.getMenu());

//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
    public boolean onMenuItemClick(MenuItem item) {
        level.setText(item.getTitle());
        return true;
    }
});

level.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {    
        popup.show();//showing popup menu
    }
});
gotwo
  • 663
  • 8
  • 16
Shadab Ansari
  • 7,022
  • 2
  • 27
  • 45
  • Thanks Shadab for your answer, but this solution is not updating list item value to each position it is updating only last list item value as it was doing before.so we need to pass position for updating item text at each position. – user3679536 Apr 15 '16 at 11:52