如何在android中的listview上实现弹出菜单

时间:2014-11-27 11:51:57

标签: android android-listview popupmenu

我正在android中的listview上实现一个弹出菜单。我在这一行上得到一个空指针异常

PopupMenu popupMenu = new PopupMenu(mContext,view.findViewById(R.id.imageButton1));

**只有第一行的弹出菜单才有效。当我点击其他行中的按钮时,异常就会出现。

原因是锚视图为空,而不是上下文。如何解决这个问题?

这是我的完整getView方法。

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

    ViewHolderItem viewHolder = null;

    final View view = convertView;

    if (convertView == null) {

        if (inflater == null) {
            inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        convertView = inflater.inflate(R.layout.main_list_row, null);

        viewHolder = new ViewHolderItem();

        viewHolder.tvTitle = (TextViewPlus) convertView
                .findViewById(R.id.tvShowTitle);
        viewHolder.tvChannelName = (TextViewPlus) convertView
                .findViewById(R.id.tvChannelName);
        viewHolder.tvViews = (TextViewPlus) convertView
                .findViewById(R.id.tvViews);
        viewHolder.ivThumbImage = (ImageView) convertView
                .findViewById(R.id.ivThumbImage);
        viewHolder.tvShowId = (TextView) convertView
                .findViewById(R.id.tvShowId);
        viewHolder.btnSubscribe = (Button)convertView.findViewById(R.id.btnSubscribe);
        //viewHolder.imgButton = (ImageButton)convertView.findViewById(R.id.imageButton1);
        viewHolder.btnTest = (Button)convertView.findViewById(R.id.imageButton1);

        convertView.setTag(viewHolder);

    } else {

        viewHolder = (ViewHolderItem) convertView.getTag();
    }

    SharedPreferences SP = PreferenceManager
            .getDefaultSharedPreferences(mContext);
    String languageType = SP.getString("applicationLanguage", "2");

    final Show show = data.get(position);

    if (languageType.equals("2")) {
        viewHolder.tvTitle.setText(show.getShowNameEn());
        viewHolder.tvChannelName.setText(show.getChannelNameEn());
    } else {
        viewHolder.tvTitle.setText(show.getShowNameSi());
        viewHolder.tvChannelName.setText(show.getChannelNameSi());
    }

    viewHolder.tvViews.setText("Views :" + show.getViews());
    viewHolder.tvShowId.setText(show.getShowId());
    viewHolder.btnSubscribe.setTag(position);
    viewHolder.btnTest.setTag(position);

    if(show.getSubscribed()==0){
        viewHolder.btnSubscribe.setText("Subscribe");
    }else{
        viewHolder.btnSubscribe.setText("Unsubscribe");
    }

    viewHolder.btnSubscribe.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            int result = dbHelper.subscribeShow(Integer.parseInt(show.getShowId()));

            if(result>0){

                if(((Button)v).getText().equals("Subscribe")){
                    ((Button)v).setText("Unsubscribe");
                }else if(((Button)v).getText().equals("Unsubscribe")){
                    ((Button)v).setText("Subscribe");
                }
            }

            data = dbHelper.getShowList(Integer.parseInt(show.getCategoryId()));

        }
    });

    viewHolder.btnTest.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            PopupMenu popupMenu = new PopupMenu(mContext,view.findViewById(R.id.imageButton1));

            popupMenu.inflate(R.menu.list_menu);

            popupMenu.show();

        }
    });

2 个答案:

答案 0 :(得分:4)

如果您想要使用您点击的按钮,作为PopMenu的anchorView,您可以使用view.findViewById更改v,例如:

 viewHolder.btnTest.setOnClickListener(new View.OnClickListener() {    
        @Override
        public void onClick(View v) {  
            PopupMenu popupMenu = new PopupMenu(mContext,v);
            popupMenu.inflate(R.menu.list_menu);
            popupMenu.show();
        }
    });

答案 1 :(得分:1)

无需手动查找ViewById中的imageButton1点击v是imageButton1的引用:

PopupMenu popupMenu = new PopupMenu(mContext,v);