Reputation: 53
I'm trying to initialise a PopupMenu when I click an item in an item in a custom listview. My onItemClick works confirmed by the logging.
However, I am unable to get the popup menu to work within my method. I do not understand what the correct context parameter is to the PopupMenu.
Given the error:
error: incompatible types: <anonymous OnItemClickListener> cannot be converted to Context
PopupMenu popup = new PopupMenu(this, position);
Using getActivity() as suggested - Error:(124, 62) error: incompatible types: Class cannot be converted to Context
Results in: error:
cannot find symbol
PopupMenu popup = new PopupMenu(getActivity(), position);
^
symbol: method getActivity()
What parameter should I pass in here? Or is OnItemClickListener even compatible with PopupMenu?
Below is the full method:
private void populateUsersList() {
// Construct the data source
ArrayList<Issue> arrayOfUsers = ProfileActivity.getAllIssueList();
// Create the adapter to convert the array to views
issueAdapter adapter = new issueAdapter(this, arrayOfUsers);
// Attach the adapter to a ListView
final ListView listView = findViewById(R.id.listView);
//populate the listView
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Get the selected item text from ListView
Issue selectedItem = (Issue) parent.getItemAtPosition(position);
Log.d("clicked",selectedItem.getIssueID());
PopupMenu popup = new PopupMenu(this, position);
//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.popup_listview, popup.getMenu());
}
});
}
Upvotes: 0
Views: 372
Reputation: 972
this in a nested class refers to the nested class instance, not to the outer class instance (MainActivity). Qualify it with e.g. MainActivity.this to refer to the outer class instance :
PopupMenu popup = new PopupMenu(MainActivity.this, position);
Upvotes: 1