Change image of listview item on click - android

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

Related

Is there onListItemTouch method in listview

I know onListItemClick method to handle click listener as below.
protected void onListItemClick(ListView l, View v, int position, long id){
}
But if I want to handle touch listener.
While item be touched, I want to get the position.
But not do in onListItemClick.
How can I do in ListView?
Is there any method such above?
you need to invoke onInterceptTouchEvent() method
Please check out my answer here.
Use AbsListView.onTouchEvent()
You have to subclass the ListView to use it though.
http://developer.android.com/reference/android/widget/AbsListView.html#onTouchEvent(android.view.MotionEvent)

How to get access to the fields of custom adapter in ListView in Android?

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

What is the best way to add an OnClickListener to Endless ListActivity

What is the best way to add an OnClickListener to a ListActivity that's Endless?
I am using cwac-endless adapter for my endless list.
The problem I am encountering is only my first batch of data that populates the list get's the click listener. The new data that get's fetched doesn't get the onclick listener.
I've tried adding a onclick listener in the following manner.
I used ListActivity onListItemClick
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String url = prodList.get(position).getProdUrl();
Log.i("URL",url);
Intent webViewIntent = new Intent(ProductListActivity.this, ProductWebView.class);
webViewIntent.putExtra("URL", url);
startActivity(webViewIntent);
}
UPDATE Rookie mistake, the above code works fine, the problem was that I was referencing the original list that was passed in instead of referencing the list in my custom ArrayAdapter.

Capturing checkbox event in a listview

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
}

Identify the item clicked on ListActivity method onListItemClick

I'm developing an Android application.
I have several objects loaded on a ListActivity. I want to know the item clicked on event onListItemClick.
I see method onListItemClick has a parameter called id. How can I setup this id?
My objects are identified by an ID, how can I add this ID to listItem?
Thanks.
if SectionObj is your object that you want to access later, set that in the adapter when you set the source.
ArrayList<SectionObj> li
ArrayAdapter<SectionObj> adapter=new ArrayAdapter<SectionObj>(getApplicationContext(),android.R.layout.simple_list_item_1, li);
setListAdapter(adapter);
then in ur listener method..
protected void onListItemClick(ListView l, View v, int position, long id) {
SectionObj o=(SectionObj)getListView().getItemAtPosition(position);
}
What is the source of your list data? If you are using a cursor - then the id passed in onListItemClick(ListView l, View v, int position, long id) will automatically be the id of the cursor row.
Use the following;
listView.getItemAtPosition(position);
Where listView is the name of your list view.
You can set the id in your ArrayAdapter view.
Checkout the following pages then you should find the solution.
http://developer.android.com/reference/android/app/ListActivity.html#onListItemClick(android.widget.ListView,%20android.view.View,%20int,%20long)
http://sudarmuthu.com/blog/using-arrayadapter-and-listview-in-android-applications

Categories

Resources