Android, pick up id of an item from a listview through long click

Asked

Viewed 3,625 times

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;
}

2 answers

2

It was a little difficult to understand, but if I understand correctly, you want to take 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 Adapterview instance as parameter. Through this instance we can capture the Adapter used in listview through the command adapterView.getAdapter(). Having the Adapter in hand we can invoke the method adapter.getItemId(int posicao), which we usually overwrite when the Adapter is customized. If you overwrite this method in your Adapter, you may 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;
    }
});

I’ll put here 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 =)

  • 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??

  • 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?

  • After the method getItemId has been implemented it is now possible to take directly the value of the id through the parameter passed to the method onItemLongClick.

  • 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)?

  • 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.

  • 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?

  • 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.

Show 2 more comments

1

If the adapt associated with your Listview for a Cursoradaptar the parameter id passed to the method onItemLongClick(), of the interface Onitemlongclicklistener, represents the value of the field _id of the register associated with the clicked line.

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;
    }
});  
  • Speak, Ramaral, I appreciate the attempt, but it did not work (The only option that appeared was ListItem and it didn’t even work out), let’s see if explaining you can help. Each line of the LV displays data from a database, so I wanted it to click on the item(certain data), take the item ID, check the database of who this ID was and go to another screen to display only this item. In this method of displaying on the other screen I used an existing ID and it worked, but I want that along click, it takes the ID of the selected, I could understand??

  • What do you mean "The only option that appeared was Listitem..."?

  • When I went to do the syntax you recommended (with ListItemPressedId), the closest it came was ListItem and nothing else of the kind, understood??

  • listItemPressedId is the variable that will store the id of the clicked list item. You must declare it first as private long listItemPressedId

  • Oh yes, I understood, but in this case, this variable of the long type would be used in a method that would need an int (the bank ID), would have to do a conversion from long to int normally or with long is different (I don’t know much about this type)?

  • No need to convert, the type long is a int with a wider range of values (larger numbers). Please note that the code posted assumes that your Adapter inherits from Cursoradpater (or Simplecursoradapter) what is advised when working with data coming from Sqlite.

  • Oh yes, but my database is not Sqlite but Mysql (it runs from the internet), has some problem or would work the same way?

  • How do you access the bank?

  • I use a Webservice, the strictmode.threadpolicy for the app to remotely pull the data, a method to search for all objects in a database table and adapt the Listview for each row to appear an object and that method I want to execute would be a SELECT nome, valor WHERE ID = X (If that’s what you asked).

  • Like your Adapter is not a Cursoradapter will have to implement the getItemId() in his Adapter. see the answer from Auad.

Show 5 more comments

Browser other questions tagged

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