ListView没有正确显示图像

时间:2013-12-16 12:47:44

标签: android

我有一个列表视图,显示从互联网下载的图像,并为列表视图的每一行提供自定义布局,如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:orientation="vertical"
    android:background="#00f"
    >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:background="@drawable/wish"
        android:gravity="center_vertical|center_horizontal"
        android:paddingBottom="5dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:paddingTop="5dp"
        android:text="" />
    </LinearLayout>

我的getview方法如下:

public View getView(int arg0, View arg1, ViewGroup arg2)
{
    // TODO Auto-generated method stub

    View v;

    final TextView tv;

    if(arg1 == null)
    { // if it's not recycled, initialize some attributes
        LayoutInflater lf = (LayoutInflater) conn.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = lf.inflate(R.layout.layitem, arg2, false);
        tv = (TextView) v.findViewById(R.id.textView1);
        tv.setLayoutParams(new LinearLayout.LayoutParams(width, width / 2));
        // tv.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);

    }

    else
    {
        v = arg1;
        tv = (TextView) v.findViewById(R.id.textView1);

    }

    if(arg0 < count)
    {
        Bitmap object = getImage(iurl[arg0]);
        tv.setBackground(new BitmapDrawable(conn.getResources(), object));
        tv.setText(texts[arg0]);
    }

    else
    {
        tv.setBackgroundResource(R.drawable.wish);
        tv.setText("");

    }

    return v;
}

我想要做的是我的ListView有20行而且我的记录少于20.所以记录填满行,其余的有默认背景image.count是实际的记录数。< / p>

iurl是包含要从服务器获取的图像名称的数组,而文本是包含消息的数组。

问题是arg0不超过2,所有行都包含从0到2索引的图像,当我向下滚动时,它会更改与行号对应的图像。

我可以将正确的图像放在第一个位置的正确行中,也可以只在滚动时占用。

我根据移动设备的屏幕宽度设置textview的宽度。这是正确的方式.kindly update。

2 个答案:

答案 0 :(得分:0)

我认为您应该使用asyntask从互联网上的特定网址获取图片。 如果您不使用线程从互联网加载图像,则需要很长时间才能加载图像并使您的程序崩溃

答案 1 :(得分:0)

您是否覆盖了getCount()方法?它应该返回此适配器所代表的数据集中的项目数。