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;
}
}
Just return
true
ofonItemLongClick
. For better understanding: http://developer.android.com/reference/android/widget/AdapterView.OnItemLongClickListener.html.– Wakim