-1
My problem is that when I click on some listview item the app does not return me any menu options. Option which, when clicked would call a new Activity.
Code:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
animais = new ArrayList<>();
View view = inflater.inflate(R.layout.fragment_animais, container, false);
listView = (ListView) view.findViewById(R.id.lv_animais);
adapter = new AnimaisAdapter(getActivity(),animais); //utilizo um adapter customizado
listView.setAdapter(adapter);
registerForContextMenu(view.findViewById(R.id.lv_animais)); //teste também passando apenas listView, porém sem sucesso
Preferencias preferencias = new Preferencias(getActivity());
String identificadorLogado = preferencias.getIdentificador();
firebase = ConfiguracaoFirebase.getFirebase()
.child("animal")
.child(identificadorLogado);
valueEventListenerAnimais = new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
animais.clear();
for(DataSnapshot dados: dataSnapshot.getChildren()){
Animal animal = dados.getValue(Animal.class);
animais.add(animal);
}
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
};
return view;
}
Routine of the Createcontextmenu:
@Override
public void onCreateContextMenu(final ContextMenu menu, View v, final ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuItem visualizarAnimal = menu.add("Visualizar Animal");
visualizarAnimal.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
final Animal animal = (Animal) animais.get(((AdapterView.AdapterContextMenuInfo) item.getMenuInfo()).position);
Intent intent = new Intent(getActivity(), CadastroAnimalActivity.class);
intent.putExtra("brinco", animal.getBrinco());
intent.putExtra("nome", animal.getNome());
startActivity(intent);
return false;
}
});
}
I made an attempt (see below), but without success:
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.menu_item_list, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
final Animal animal = (Animal) animais.get(info.position);
switch (item.getItemId()) {
case R.id.item_list_editar:
Intent intent = new Intent(getActivity(), CadastroAnimalActivity.class);
intent.putExtra("brinco", animal.getBrinco());
intent.putExtra("nome", animal.getNome());
startActivity(intent);
return true;
case R.id.item_list_visualizar_despesas:
Intent intentDespesas = new Intent(getActivity(), DespesasAnimal.class);
intentDespesas.putExtra("brinco", animal.getBrinco());
intentDespesas.putExtra("nome", animal.getNome());
startActivity(intentDespesas);
return true;
default:
return super.onContextItemSelected(item);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
animais = new ArrayList<>();
View view = inflater.inflate(R.layout.fragment_animais, container, false);
listView = (ListView) view.findViewById(R.id.lv_animais);
adapter = new AnimaisAdapter(getActivity(),animais);
listView.setAdapter(adapter);
registerForContextMenu(listView);
Preferencias preferencias = new Preferencias(getActivity());
String identificadorLogado = preferencias.getIdentificador();
firebase = ConfiguracaoFirebase.getFirebase()
.child("animal")
.child(identificadorLogado);
valueEventListenerAnimais = new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
animais.clear();
for(DataSnapshot dados: dataSnapshot.getChildren()){
Animal animal = dados.getValue(Animal.class);
animais.add(animal);
}
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
};
return view;
}
}
Maybe this resolve.
– ramaral