Null In While Using Context Menu For Recyclerview
I have read a lot of STACK questions and tried implementing everything possible. but i keep getting null for AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextM
Solution 1:
I finally managed to get the Selected Item Id as you requested.
In the case of this line
AdapterView.AdapterContextMenuInfoinfo= (AdapterView.AdapterContextMenuInfo) menuInfo;
menuinfo will always be null and It's reported many times but still not solved. I hardly managed to get the position of selected List Item and I will give you some hints of that.
extend View.OnLongClickListener
publicclassViewHolderextendsRecyclerView.ViewHolder implementsView.OnCreateContextMenuListener, View.OnClickListener, View.OnLongClickListener
inside ViewHolder assign setOnLongClickListener
public ViewHolder(View v) {
super(v);
v.setOnCreateContextMenuListener(this);
v.setOnLongClickListener(this);
}
then onLongClicklooks like this
@OverridepublicbooleanonLongClick(View v) {
recyclerViewClickListener.recyclerViewListClicked(this.getPosition());
returnfalse;
}
Where recyclerViewClickListener is an Interface and recyclerViewListClicked is a method
publicinterfaceRecyclerViewClickListener
{
publicintrecyclerViewListClicked(int position);
}
Then in your Activity create a globalVariable which should be a class level variable and set its value inside the override method which looks like
@OverridepublicintrecyclerViewListClicked(int position) {
globalVariable = position;
return position;
}
Then after that you can use that globalVariable as selected items position like here
@OverridepublicbooleanonContextItemSelected(MenuItem item) {
Log.d("mySelectedItem", ""+ globalVariable) //use the globalVariable as you needreturnsuper.onContextItemSelected(item);
}
Post a Comment for "Null In While Using Context Menu For Recyclerview"