setOnItemLongClickListener is not working

Asked

Viewed 563 times

0

Can someone help me by telling me setOnItemLongClickListener not being called when I give a long click on my list item?

I have in the same Adapter 2 methods of type onclick, one is the setOnItemClickListener and the other is setOnItemLongClickListener, but only the first is working, follows the code:

public class CategoriaAdapter extends BaseAdapter {
private Context context;
List<Categoria> lista;
private ListView listView;
private SubCategoria subcategoria = new SubCategoria();

public CategoriaAdapter(Context context, List<Categoria> lista, ListView listView) {
    this.context = context;
    this.lista = lista;
    this.listView = listView;
}


@Override
public int getCount() {
    return lista.size();
}

@Override
public Object getItem(int position) {
    return lista.get(position);
}

@Override
public long getItemId(int i) {
    return i;
}

@Override
public View getView(int position, View view, ViewGroup viewGroup) {
    final int auxPosition = position;

    final Categoria categoria = new Categoria();


    LayoutInflater inflater = (LayoutInflater)
            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);


    final RelativeLayout layout = (RelativeLayout)
            inflater.inflate(R.layout.categoria_row, null);

    final TextView categ = (TextView)
            layout.findViewById(R.id.tvCat);
    categ.setText(lista.get(position).getNome());


    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, final long l) {
            AlertDialog.Builder alert = new AlertDialog.Builder(context);
            alert.setTitle("Adicionar subcategoria");
            alert.setMessage("Digite um nome para a subcategoria:");

            Toast.makeText(context, "Item with id [" + l + "] - Position [" + i + "] ", Toast.LENGTH_SHORT).show();


            final EditText input = new EditText(context);
            alert.setView(input);


            alert.setPositiveButton("Adicionar", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    //int idCategoria = (Integer) button.getTag();
                    int position = (int) l;

                    subcategoria.setNome(input.getText().toString());
                    subcategoria.setIdCategoria(position);


                    SubCategoriaDAO subCategoriaDAO = new SubCategoriaDAO(context);

                    subCategoriaDAO.inserir(subcategoria);
                }
            });

            alert.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    return;
                }
            });

            alert.show();
        }
    });


    listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
            AlertDialog.Builder dialog = new AlertDialog.Builder(context);
            dialog.setTitle("Apagar categoria?");
            dialog.setMessage("Essa ação não poderá ser desfeita");

            dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    CategoriaDAO categoriaDAO = new CategoriaDAO(context);

                    categoriaDAO.deletar(categoria);
                }
            });

            dialog.setNegativeButton("Voltar", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    return;
                }
            });
            return false;
        }
    });


    SubCategoriaDAO subcategoria = new SubCategoriaDAO(context);
    List<SubCategoria> list = subcategoria.getLista();
    ListView listView = (ListView) layout.findViewById(android.R.id.list);
    listView.setAdapter(new SubCategoriaAdapter(context, list, listView));


    return layout;
}
}

3 answers

1

You have to set setOnItemLongClickListener() in Listview:

        listView.setOnItemLongClickListener(new OnItemLongClickListener() {

        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int pos, long id) {
            // TODO Auto-generated method stub

            Log.v("long clicked","pos: " + pos);

            return true;
        }
    }); 

For each item in the list, XML (you should use a custom XML) should have android: longClickable = "true", as well as (or you can use the method listView.setLongClickable (true);). But this way, you can have a list with only a few items that respond to longclick.

1


I just forgot one line that already solved everything:

dialog.show();

It worked at the time, but now it calls onItemClick and onItemLongClick, the two together, how to solve?

  • 1

    Just return true of onItemLongClick. For better understanding: http://developer.android.com/reference/android/widget/AdapterView.OnItemLongClickListener.html.

-1

Dude, try using Log in your application, and so know where the error is occurring! However, create a method to put Alertdialog, and so you call it within this method!

(sorry to put as an answer, but I do not have enough points to comment!)

  • No error @Taironedias, it does not give any message or anything, I put a Log. i in the method but it is not called.

Browser other questions tagged

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