There is output to the ListView from the database. How to make it so that by clicking on an element from the list, it is not the position that is transferred to the next activity, but the id of the element that was in its database.

For example:

1 элемент (id из db - 12 --- по нажатию на этот элемент передавало не position(0),а id 12 2 элемент (id из db - 43)--- по нажатию на этот элемент передавало не position(1),а id 43 3 элемент (id из db-145)--- по нажатию на этот элемент передавало не position(2),а id 145 и тд. 
  • 2
    What kind of nonsense? - AseN

3 answers 3

 @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { long id = parent.getAdapter().getItem(position).getId(); } 

something like this, depending on what data you are transferring to the adapter


  public class ContractDB { public static class Groups { public static final String TABLE_NAME = "groups"; private String name; private long id; private long id_sprint; private int min; private int max; // geters and seters ........... public class GroupsColumns implements BaseColumns { public static final String NAME = "group_name"; public static final String ID_SPRINT = "id_sprint"; public static final String MIN = "min"; public static final String MAX = "max"; } } // others tables } 

and in the adapter I work directly with the Groups objects

  • And how to set the list of indicators so that they can be transferred - smalser
  • there is a similar task in a flowing project, described contracts, my adapter and I load objects into the listview, I don’t know how correct it is, but it’s convenient to work - gadfil
  • Tolley, I did not correctly explain the problem, or you did not understand me correctly. First you need to build a list with the necessary IDs (from db), and then when you click on the list to get this ID, well, or something like that. Here is my Activity: Working with the database in Android from the list you need to go and transfer the clicked element from of the list, and there select a list by the transmitted id from b - smalser
  • So I understand no answers to my question ?? - smalser
  • In the database, there is data that has _id (so far without it), having received this data, we output it in the listview, by clicking on an element in the listview we want to know which _id corresponds to this element? do you understand correctly? - gadfil

If we are talking about SQLite and an adapter based on SimpleCursorAdapter , then the fourth argument of the callback that returns a click on the item's list is the ID from the database

 @Override public void onItemClick(AdapterView<?> parent, View .view, int position, long id) { } 

long id - ID of the record in the database by item in the list of which clicked

    The question is solved very simply "in the forehead," the author has clouded the situation more.

      ЛистВью.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View itemClicked, int position, long id) { cursor = Адаптер.getCursor(); переменная = cursor.getInt(cursor.getColumnIndex("_id")) } }); 

    Nobody needs to assign anything, but to work with this same _id everywhere.