Reputation: 63
I have a button on a ListView
and on clicking that button i want to show a contextmenu defined in my layout.
Problem is the method registerForContextMenu
is not recognized by customerlistadapter.
The context menu methods onCreateContextMenu
,onContextItemSelected
are overridden in the activity which is showing the ListView
.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.customforumview, null);
holder = new ViewHolder();
holder.txtTitle = (TextView)
convertView.findViewById(R.id.forumtitle);
holder.txtCategory = (TextView)
convertView.findViewById(R.id.forumcategory);
holder.menubutton = (ImageButton)
convertView.findViewById(R.id.menuselect);
holder.menubutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//context menu to be called here
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtTitle.setText(searchArrayList.get(position).getTitle());
holder.txtCategory.setText(searchArrayList.get(position).getCategory());
return convertView;
}
Upvotes: 3
Views: 2507
Reputation: 5087
If you have registered correctly your contextMenu in your Activity, you could call your contextMenu with:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
...
holder.menubutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//context menu to be called here
parent.showContextMenuForChild(v);
}
...
});
If you want to access to which elements was called in your activity add in your onCreateContextMenu
like this
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
long itemID = info.position;
menu.setHeaderTitle("lior" + itemID);
}
Refer for more info to @Lior Iluz answer
Hope this helps!!
Upvotes: 4