Android, pick id of an item from a listView by long click

1

Does anyone know how to pull an ID of a ListView item through SetOnItemLongClickListener and could you tell me if this method would work with an item being displayed from the database ??

The Adapter code:

public class ObjetoAdapter extends BaseAdapter {

private List<Objeto> objeto;

private Context context;

public ObjetoAdapter(Context context, List<Objeto> objeto) {
    this.objeto = objeto;
    this.context = context;
}

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

@Override
public Object getItem(int arg0) {
    return objeto.get(arg0);
}

@Override
public long getItemId(int position) {
    return objeto.get(position).getId();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View rootView = LayoutInflater.from(context).inflate(R.layout.lista_objetos, parent, false);

    TextView tvID = (TextView) rootView.findViewById(R.id.tvID);
    TextView tvNome = (TextView) rootView.findViewById(R.id.tvLvNome);
    TextView tvValor = (TextView) rootView.findViewById(R.id.tvLvValor);

    Objeto objetoDaVez = objeto.get(position);

    tvID.setText("ID: " + getItemId(position));
    tvNome.setText(" Nome" + objetoDaVez.getNome());
    tvValor.setText(" Valor: R$" + objetoDaVez.getValor());

    return rootView;
}
    
asked by anonymous 01.06.2015 / 00:14

2 answers

2

It was a bit difficult to understand, but if I understood correctly, you want to get the id of your object and not simply its position in the listview, as it is received in the Listener parameter. So, come on ...

In the OnItemLongClickListener interface we have the onItemLongClick method that takes an instance of AdapterView as its parameter. Through this instance we can capture the adapter used in the listview through the adapterView.getAdapter() command. Having the adapter in hand we can invoke the adapter.getItemId(int posicao) method, which we usually override when the adapter is customized. If you override this method on your adapter, you can have the id, whatever it is, of your object referenced by the listview item that was clicked and not necessarily the position of the item in the list.

listView.setOnItemLongClickListener(new OnItemLongClickListener() {

    @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View v, int pos, long id) {
        long codigoDoObjeto = adapterView.getAdapter().getItemId(pos);
        return true;
    }
});

Here's an example of the custom adapter:

public class MeuObjetoListListViewAdapter extends BaseAdapter {

private Context context;
private List<MeuObjeto> meuObjetoList;

public SistemaListViewAdapter(Context context, List<MeuObjeto> meuObjetoList) {
    this.context = context;
    this.meuObjetoList= meuObjetoList;
}

@Override
public long getItemId(int position) {
    return meuObjetoList.get(position).getCodigoIdOuPKQualquer();
}

//Demais métodos ...

}

If not, I apologize, but that's what I understood =)

    
02.06.2015 / 15:28
1

If the match associated with your ListView is a CursorAdaptar parameter the id passed to the onItemLongClick() method, > OnItemLongClickListener , represents the value of the _id field of the record associated with the clicked row.

private long listItemPressedId;
minhaList.setOnItemLongClickListener(new OnItemLongClickListener() {

    @Override
    public boolean onItemLongClick(AdapterView<?> adapter, View v,
            int pos, long id) {
        listItemPressedId = id; //id do registo associado à linha clicada
        return false;
    }
});  
    
01.06.2015 / 12:14