How to get _id from listView (database) [android] - android

I have a problem with receiving _ID value when an item is clicked on listView.
I have this code:
List<SavedSearch> values = mydb.getAllSavedSearches();
ArrayAdapter<SavedSearch> adapter = new ArrayAdapter<SavedSearch>(this,
android.R.layout.simple_list_item_1, values);
//adding it to the list view.
obj = (ListView)findViewById(R.id.listView1);
obj.setAdapter(adapter);
obj.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
And my problem is that i want in onItemClick to somehow get the _ID value from database of clicked item in listView. int position and long id both are returning just position on the list.
Thanks for help, and I can say that any of previous topics helped me.

If you're using a database I would suggest you do not use an ArrayAdapter and instead use a CursorAdapter. Then simply call the method getItemId() from your CursorAdapter to retrieve the id of an item at a given position. So, do this:
CursorAdapter adapter = new CursorAdapter(Context yourAppContext, Cursor yourDBCursor, false);
obj = (ListView)findViewById(R.id.listView1);
obj.setAdapter(adapter);
obj.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
adapter.getItemId(position); // This is the _ID value
}

This is just the suggestion. I am not sure this will solve the issue.
Method 1:
Whenever you load the listView just set the id for the row and you can get the id from onItemClikListener method
Method 2:
Add an texView (set visibility gone) and set text as your id and get id using getText() method
Note
Don't use position this is always changing when the listview is reCycling.

Related

Get specific id from a listview

I have a ListView which is implemented using a customs adapter. For making the adapter i am using a holder class. The class has various TextViews and ImageViews as well as an Int variable id to store the id being fetched from the database. Now when i click the particular list i want to get the id so that using it I can further display the information on a new activity. The id is not meant to be displayed in the ListView. How can i get the id from onItemClickListener()
You can set a tag to a View (.setTag()) and then retrieve it, it happens on getView() method inside your custom Adapter
Here is a sample code:
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String transactionId = ((TextView) view
.findViewById(R.id.tvTID)).getText().toString();
handler.getTransactionDetails(callback, transactionId);
}
});
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
YourObject selected = adapter.getItem(position);
Then you can send the selected list item (Object) to your new activity
Intent mIntent = new Intent(ThisActivity.this, NewActivity.class);
mIntent.putExtra("list_selected", selected);
startActivity(mIntent);
as well as an Int variable id to store the id being fetched from the
database.
You didn't provide more infomation about that id (i don't know exactly what it presents) but to make it simple you can set this id for each widget in ListAdapter via setTag() method and then simply retrieve it from your onItemClick() method.

Android - Getting database ID from ListView selection

I have a ListView lv which uses a Cursor c from an SQL database to populate it. When an item is selected however, I need to get the ID of the row. How can I do this?
I assume that you are using a SimpleCursorAdapter (or similar), so I would use the OnItemClickListener:
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// id references the SQLiteDatabase _id column
}
});
Here's how it worked out for me. In an other class, I have created a method of List type and added the column id & the name of the item in the list from the sqlite database.
Here's the piece of code to retrieve the column id of the selected item.
public List<String> list;
adapt = new MyAdapter(this, R.layout.list_inner_view, list); //custom adapter
listTask.setAdapter(adapt);
listTask.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View v, int position, long l) {
Integer id = list.get(position).getId();
//position is the position of the item in the list.
}
});
Result: the item is at 3rd position in the list. Its database column_id (auto increment) value is 12 because I have deleted previous items. So the result you expect is 12, which is the value of variable id.
If you don't understand this fully, it may be because I haven't posted my entire code. But I'll help you out with your problem.

how to get individual associated id's of listview elements when clicked

I am displaying an arraylist through a listactivity. Also, i have associated id's with these arraylist elements which i can get together in a hashmap maybe. But the problem is i want to obtain that particular id of the element when it is clicked. How to achieve that. Any help is appreciated.
You can return the id based on the clicked position if you an implementation of the getItemId(position) method in your adapter. The id you return here will be the long id argument in the onListItemClick(...) method of your ListActivity.
getItemId(int position) is a member of the ArrayAdapter class. The ListActivity will call that function from the adapter of yourListView. All you have to do is to provide the implementation, the rest will work out just fine.
http://developer.android.com/reference/android/widget/ArrayAdapter.html#getItemId(int)
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
long pos=lv.getItemAtPosition(position);
//do your stuff here
}
});
if you are using your own ArrayAdapter, you can implement the getItem(position) method..
The answer that worked for me is as follows. Hope it maybe useful for others.
I had the data in Arraylist> testdata. I passed it like this :-
this.setListAdapter(new SimpleAdapter(Grammar_tab_start.this, testdata ,
.layout.tab_single_item, new String[] { "module_name"},
new int[] { R.id.from}));
ListView lv = getListView();
to single list item on click
lv.setOnItemClickListener(new OnItemClickListener()
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
Log.i("check","Module item clicked");
HashMap<String,String> pos=(HashMap<String, String>) lv.getItemAtPosition(position) ;
Log.i("check","----" + position + "id" + id +"actual is "+ pos.get("module_id"));
}
I used the lv.getItemAtPosition() function to retrieve the whole object that was clicked. And then took th eid part from it. Cheers.

Getting list row id in list view

If I have a listview being populated with webservices and the elements added in each row have a unique id stored in the array list which utilizes a hashmap to store data,thn what would be the simplest way to obtain the id of the data,that was clicked in the row..
I am using a base adapter on my list,
Any help would be greatly appreciated.
set on item click listener on listview. it will return position of the clicked item in the adapter (equivalent to position in the array list). you can fetch the id using that.
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),String.valueOf(position), Toast.LENGTH_SHORT).show();
}
});
Third parameter is Position of Item in the list.
use this method listView.getItemAtPosition(position). It got a position you can use
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
// Get item at position like this:
// listView.getItemAtPosition(position));
}
});
if you use the adapter.getItem(position) function you'll be able to get the item stored in the ListView in that specific position.
update: this is of-course in the setOnItemClickListener

How to get row view data in the listview

this example: http://saigeethamn.blogspot.com/2010/04/custom-listview-android-developer.html .There are 3 textview ids at custom_row_view.xml. There are 3 row data inside one position listitem when using onListItemClick. how to extract these data? how to use the id? Anyway to get row view data in the list view when the list item is clicked
[protected void onListItemClick(ListView l, View v, int position, long id]?
The parameter View v itself is the row view. You can get the attached data by calling v.getTag(). Should set it earlier in getView of adapter using v.setTag(Object)
Depends on the kind of data. For example id does contain the _id from a database table row that was set with one of the CursorAdapters. Usually this is the PK of that database table row.
listView.getAdapter().getItemAt(position)
gets you the object bound to the view V
I would like to recommend you not to get data from the view, instead use the ArrayList you have used to set data to the Adapter of the ListView.
In the example which you have pointed out, you are using an ArrayList of HashMap. So for an example..
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// arrayList is the variable which you have used as a list in your SimpleAdapter
hashMap = arrayList.get((int)id); // you need to typecast 'id' from long to int
value = hashMap.get(KEY);
}
});

Categories

Resources