为什么 item.getMenuInfo() 为空?
在我的 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。
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(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;
}
And when I attempt to get item.getMenuInfo()
, I receive null.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要注册上下文菜单的列表,而不是项目。
You need to register the list for context menu, not the items.
它可能与它没有任何关系,但我立即看到的一件明显的事情是:
您的 getItem 方法返回对象位置的
int
,而不是实际的Object
其本身如其方法声明中所述。It may not have anything to do with it, but one glaring thing I see right away is:
Your getItem method is returning an
int
of the object's position, not the actualObject
itself as stated in its method declaration.getMenuInfo() 适用于 ListAdapter,不适用于视图。
但是,您可以使用视图的标签传递附加数据。
在获取视图中:
vi.setTag(位置)
Activity.registerForContextMenu(vi);
在活动中声明
私有 int id;
onCreateContextMenu:
id = (int) v.getTag();
onContextItemSelected:
你可以使用id
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
不久前我也遇到了类似的问题。深入研究文档后,我发现必须使用 View#setOnCreateContextMenuListener 方法将所选项目的数据传递到视图 View.OnCreateContextMenuListener 内适配器中项目根视图中的 ContextMenu.ContextMenuInfo 对象。但是,没有方法可以执行此操作,并且使用 AdapterView.AdapterContextMenuInfo 构造函数设置对象实际上不会覆盖它的 null 值。因此,我使用自定义对象在 View#setOnCreateContextMenuListener 方法中传递这些值,并在 AppCompatActivity#onContextItemSelected 方法中获取值。这是我的代码...
这是我的项目布局
这是我的适配器,我在 RecyclerView.Adapter#onBindViewHolder 方法中设置了 View#setOnCreateContextMenuListener
这是我的 AppCompatActivity,我创建了一个 PositionHolder 的私有对象并将引用传递给创建上下文菜单时进行修改的适配器对象。我在底部添加了 PositionHolder 类。
一切顺利。感谢您的阅读,祝您编码愉快。
I had a similar problem a while ago. After taking a deep look at the documentation, I found out one has to pass the data of the selected item to the ContextMenu.ContextMenuInfo object in the root view of the item in adapter inside the view's View.OnCreateContextMenuListener using the View#setOnCreateContextMenuListener method. However, there are no methods to do this and setting the object using an AdapterView.AdapterContextMenuInfo constructor does not actually overwrite it's null value. Thus I used a custom object to pass these values in the View#setOnCreateContextMenuListener method and get the values in the AppCompatActivity#onContextItemSelected method. Here's how my codes were...
This was my item layout
And this was my adapter, I set the View#setOnCreateContextMenuListener in the RecyclerView.Adapter#onBindViewHolder method
And Here's my AppCompatActivity, I created a private object of PositionHolder and passed a reference to the adapter object for modification upon context menu creation. I added the PositionHolder class at the bottom.
Everything worked well. Thank you for reading, happy coding.