【问题标题】:how to add popup menu to custom listview to each item android如何将弹出菜单添加到自定义列表视图到每个项目android
【发布时间】:2023-03-24 19:30:02
【问题描述】:

我被困在某一点上。 我想为自定义列表视图中的每个项目添加弹出菜单。 我已经尝试过,但它只在最后一个位置或列表的最后一项弹出。我得到了为什么它出现在最后一个位置但没有得到解决方案的原因 这里我添加了适配器类

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;
    }

}

我得到的理由是我必须在点击侦听器上传递位置,但不知道我应该如何添加位置或(具有位置的 InterestLevel 的 m 对象)。

提前致谢。 Screenshot

【问题讨论】:

    标签: android listview android-custom-view


    【解决方案1】:

    我需要像列表视图一样的弹出菜单。我试试这段代码,没错(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

    【讨论】:

      【解决方案2】:

      setOnClickListener() 之外创建您的PopupMenu 并在onClick() 中创建show()

      //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
          }
      });
      

      【讨论】:

      • 感谢 Shadab 的回答,但此解决方案不会将列表项值更新到每个位置,它只更新最后一个列表项值,就像以前一样。所以我们需要传递位置来更新项目文本在每个位置。
      猜你喜欢
      • 1970-01-01
      • 2011-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-24
      • 2010-09-26
      相关资源
      最近更新 更多