Reputation: 2268
I have a popup menu and I'm implementing the actions for the items inside the menu; however, when pressing second item in menu "notification" nothing happens. the first item works "change icon" but not the second. I'm suspecting a problem with evenlistener inside the function. if someone can point me to proper way of doing this. I was check android dev documentation and they implement a bit different. main activity
package org.pctechtips.menulistview;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayList hosts;
String ipAdd = "192.168.10.";
ListView list;
ArrayAdapter<String> adapter;
PopupMenu popup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hosts = new ArrayList<String>();
for(int i = 0; i < 255; i++) {
hosts.add(ipAdd+i);
}
list = (ListView) findViewById(R.id.list);
adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.ip_address, hosts);
list.setAdapter(adapter);
//registering the menu with the listview
// registerForContextMenu(list);
}
/* code for popup menu in listview */
public void showPopupMenu(View v) {
popup = new PopupMenu(this, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.list_menu, popup.getMenu());
/* handling event listeners*/
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch(menuItem.getItemId()) {
case R.id.change_icon:
Toast.makeText(MainActivity.this, menuItem.getTitle(), Toast.LENGTH_SHORT).show();
return true;
case R.id.notifications:
Toast.makeText(MainActivity.this, menuItem.getTitle(), Toast.LENGTH_SHORT).show();
return true;
default:
return MainActivity.super.onOptionsItemSelected(menuItem);
}
}
});
popup.show();
}
}
popup menu xml file
<!-- listview menu for host scan options eg:
change hostname, icon, notification etc -->
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- Settings, should always be in the overflow -->
<item android:id="@+id/change_icon"
android:title="change icon"
app:showAsAction="always"/>
<itme android:id="@+id/notifications"
android:title="notifications"
app:showAsAction="always"/>
<item android:id="@+id/set_hostname"
android:title="set notification"
app:showAsAction="always"/>
</menu>
Upvotes: 0
Views: 1411
Reputation: 62831
You have item
misspelled in your menu file for notifications.
<itme android:id="@+id/notifications"
android:title="notifications"
app:showAsAction="always"/>
Could that be the issue?
Just tested and that seems to be the issue. Correct the spelling and the menu should work OK.
Upvotes: 1