setOnItemLongClickListener is not working

0

Can someone help me by saying that my setOnItemLongClickListener is not being called when I give a long click on the item in my list?

I have in the same adapter 2 methods of type onclick, one is the setOnItemClickListener and the other is setOnItemLongClickListener, but only the first one is working, it 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;
}
}
    
asked by anonymous 26.01.2015 / 20:34

3 answers

1

I just forgot one line that has already solved everything:

dialog.show();

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

    
27.01.2015 / 01:00
1

You have to set setOnItemLongClickListener () in the 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 must use custom XML) must have android: longClickable = "true" , as well as (or you can use the listView.setLongClickable (true); method). But this way, you can have a list with just a few items that respond to longclick.

    
26.01.2015 / 21:59
-1

Dude, try to use Log in your application, and then know where the error is occurring! However, create a method to put AlertDialog, and so you call it inside this method!

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

    
26.01.2015 / 20:52