ListView显示重复项?

时间:2015-03-25 10:23:00

标签: listview android-listview

我有一个列表视图,其中列表项包含三个视图。图像,标题和副标题。当我调用它时,它会在每个第4个之后重复项目。例如

Item 0
Item 1
Item 2
Item 3
Item 0
Item 1
Item 2
Item 3

依旧......

list_item_view.xml

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="6dp" >

   <ImageView
    android:id="@+id/listImg"
    android:layout_width="80dp"
    android:layout_height="80dp" />

<TextView
    android:id="@+id/listItemTxt"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="90dp"
    android:layout_marginStart="90dp"
    android:textColor="#D8000000"
    android:textSize="20sp"
   />

<TextView
    android:id="@+id/listSubtxt"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/listItemTxt"
    android:layout_marginLeft="90dp"
    android:layout_marginStart="90dp"
    android:ellipsize="marquee"
    android:textColor="#D8000000"
    android:textSize="15sp" />

</RelativeLayout>

list_view.xml

<?xml version="1.0" encoding="UTF-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</ListView>
</LinearLayout>

ListViewAdapter.java

public class ListViewAdapter extends ArrayAdapter<Resource>{
private Activity context;
private ArrayList<Resource> resList;

public ListViewAdapter(Context context, ArrayList<Resource> reslist) {
    super(context, R.layout.list_item_view, reslist);
    this.context = (Activity) context;
    this.resList = reslist;
}
static class ViewHolder {
    private TextView itemName;
    private TextView subItemName;
    private ImageView thumb_image;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder mViewHolder = null;
    if (convertView == null) {
        mViewHolder = new ViewHolder();

        LayoutInflater vi = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        convertView = vi.inflate(R.layout.list_item_view,
                parent, false);
        mViewHolder.itemName = (TextView) convertView
                .findViewById(R.id.listItemTxt); // title
        mViewHolder.subItemName = (TextView) convertView.findViewById(R.id.listSubtxt);//subtitle
        mViewHolder.thumb_image = (ImageView) convertView
                .findViewById(R.id.listImg); // thumb image
        mViewHolder.itemName.setText(resList.get(position).title);
        //Log.e("TITLE:",">>"+items[position]);
        mViewHolder.subItemName.setText("Last Updated: "+resList.get(position).date_updated);
        Bitmap bm = DecodeBitmap.decodeSampledBitmapFromUri(
                resList.get(position).thumbnile_path, 220, 220);
        if(bm != null)
            mViewHolder.thumb_image.setImageBitmap(bm);
        else
            mViewHolder.thumb_image.setImageResource(R.drawable.thumb_img);
        convertView.setTag(mViewHolder);
    } else {
        mViewHolder = (ViewHolder) convertView.getTag();
    }
    Log.e("getView " + position, ">>" + convertView);
    return convertView;
}
}

任何帮助将不胜感激。感谢。

1 个答案:

答案 0 :(得分:3)

您正在错误地实施ViewHolder模式。这应该有用。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder mViewHolder = null;
    if (convertView == null) {
        mViewHolder = new ViewHolder();

        LayoutInflater vi = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        convertView = vi.inflate(R.layout.list_item_view,
                parent, false);
        mViewHolder.itemName = (TextView) convertView.findViewById(R.id.listItemTxt);
        mViewHolder.subItemName = (TextView) convertView.findViewById(R.id.listSubtxt);
        mViewHolder.thumb_image = (ImageView) convertView.findViewById(R.id.listImg);

        convertView.setTag(mViewHolder);
    }
    mViewHolder = (ViewHolder) convertView.getTag();
    mViewHolder.itemName.setText(resList.get(position).title);
    mViewHolder.subItemName.setText("Last Updated: "+ resList.get(position).date_updated);
    Bitmap bm = DecodeBitmap.decodeSampledBitmapFromUri(
            resList.get(position).thumbnile_path, 220, 220);
    if(bm != null) {
        mViewHolder.thumb_image.setImageBitmap(bm);
    } else {
        mViewHolder.thumb_image.setImageResource(R.drawable.thumb_img);
    }
    convertView.setTag(mViewHolder);

    return convertView;
}