Android ListView的OnItemClickListener相关问题
我的布局中有一个 ListView 。这是列表项的布局。
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>
现在我想在 playBtn
OnItemClickListener 中设置一些图像更改。为此,我使用以下代码。
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);
}
});
但 itemclick 上没有任何反应。我检查了 onItemClick
方法正在执行,没有任何异常。但一切都没有改变。我的代码有什么问题?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Listview
的任何一项中的图像都无法直接更改,因为它是由 Adapter 控制的。只有适配器的getView()
方法可以设置项目中的任何视图。因此,在
onItemClick
方法中,我们必须设置一个position
特定标志,然后为适配器调用notifyDataSetChanged()
。此外,还需要一个自定义适配器,我们必须在其中定义 getView() 方法,以便我们根据上面的position
特定标志设置图像。The image in any item of
Listview
cannot change directly as it is controlled by Adapter. OnlygetView()
method of the adapter can set any view in the items.Therefore, in
onItemClick
method, we have to set aposition
specific flag and then call thenotifyDataSetChanged()
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 aboveposition
specific flag.