I have a ListView in my layout. Here is the layout for list item.
shopitem.xml
<?xml version="1.0" encoding="utf-8"?>
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="2dip"
android:paddingBottom="2dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:mode="twoLine">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="6dip"
android:paddingLeft="44dip"
android:textStyle="bold"
android:lines="1"
android:textColor="#FFFFFF"/>
<ImageView
android:id="@+id/playBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dip"
android:layout_alignRight="@android:id/text1"
开发者_开发技巧 android:src="@drawable/btnplaypreview"/>
</TwoLineListItem>
Now I want to set some change of image in playBtn
OnItemClickListener. For that I am using the following code.
ListView shopActivityListView = (ListView) findViewById(R.id.shopActivityListView);
shopActivityListView.setCacheColorHint(Color.TRANSPARENT);
shopActivityListView.setAdapter(new ArrayAdapter<String>(getApplicationContext(), R.layout.shopitem, trackArr[1]));
shopActivityListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View vItem, int position, long id) {
// TODO Auto-generated method stub
ImageView playBtn = (ImageView) vItem.findViewById(R.id.playBtn);
playBtn.setImageResource(R.drawable.eq12);
}
});
But nothing is happening on itemclick. I have checked that onItemClick
method is executing without any exception. But nothing is changing. What is the problem in my code?
The image in any item of Listview
cannot change directly as it is controlled by Adapter. Only getView()
method of the adapter can set any view in the items.
Therefore, in onItemClick
method, we have to set a position
specific flag and then call the notifyDataSetChanged()
for the adapter. Moreover, a custom adapter is needed, where we have to define the getView() method so that we have set the image depending on the above position
specific flag.
精彩评论