How can I detect selection on an Android ListView? - android

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 ...
}
});

Related

Android OnClick in ListAdapter

How to implement OnClickListener on a ListAdapter? My data is coming from sqlite. I want to show half of the data in the first activity and the reamining in the second activty using OnClickListener in ListAdapter.
You need to implement the AdapterView.OnItemClickListener interface and call setOnItemClickListener(AdapterView.OnItemClickListener) on your ListView.
If you're using ListFragment oder ListActivity, you could also override their onListItemClick(ListView lv, View v, int position, long id) method instead.
You should use set ItemClickListener on ListView with help of "setOnItemClickListener".
If you want to catch clicks in adapter, it will be better to use RecyclerView.
Example - https://stackoverflow.com/a/24933117/2687734
What about:
.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
adapter.get(position); //get your custom model
}
});

Retrieve data from SQLite in a ListView

Hi guys so I have this doubt. I've created an app that stores data in a SQLite and its displayed on a Listview in the Main Activity. What I'm trying to do is once I click one item from the list it should open an image from sd card. So my question is how can I use onClick here since I don't know the longitude of the list? And may I include the onclick for the list in the switch I've got?
UPDATED:
I forgot to say that that I would like to open an image stored in the sdcard when I click a item of the list.
That's what I've got for now:
Contact_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor cursor = (Cursor) parent.getItemAtPosition(position);
String lat = cursor.getString(cursor.getColumnIndexOrThrow("lat"));
Toast.makeText(getApplicationContext(), lat, Toast.LENGTH_SHORT).show();
}
});
All comments or suggestions are welcomed. Thanks!
Use setOnItemClickListener() method:
Contact_listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, final int selectedIndex, long arg3) {
}
);

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 set color to the selected item in listview and at the same time the other items should not be selected (in android)?

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);
}

getting the index of clicked item in a listview

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);
}
});

Categories

Resources