I'm having somewhat of an issue in an application I'm developing.
So, all my tables have _id fields for primary keys, and I use SimpleCursorAdapter to bind them to ListViews and Spinners.
What I wanted to know is how can I make the ListView or Spinner selected item have the same ID as the corresponding row?
The strange thing is that this works with the ContextMenu, which I am using straight of the NotePad example:
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
info.id
This ID field IS the same as the RowID on the table, and I can delete items fine, but when I try something like this:
getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View view,
int position, long arg) {
Intent i = new Intent(getApplicationContext(),
FuelingList.class);
long id = view.getId();
The ID field is some random rubberish.
So my question is, in the first code bit, what Id is the AdapterContextMenuInfo getting and how can I retrieve it in other parts of my code?
Since you are using a SimpleCursorAdapter, the onItemClick is passing the database row id in to you..
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg)
The long arg part is actually your row id from the database.
So your code should be:
getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View view,
int position, long arg) {
Intent i = new Intent(getApplicationContext(),
FuelingList.class);
long id = arg;
Since you know you are using a SimpleCursorAdapter, then in your onItemClick method, you can call
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
String value = ((SimpleCursorAdapter) adapter.getAdapter()).getCursor().getString(COLUMN_INDEX);
}
Where COLUMN_INDEX in the column that you want to fetch from the currently select row in the cursor.
Alternately, calling
adapter.getAdapter().getItem(position)
and then casting that to a Cursor, works as well.
NOTE 1: The AdapterView is actually your ListView.
NOTE 2: Since you using a SimpleCursorAdapter then getItem(position) on the Adapter returns the Cursor positioned at the row your specifed
NOTE 3: When you have Cursor, you can fetch data by providing the column index (0 based) or by using cursor.getString(cursor.getColumnIndex("COLUMN_NAME"))
NOTE 4: As per Barak's comment the arg parameter is the value from the _ID field when using a CursorAdapter, if all you need to know is the row _ID, then just use the arg value.
Can you store the ids of rows in an arraylist and take the id of the row from corresponding position of arraylist. This worked for me..
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Intent intent=new Intent(this , Details.class);
Bundle extras = new Bundle();
extras.putLong("ID", IDList.get(position));
intent.putExtras(extras);
startActivityForResult(intent, SHOW_DETAILS);
}
You can use listview.setonitenclicklistener... Hope it helps
Related
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.
I have an activity that lists all rows of a SQLite table. Initially, as I populate the table, clicking on items will open up each item as expected. However, the database is supposed to be modified and if I delete some rows, the list gets out of sequence (the items' SQLite _id and position in the list are mismatched). Is there a way to retrieve the item's _id from the SQLite table and pass that through the intent?
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Intent i = new Intent(MainActivity.this, FilmEditActivity.class);
Log.d(TAG, String.valueOf(id));
i.putExtra(FilmEditActivity.EXTRA_FILM_ID, position+1); //sql starts at 1; java at 0
Toast.makeText(getApplicationContext(),
"position:" + String.valueOf(position) + "; id:" + String.valueOf(id),
Toast.LENGTH_SHORT).show();
startActivityForResult(i, 0);
}
});
If you are using the CursorAdapter then the param id passed to the onItemClick() is exactly the SQLite "_id".
There is also another way to get "_id"
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Cursor cursor = (Cursor) adapter.getItem(position);
int id = cursor.getInt(cursor.getColumnIndex("_id"));
}
I'm assuming you work with a CursorAdapter which is certainly the best way to populate a list from an SQLite database.
To retrieve the primary key of a clicked entry you can do the following:
int adapterPosition = position - listView.getHeaderViewsCount();
Cursor cursor = (Cursor) yourAdapter.getItem(adapterPosition);
int id = cursor.getInt( cursor.getColumnIndex("_id") );
Pass this id to your activity and you should be all set.
The parameter id passed into the onItemClick is what the adapter returns in a getItemId(int) calls which should be the same as the _id in your database. The documentation states:
Get the row id associated with the specified position in the list.
http://developer.android.com/reference/android/widget/Adapter.html#getItemId(int)
Row id isn't explicitly the primary key but it's probably safe to assume that they are identical (checking the CursorAdapter.getItemId(int) code confirms that assumption).
Use The id parameter instead of the position parameter, the inbuilt class already helps in getting the id from the source data. after implementing the
onItemCliclListener
in the onItemClick function, use the id paramenter as show in the code below
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// this will be executed in respective of id
Intent i = new Intent(this, ViewLyrics.class);
Bundle sendid = new Bundle();
sendid.putLong(database.KEY_ROWID, id);
i.putExtras(sendid);
startActivity(i);
}
I have generated a list of customers. On click this should open edit view to edit the customer. Here the parameter should pass the _id of the row according to stored in the database. But everytime passing it's position in the list. So the edit view is opening wrong data. Please help.
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
Uri customerEditUri = Uri.parse(CustomerBean.CONTENT_URI + "/" + id);
customerEdit(customerEditUri);
}
});
Answer:
Thank you all. Your comments helped me to solve this. I have created following function inside my CustomerObject class:
#Override
public String toString() {
return this.name;
}
After that created an array of CustomerObject in activity like following:
List<CustomerObject> customers = new ArrayList<CustomerObject>();
Created ArrayAdapter by following:
adapter = new ArrayAdapter<CustomerObject>(this, R.layout.list, R.id.customer_name, customers);
Finally called setOnItemClickListener() like this:
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
CustomerObject custObj = adapter.getItem(position);
Uri customerEditUri = Uri.parse(CustomerBean.CONTENT_URI + "/" + custObj.pkid);
customerEdit(customerEditUri);
}
});
You will have to set the ID you would like it to return in the adapter, the List View Adapter that you used to bind data to the ListView.
If I am not wrong, the method is in the adapter class under the following method name:
public long getItemId(int position) {
return myitem[position].getId();
}
Returning the appropriate ID will get you the results you wanted.
I believe that the "long id" is not the record id but the internally generated view id.
If you want to get back to the datasource id then you need to use position and something like:
// Assuming datasource is an ArrayAdapter<Customer>
Customer customer = customerAdapter.getItemAtPosition(position);
// then you can do
Uri customerEditUri = Uri.parse(CustomerBean.CONTENT_URI + "/" + customer.getId());
customerEdit(customerEditUri);
Replace id with position.
Use
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
Uri customerEditUri = Uri.parse(CustomerBean.CONTENT_URI + "/" + position);
customerEdit(customerEditUri);
}
})
In my opinion, through the position, you can get row item with adapter's getItem(position).
So, the position mean the data position in the adapter.
For the id parameter, I think it is a help method, as you know, the data in adapter always is a recorder. general speaking, a recorder should have an id column(something like the database id). when coding, you can get the item through position, then get the item's id(if the item has id). but you can get such "id" directly with "id" parameter.
By the way, if you want use the id parameter, you have to implement the getItemId() method in adapter. the default implement in ArrayAdapter is just return the position.
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.
I have a ListView that gets filled from a cursor (using rawQuery) that I need to get the text of the selected item from on click.
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent mViewChaptersIntent = new Intent(this, ViewWeb.class);
String s = ((TextView) l.getItemAtPosition(position)).getText().toString(); // Tried this, didn't work.
mViewChaptersIntent.putExtra("extension", s);
mViewChaptersIntent.putExtra("itmClicked", String.format("%d", id));
startActivity(mViewChaptersIntent);
}
But I'm not sure about the right way to do it. The getItemAtPosition that I've seen in other posts doesn't seem to work...
getItemAtPosition() should return you a Cursor that is positioned at the specified row. You will then need to call getString() to retrieve whatever column you are looking for.