I want to get the index of the Selected Item ( Clicked ) in a ListView.
Can any one please tell me?
Thanks
Deepak
You need to use an AdapterView and its onItemClick method :
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> list, View v, int pos, long id) {
yourAdapter.getItem(pos);
}
});
Related
I have 20 buttons in vertical layout in a scrollable window. While click on a button I want to know the position of that button on screen.Some times I click the button after a scrolling.I want the exact position at that time too.What is the code for that.please help me.
Never use 20 buttons inside layout. Use listview with button and use onitem click of listview and get button position:
list view onitem click:
listId.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> list, View v, int pos, long id) {
-------Here position displays **pos**----
}
});
Refer this example for a nice listview sample
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
System.out.println("this is position"+lv.getItemAtPosition(arg2)
}
});
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
I am new to android programming. I need some help here. I have used this site example on creating a listview. What I want to achieve is when the user clicks a particular row, the row clicked will perform its respective action. (Eg. When clicked row 1 will show a toast. When clicked row 2 will direct the user to another new view, etc.)
I have set a OnItemClickListener to the listview but am lost on how to do it. Any help will be appreciated. Thanks!
Below is my code:
.......
final ListView list = new ListView(this);
list.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(Adapter<?> arg0, View v, int i, long l){
// At implementation
}
});
.......
It looks like you're on the right track, everything should be clearer with onItemClick's parameter names :
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(), "Clicked row " + position,
Toast.LENGTH_SHORT).show();
}
So you actually get the position of the item clicked, and can have a different action for each item.
To get the index of the selected item:
int selected_item = myListView.getPositionForView(view);
or to get the string:
String chosen_item = (myListView.getItemAtPosition(selected_item).toString());
If your class is extending ListActivity replace "myListView" with "this".
I have a simple ListView that gets populated using an array of SimpleAdapters.
How can I detect selection on this ListView?
Sample code is appreciated...
Try ListView.setOnItemClickListener():
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
... do something based on position ...
}
});
How to set color to the selected item in listview and at the same time the other items should not be selected (in android)?
Thanks in advance,
Neha
Go with registering the onItemClickListener() on your listview and get the view and set colot like this
listview
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView parent, View view,
int position, long id) {
view.setBackgroundColor(color);
}