开发者

Why is item.getMenuInfo() null?

Within my ListVeiw item I have 4-8 ImageView(some of them are invisible depended of some value) and 4 TextView.

When I Long click to item of Listview the (AdapterContextMenuInfo) item.getMenuInfo() returns me null.

In my adapter I have defined the following methods:

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(Ex开发者_StackOverflowception 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;

}

And when I attempt to get item.getMenuInfo(), I receive null.


You need to register the list for context menu, not the items.

// wrong:
activity.registerForContextMenu(vi);

// right:
activity.registerForContextMenu(myList);


It may not have anything to do with it, but one glaring thing I see right away is:

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

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

Your getItem method is returning an int of the object's position, not the actual Object itself as stated in its method declaration.


getMenuInfo() will work on ListAdapter, not on views.

But, You can pass additional data with the tag of the view.

in getView: vi.setTag(position) activity.registerForContextMenu(vi);

declare in Activity private int id;

onCreateContextMenu: id = (int) v.getTag();

onContextItemSelected: you can use the id

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜