0

实际上我的问题和这个人的一样。我不知道如何解决它。这是我的 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我的AsyncTask class. 这是我的列表活动的功能。

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);
}

这个 bmp 是asynctask类中下载的位图,ls是列表视图,是列表视图index中项目的索引。当 postexecute 运行时,我使用参数调用此函数以更新列表视图项目图像。

问题与我在链接中提到的完全相同,即当我滚动它时出现错误。但他通过更改列表视图的布局宽度解决了问题,但它在这里不起作用。

4

2 回答 2

0

似乎和许多其他人一样,您对这种特殊观点有一些问题。

我强烈推荐观看讲座“ listView 的世界”。

他们谈论了很多与 listView 相关的话题,包括在同一个视图上多次调用 getView 的话题。简而言之,之所以会发生,是因为测量listView项目时也调用了getView。您可以使用(并且应该)使用 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 。

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

于 2013-02-07T20:49:36.337 回答
0

1)定义您的自定义适配器

2) 应用支架图案

3)使用一些图片上传器(例如这个

看一个简单的例子

于 2013-02-07T20:59:41.547 回答