Suppose we have this example:
http://techdroid.kbeanie.com/2009/07/custom-listview-for-android.html
with source code available here:
http://code.google.com/p/myandroidwidgets/source/browse/trunk/Phonebook/src/com/abeanie/
How can we modify a mobile phone number once clicked on the list item?
In the method onItemClick() get the PhoneBook element corresponding to the position(the position parameter) of the row clicked, update the value and then notify the adapter that the data has changed with a call to the method notifyDataSetChanged():
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long index) {
// make the adapter a field in your class (or final)
PhoneBook element = (PhoneBook) adapter.getItem(position);
//modify the PhoneBook element
element.setPhone("555-555-555");
// notify the adapter that something has changed
adapter.notifyDataSetChanged();
showToast(listOfPhonebook.get(position).getName());
}
});
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'm new to android and my scenario is as follows:
I have a list view that is bound to an array list of type Department (a simple object with id and name properties). My goal is to have the list view display the department names (done by overriding the toString() method of the Department class) and when I click on a list view item, I want to pass the ID property of the selected item to another Activity. So, how do I get the ID property on item select?
You should use this:
yourListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
}
});
Here in onItemClick you will get the position of the selected item and you can get your id like this:
yourArrayList.get(position).getDepartmentId();
You can use Get Tag / Set Tag in android for this purpose.
Following example will help :
setTag() and getTag() on ListView
You can override onItemClick method and get the position of your view
public abstract void onItemClick (AdapterView<?> parent, View view, int position, long id)
Callback method to be invoked when an item in this AdapterView has been clicked.
Implementers can call getItemAtPosition(position) if they need to access the data associated with the selected item.
Parameters
parent The AdapterView where the click happened.
view The view within the AdapterView that was clicked (this will be a
view provided by the adapter)
position The position of the view in the adapter.
id The row id of the item that was clicked.
I have a list view and I've implemente filtering.
Lets say I have items A, B and C. If I type B in the filter box, only item B will be displayed and it is the position 0 of the list (before it was in position 1). So when I call the onClick item, I get the the id/position 0, which leads to displaying details about A instead of B.
This is the onclick code:
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Poi poi = pois.get((int)id);
goPOIDETAIL(poi);
}
});
id and position have the same value.
is there a way to get the original position, or get some other value indicating the real item that I clicked?
Thanks
flashsearchList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Integer temp=flashSearchNameMap.get(adapter.getItem(position));
navigateSearch(temp);
}
});
(adapter.getItem(position) will return you the exact list name and in flashSearchNameMap i have stored names and position at beginning from oncreate before applying filtering.So you can get exact position by this
I think the problem is in the way you manage your filter. You should get the object with selected id not from the original List (or array) but from the filtered one.
I used something like it in this post from my blog. Hope this help you
ID and Index are not the same. Of course, you can return item index in getItemId() method of your adapter, but don't expect your items to be identified correctly by this method if you do.
Try providing unique ID for each of your items. The idea is somewhat similar to ID of each record in the database, which never changes (and lets you reliably identify each record), and it is easily implemented when you get your data from database.
But if your items don't have unique IDs, and you don't want to bother providing them, there's another approach (see this example code for Adapter below):
public MyAdapter extends BaseAdapter {
private List<Item> items;
private List<Item> displayedItems;
public MyAdapter(List<Item> items) {
this.items=items;
this.displayedItems=items;
}
public filter(String query) {
if(query.isEmpty()) {
displayedItems=items;
} else {
displayedItems=new ArrayList<Item>();
for (Item item : items) {
displayedItems.add(...) //add items matching your query
}
}
notifyDataSetChanged();
}
//...
//NOTE: we use displayedItems in getSize(), getView() and other callbacks
}
You can try:
#Override
public boolean hasStableIds() {
return false;
}
in your adapter
if you are using datbase you have the _id key that you can load in a filtered list as invisible field. Once you click on the item you can query data with _id key.
If you aren't using a database you could add a hidden id element in your row element as well.
I have a listview and I want to able to select one of the entries here in order to delete it. For example, my entry in listview looks like this:
John Smith
john#hotmail.com
4857394
NewYork
So I want to select this one and delete it. I find this set OnItemClickListener in order to select it. I have delete function. I just need to select name "John Smith" when I click this entry. I don't know what I can write inside function here.
listContent.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id)
{
listContent.getItemAtPosition(position);
}
});
listContent.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
listContent.getItemAtPosition(position);
listContent.removeViewAt(position);
adapter.notifyDatasetChanged();========> your adapter
}
});
You may remove an item as below:
adapter.remove(listContent.getAdapter().getItem(arg2));
Note: Since you are removing directly from adapter object, so you don't need to worry about calling notifyDataSetChanged() method for the changes to take effect.
On ListView you are providing data through some collection of data and index of individual data in list will be same as it is shown in screen. So onItemClick method you are also getting int arg2 , which signifies selected list. Same index you can use to delete element from List and write this deletion operations in runOnUIThread method so that you will not find any glitches.
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