Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android-ListView,为什么list.getChildAt(2).findViewById(R.id.icon)返回多个_Java_Android_Android Layout_Listview - Fatal编程技术网

Java Android-ListView,为什么list.getChildAt(2).findViewById(R.id.icon)返回多个

Java Android-ListView,为什么list.getChildAt(2).findViewById(R.id.icon)返回多个,java,android,android-layout,listview,Java,Android,Android Layout,Listview,我对android ListView和GridView有一个奇怪的问题 我有一个列表视图,其中包含100个项目和以下xml: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:l

我对android ListView和GridView有一个奇怪的问题

我有一个列表视图,其中包含100个项目和以下xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:id="@+id/main_screen">
  <RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextView
      android:id="@+id/name"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
    <ImageView
      android:id="@+id/icon"
      android:layout_width="40dp"
      android:layout_height="40dp"
      android:src="@drawable/icon_1"
      android:onClick="changeIcon" />
  </RelativeLayout>
</RelativeLayout>

public void changeIcon(View view) {
    ImageView myIcon= (ImageView)view.findViewById(R.id.icon);
    myIcon.setImageResource(R.drawable.icon_2);
}

公共无效更改图标(视图){
ImageView myIcon=(ImageView)view.findViewById(R.id.icon);
myIcon.setImageResource(R.drawable.icon_2);
}
如果我单击列表中的第一个图标,它会替换我触摸过的图标(这是我想要的),但是,它也会替换视图之外的图标,不是所有图标,而是按顺序发生的

所以在这些图片中,我的触摸选择了1个,13,25,37等。。已自动选择。。 当我快速向上滚动时,我的图标会随机变化。有时它会转到2或3

同样的问题也发生在GridView上

有人能解释这种行为吗?我能做些什么来避免这种情况

链接到屏幕截图:

图1为空列表, 图2是我点击图标#1,
图3显示,我触摸#1时,13也发生了变化。

问题是ListView在滚动后会重新使用视图


您应该单独保存每个列表项的状态。这应该在自定义适配器中完成。

您好,谢谢您的回复。。有关于如何保存项目状态的提示吗?