I am using a list view containing check boxes. Could you please help me how to identify which check box has the user selected? Basically I want different functions to be performed when a checkbox is clicked or a list item is selected similar to Android alarm clock.
does this help you:
how to display a list of checkboxes using dynamic data
That is how I did it with dynamic data.
Basically, in your listView class override:
#override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
boolean isClicked = l.isItemChecked(position);
// do something
}
Related
I am trying to change image of a list item on click.I have tried using view but on using it I am getting multiple items with changed image even though I just click on a single one.I am using a simplecursor adapter and code is same as given in this question:-changing image on listview at runtime in android.
And also I don't want to use a custom adapter.It will be really helpful if someone helps me in solving this problem just through simple adapter.
Current code
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
/*
imageView=(ImageView)l.getChildAt(position).findViewById(R.id.PlayPause);
imageView.setImageResource(R.drawable.pause);
*/
}
1- Do not use get child at when u are directly getting the child view as "View v" in the event.
2- you just need to call invalidate() method on the view once u set the new background resource.
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
imageView=((ImageView)v)
imageView.setImageResource(R.drawable.pause);
imageView.invalidate();
}
While you should make your list using a simple cursor adapter with an imageview ---- :-
http://thinkandroid.wordpress.com/2010/01/09/simplecursoradapters-and-listviews/
refer this link for that.
Why not use
ListView.OnItemClick(OnItemClickListener{})
http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html
In my project I use a custom adapter to define the items of a ListView. So I want to define special behavior of item in ListView, which will be depend on the value of the field in adapter. Like this :
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
}
So, here, in this method I want to get access to the element of adapter to get the value from it, number of which is defined by the int position. So, what is code to do this?
you can simply cast it if you're confident enough it is always your adapter that is used:
protected void onListItemClick(ListView l, View v, int position, long id) {
((MyAdapter) l.getAdapter()).myMethod(position);
}
If you are defining an custom adapter you can save a reference to the adapter in the activity that registers the onClick behaviour. You now can call getItem on the Adapter, if you have implemented the getItem method properly.
You can get selected item(object) from list. Lets say your custom adapter containing Restaurent object per row, and your listView name foodJntListView, then
Restaurent rest= (Restaurent) foodJntListView.getSelectedItem();
will give you the selected item(object).
I use a CustomCursorAdapter to populate a ListView. Also the onitemclicklistener works
for reading text from the clicked ListItem.
I only display partial data from the cursor used..
How can i access the corresponding item in the database , when a listitem is clicked
so that i can display all the data in a toast or dialog box.
What is your Object about, what do you want to do on list item click?
Posting the details will help us to answer but here is a simple code to show a tost:
protected void onListItemClick(ListView l, View v, int position, long thisID)
{
super.onListItemClick(l, v, position, thisID);
// Get the item that was clicked
String keyword = (String)this.getListAdapter().getItem(position);
Toast.makeText(Activity.this, keyword, Toast.LENGHT_SHORT).show();
}
Can anyone tell me how I can find the relevant list item id for a touch event associated with a listview?
I want to set the background of a list item to a different color when the user presses down on it, and change it back when they release. I prefer to do this with code and not the background setting.
I already have an onclicklistener, but want to get this ontouchlistener working as well.
I know this is an old question. But if anyone else had this same question the solution is the pointToPosition method found in classes that derive AbsListView.
public boolean onTouch(View arg0, MotionEvent arg1) {
// ... other logic ...
int lListViewPosition = mYourListView.pointToPosition((int)arg1.getX(), (int)arg1.getY());
// get the item id
int lItemId = mYourListView.getItemIdAtPosition(lListViewPosition);
// or get the actual item
Object lYourItemObj = mYourListView.getItemAtPosition(lListViewPosition);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
Object o = this.getListAdapter().getItem(position);
//do Magic
}
Hope this helps!
I am stuck on this! I have a listView populated by a a cursor. My question is how can i get the specific item information from the cursor when a certain position in the listview is selected. For example, i select the third index in the listView, I want the information associated with that index from the cursor. How can I do that? Thanks in advance
Implement the onListItemClicked() method like so:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
yourCursor.moveToPosition(position);
yourRowId = yourCursor.getLong(0);
}
Your Activity must extend ListActivity for this to work as expected, like so:
public class YourActivity extends ListActivity {
You are creating listview from cursor.So i am assuming that your list item 0 comes from Cursor's first record and so on.So if you click list item at position 3 you can move to cursor corresponding position by cursor.moveToPosition(3).Then can work accordingly