Problem with listview inside a Fragment

Asked

Viewed 104 times

-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;
}

}

1 answer

0


> I would do differently put a popup in the list click to so make the call

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

            Animal animal = (Animal) arg0.getItemAtPosition(position)

            PopupMenu popup = new PopupMenu(MainActivity.this, arg1);
            popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
            //registering popup with OnMenuItemClickListener
            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                public boolean onMenuItemClick(MenuItem item) {
                    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);
                    }
                    return true;
                }
            });

            popup.show();//showing popup menu
        }
    });
  • I was able to perform the procedure using your suggestion. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.