How to get the Primary Key ID of the information from a line clicked on Listview?

Asked

Viewed 19 times

0

Hello, I am new to Android programming and am doing a project of which I need to do a CRUD with a litView, anyway, I already have it displaying the data, however I would like to know how I do to get the primary key ID of a record for example, which is being displayed on a listview line from which I clicked. Not the position of the element in listview.

I have already tried to search several examples on the internet and also tried in the English Stack, because they all present methods that take the value of the element’s position in the listview and not the value of Id of the Bank’s information, which does not solve my problem.

For to clarify, I will illustrate a situation: It has two factors, one is the listview line (e.g., position 1); And another is the database record line (e.g., value ID 5) that is being displayed at position 1 of listview.

For I would like to know how I do to get the value 5 (from the Bank ID) by clicking at position 1 of listview.

One of my attempts:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
             @Override
             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String selectedItem = (String) parent.getItemAtPosition(position);
                textView.setText("The item you clicked is : " + selectedItem);
             }
          });



listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
             @Override
             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            
                textView.setText("The item you clicked is : " + id);
//Or
                textView.setText("The item you clicked is: "+position);

             }
          });

All of them apparently take the position value of the list line, not the Sqlite record ID.

1 answer

0


Oops, beauty? I don’t know if you’re using a custom Adapter, but I’d make my listview with a custom Adapter (here is an example of how to do).

The list to be displayed would be an Item (List) type list (Item is a class to represent the items, I gave the Item name but it can be the name you want) and, in this class, would have an attribute to represent the ID in the database.

public class Item{
    private int id;
    private String conteudo;

    public Item(int id, String conteudo){
        this.id = id;
        this.conteudo = conteudo;

    }

}

When you fill in the list, you add the database ID for each item. In custom Adapter I would make a function to return me the item in the style informed position:

public Item getItemAtPosition(int position){
    return list.get(position)
}

And the click on the item would look something like this:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
             @Override
             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Item item = adapter.getItemAtPosition(position);
                 textView.setText("The item you clicked is : " + item.id);
             }
          });

With this you could get the database id of the item that was clicked

Browser other questions tagged

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