I have been trying to make a simple app with a menu that change the bg color of the item that calls the menu. But instead item.getMenuInfo is returning null and I dont know why.
Some solutions on the internet says that it's because you should pass the entire list instead of the items. However I'm passing the entire ListView but I keep getting an java.lang.NullPointerException because item.getMenuItem() returns null.
This is where I register it:
AdaptadorPersonalizado adaptador = new AdaptadorPersonalizado(this, R.layout.layoutlinealistview, elementos);
ListView lista = (ListView) findViewById(R.id.provincias);
lista.setAdapter(adaptador);
lista.setOnItemClickListener(this);
registerForContextMenu(lista);
And is in this method where I use the item.getMenuInfo() function:
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId()) {
case R.id.rojo:
return true;
default:
return true;
}
}
And this is the class where I create the custom ArrayAdapter and assign it the content:
String[] elementos = {"León",
"Zamora",
"Salamanca",
"Palencia",
"Valladolid",
"Ávila",
"Burgos",
"Segovia",
"Soria"};
String[] descripciones = {"Provincia de Leon",
"Provincia de Zamora",
"Provincia de Salamanca",
"Provincia de Palencia",
"Provincia de Valladolid",
"Provincia de Ávila",
"Provincia de Burgos",
"Provincia de Segovia",
"Provincia de Soria"
};
int[] imagenes = {R.drawable.leon,
R.drawable.zamora,
R.drawable.salamanca,
R.drawable.palencia,
R.drawable.valladolid,
R.drawable.avila,
R.drawable.burgos,
R.drawable.segovia,
R.drawable.soria
};
class AdaptadorPersonalizado extends ArrayAdapter<String> {
public AdaptadorPersonalizado(Context ctx, int txtViewResourceId, String[] objects) {
super(ctx, txtViewResourceId, objects);
}
@Override
public View getDropDownView(int position, View cnvtView, ViewGroup prnt) {
return crearFilePersonalizada(position, cnvtView, prnt);
}
public View getView(int pos, View cnvtView, ViewGroup prnt) {
return crearFilePersonalizada(pos, cnvtView, prnt);
}
private View crearFilePersonalizada(int posicion, View convertView, ViewGroup parent) {
LayoutInflater inflador = getLayoutInflater();
View miFila = inflador.inflate(R.layout.layoutlinealistview, parent, false);
TextView nombre = miFila.findViewById(R.id.textViewNombre);
nombre.setText(elementos[posicion]);
TextView descripcion = miFila.findViewById(R.id.textViewDescripcion);
descripcion.setText(descripciones[posicion]);
ImageView imagen = miFila.findViewById(R.id.imageViewImagenesCiudades);
imagen.setImageResource(imagenes[posicion]);
return miFila;
}
}
Thanks in advance.
The Android menu is very easy to use, compared to other widgets like the recycler view. Using an Adapter for a menu is not needed , as it's handled by default. All you need to do is, Create a XML file called menu inside res/layout
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Single menu item
Set id, icon and Title for each menu item -->
<item android:id="@+id/menu_leon"
android:icon="@drawable/icon_leon"
android:title="Leon" />
<item android:id="@+id/menu_zamora"
android:icon="@drawable/icon_zamora"
android:title="zamora" />
</menu>
For all your menu items, and then Initiate it in your fragment / Activity using
// Initiating Menu XML file (menu.xml)
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
return true;
}
And then do,
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.leon:
//Do your stuff for leon, zomara,etc
return true;
}
}
Hope this helped 😇
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.