1
someone knows how to pull an ID from a Listview item through the SetOnItemLongClickListener
and could tell me if this method would work with an item being displayed from the database??
The code of the Adapter:
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;
}
Maybe that’s it (I can’t test at the moment), to be clear, what I want, I want to take the ID of the LV object in the database (through the long click), to be able, for example, to use this ID and display the name and value of the object (which is in the database) referring to the ID in another Activity in two Textview, gave to understand or remains confused??
– Diogo Alves
I believe then that what I have mentioned is the right way. You want to get the id of the object, which is the key Primary in the database, not its position in Listview. Which Adapter you are using in your Listview?
– Tássio Auad
After the method
getItemId
has been implemented it is now possible to take directly the value of theid
through the parameter passed to the methodonItemLongClick
.– ramaral
Okay, Tássio, I’ve been through the code here and my code is apparently identical to yours, I’m using the Basedapter. It seems to me that this is the same way, so in this case, as I could do for that click over, it stores the id in a variable so that in another Activity, it can be passed as parameter to my method
buscarObjetoPorID(variavel)
?– Diogo Alves
To point out, I did it the way you did and then in the method I set the
Objeto.getId()
and he returned me the first item regardless of which I click.– Diogo Alves
Yes, by this way you can get the id (Primary key of the table) of your object. About your new problem, you would need to see part of your Adapter code to understand what is happening. You could show?
– Tássio Auad
Of course, after your demonstration and the comment of Ramaral, I changed the code portion of the ID and put that
getItemId(position)
to see if it worked and in Listview, below each object, the referent ID appears, but I still don’t know how to get the ID, once I tried to instantiate this adapte and it didn’t work.– Diogo Alves