在我的ListVeiw项目中,我有4-8个ImageView(其中一些是不可见的,取决于某些值)和4个TextView。

当我长按一下Listview的项目时(AdapterContextMenuInfo)item.getMenuInfo()返回null。

在我的适配器中,我定义了以下方法:

public int getCount() {
   return NewList.size();
}

public Object getItem(int position) {
   return position;
}

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

public void Remove(int id){
    notifyDataSetChanged();
}


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

   View vi=convertView;
   NewsFeedHolder holder;
   News n= NewList.get(position);
   List<NewsItem> items = n.getNewsItems();
   int ItemsLen = items.size();

   if (convertView == null){

       vi = inflater.inflate(R.layout.newsfeed_item, null);

       holder=new NewsFeedHolder();

       holder.txtTitle        = (TextView)vi.findViewById(R.id.newsfeed_item_name);
       holder.avatar_image    = (ImageView)vi.findViewById(R.id.newsfeed_item_icon);
       holder.txtUpdate       = (TextView)vi.findViewById(R.id.newsfeed_item_update);
       holder.txtLike         = (TextView)vi.findViewById(R.id.newsfeed_item_like);
       holder.txtDisLike      = (TextView)vi.findViewById(R.id.newsfeed_item_dislike);
       holder.btnLike         = (ImageButton)vi.findViewById(R.id.newsfeed_like_button) ;
       holder.btntxtDisLike   = (ImageButton)vi.findViewById(R.id.newsfeed_dislike_button);

       holder.imgItemIcon1    = (ImageView)vi.findViewById(R.id.newsfeed_item_icon1);
       holder.imgItemIcon2    = (ImageView)vi.findViewById(R.id.newsfeed_item_icon2);
       holder.imgItemIcon3    = (ImageView)vi.findViewById(R.id.newsfeed_item_icon3);
       holder.btnLike.setOnClickListener(activity.OnLikeClickListener);
       holder.btntxtDisLike.setOnClickListener(activity.OnDisLikeOnClickListener);

        vi.setTag(holder);
    }
    else {
          holder=(NewsFeedHolder)vi.getTag();
    }

   imageLoader.DisplayImage(n.getUserAvatarUrl(), activity, holder.avatar_image);
   String url1,url2,url3;

    try{

        switch(ItemsLen){


        case 1:
            url1 = n.getNewsItems().get(0).getItemUrl();
            imageLoader.DisplayImage(url1, activity, holder.imgItemIcon1);
            break;

        case 2:
            url1 = n.getNewsItems().get(1).getItemUrl();
            imageLoader.DisplayImage(url1, activity, holder.imgItemIcon1);
            url2 = n.getNewsItems().get(1).getItemUrl();
            imageLoader.DisplayImage(url2, activity, holder.imgItemIcon2);
            break;

        case 3:
            url1 = n.getNewsItems().get(2).getItemUrl();
            imageLoader.DisplayImage(url1, activity, holder.imgItemIcon1);
            url2 = n.getNewsItems().get(2).getItemUrl();
            imageLoader.DisplayImage(url2, activity, holder.imgItemIcon2);
            url3 = n.getNewsItems().get(2).getItemUrl();
            imageLoader.DisplayImage(url3, activity, holder.imgItemIcon3);
            break;

    default:

    }
    }
    catch(Exception e){
        Log.i("adaptex",e.getMessage());
    }


    holder.txtTitle.setText(n.getNewsTitle());
    holder.txtUpdate.setText(n.getUpdate());
    holder.txtLike.setText(Integer.toString(n.getLike()));
    holder.txtDisLike.setText(Integer.toString(n.getDesLike()));

    activity.registerForContextMenu(vi);

    vi.setOnClickListener(activity.OnListItemClickListener);


    return vi;

}


当我尝试获取item.getMenuInfo()时,会收到null。

最佳答案

您需要注册上下文菜单的列表,而不是项目。

// wrong:
activity.registerForContextMenu(vi);

// right:
activity.registerForContextMenu(myList);

关于android - 为什么item.getMenuInfo()为null?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6190451/

10-10 06:10