如何将弹出菜单添加到自定义列表视图到每个项目android

时间:2016-04-14 22:16:28

标签: android listview android-custom-view

我被困在某一点上。 我想在自定义列表视图中为每个项添加弹出菜单。 我已经尝试但它只是在最后一个位置或列表的最后一项弹出。我有理由为什么它会在最后一个位置而不是得到解决方案 这里我要添加适配器类

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

2 个答案:

答案 0 :(得分:0)

PopupMenu -

中创建setOnClickListener() show()以及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 } });
def self.import(file)
  spreadsheet = open_spreadsheet(file)
  cn = column_names
  header = spreadsheet.row(1)
  (2..spreadsheet.last_row).each do |i|
    rawrow = Hash[[header, spreadsheet.row(i)].transpose]
    cleansedrow = {:properties => {}}
    rawrow.each do |key, value|
      if cn.include?(key)
        cleansedrow[key] = value
      else
        cleansedrow[:properties][key] = value
      end
    end
    record = find_by_id(cleansedrow["id"]) || new
    record.attributes = cleansedrow.to_hash
    record.save!
  end
end

答案 1 :(得分:0)

我需要像listview一样的弹出菜单。我尝试使用此代码并且它正确(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