Listview BaseAdapter类getview被多次调用同一位置

时间:2013-02-07 19:49:22

标签: android android-listview

实际上我的问题与this家伙的问题相同。但我不知道如何解决它。这是我的listview xml文件。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">

<LinearLayout     android:orientation="horizontal"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:weightSum="3"
                  android:background="#CCCCCC"
                  android:id="@+id/tabs">

    <Button 
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           android:background="#CCCCCC"
           android:textSize="20sp"
           android:textColor="#000000"
           android:text="@string/NewTask"
           android:id="@+id/tab1"
        />

    <Button 
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:background="#CCCCCC"
           android:layout_weight="1"
           android:textSize="20sp"
           android:textColor="#000000"
           android:text="@string/Friends"
           android:id="@+id/tab2"
        />
    <Button 
           android:layout_width="fill_parent"
           android:background="#CCCCCC"
           android:layout_height="wrap_content"
           android:textColor="#000000"
           android:layout_weight="1"
           android:text="@string/AddPeople"
           android:textSize="20sp"
           android:id="@+id/tab3"
        />
    </LinearLayout>

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:cacheColorHint="#000000"
    android:layout_below="@+id/tabs"
/>

</RelativeLayout>

我正在尝试从onpostExecute的{​​{1}}调用我的listactivity中的函数。 这是我的listactivity的功能。

AsyncTask class

这个bmp是在public void SetImage(Bitmap bmp,int index) { View v = this.ls.getChildAt(index); ImageView img = (ImageView)v.findViewById(R.id.tasks_userimg); img.setImageBitmap(bmp); } 类中下载的位图,asynctask是列表视图,ls是列表视图中项目的索引。当postexecute运行时,我称之为使用参数来更新listview项目图像。

问题与我在链接中提到的完全相同,即当我滚动它时会给我错误。但他通过更改listview的布局宽度来解决问题,但它在这里不起作用。

2 个答案:

答案 0 :(得分:0)

似乎与许多其他人一样,您对此特殊视图存在一些问题。

我强烈建议您观看演讲“the world of listView”。

他们谈论了很多与listView相关的主题,包括在同一视图上多次调用getView的主题。简而言之,它之所以发生,是因为还调用了getView来测量listView项。您可以使用(并且应该)使用convertView以避免不需要的数据膨胀和提取。

关于问题本身,你不应该在listView项目上使用findViewById,因为它们会被重用。例如,在用户向下滚动的情况下,可以将为第0个位置设置的视图设置为第7个位置。

如果您想从listView更新单个项目,可以使用以下内容:

// itemIndex is the index of the item to update
final View v=listView.getChildAt(itemIndex-listView.getFirstVisiblePosition());
//now you update your view according to what you wish . 
//you must of course handle the cases that you can't get the view .

当然,一旦你这样做,你还必须更新适配器后面的数据,以便下次用户滚动到这个项目时,它会显示位图。

你应该做什么取决于你使用的asyncTask。

我假设您在listview填充其项目时下载了多个位图。如果是这种情况,请查看this sample,或使用我的建议:为每个viewHolder设置一个asyncTask。每次调用getView时,取消旧的getView并为当前项创建一个新的。

答案 1 :(得分:0)

1)定义自定义适配器

2)应用持有人模式

3)使用一些图像上传器(例如this

看一下简单的example

相关问题