自定义listView以随机顺序显示较少的项目

时间:2013-04-05 22:00:30

标签: android listview

我为我的Android应用程序制作了一个自定义的膨胀listView,我遇到了getView()方法的问题。

public class BinderData extends BaseAdapter {

    private LayoutInflater inflater;
    private ArrayList targetList;
    private ViewHolder holder;
    private Activity nikola;
    private Context context;

    private static ArrayList genres = new ArrayList();

    public BinderData(Activity act, List<Genre> map) {
        targetList = new ArrayList();
        for (int i = 0; i<map.size(); i++){
            Genre genre = map.get(i);
            targetList.add(genre);
            ArrayList<Artist> aList = genre.getArtists();
            for (int j = 0; j<aList.size(); j++){
                targetList.add(aList.get(j));
            }
        }
        this.nikola = act;
        inflater = (LayoutInflater) act
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

这是我的getView()方法

public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;

        if (convertView == null){
            ViewableItem item = (ViewableItem)targetList.get(position);
            if (item.isTypeOf().equalsIgnoreCase("genre")){
                vi = inflater.inflate(R.layout.category_row, null);
                holder = new GenreViewHolder();
                ((GenreViewHolder)holder).textViewGenreTitle = (TextView)vi.findViewById(R.id.textViewCategoryTitle);
                String s = ((Genre)item).getTitle();
                holder.setTitle(s);
            }else{
                vi = inflater.inflate(R.layout.list_row, null);
                holder = new ArtistViewHolder();
                ((ArtistViewHolder)holder).textViewArtistName = (TextView)vi.findViewById(R.id.textViewAuthorNameHome);
                String s = ((Artist)item).getArtist();
                holder.setArtistName(s);
            }
            vi.setTag(holder);
        }else{
            holder = (ViewHolder)vi.getTag();
        }

        return vi;
    }

我的列表有两种类型的布局。一个用于类别标题,另一个用于该类别下的项目。

http://imgur.com/xbPfGTi - 这是图片

我已经使targetList填充了我需要在listView中显示的数据。 (在构造函数中) 而且有问题! 列表中的艺术家不断重复,类别不在正确的位置。

有任何想法吗?

1 个答案:

答案 0 :(得分:2)

您的问题是由于您在convertView != null时没有将数据绑定到您的行,而是返回了回收给您的convertView,这就是为什么您会看到相同的问题重复数据。

您需要根据数据类型将数据绑定到每行的View行,如下所示:

public View getView(int position, View convertView, ViewGroup parent) {
    final View row;
    boolean isGenreRow = getItemViewType(position) == GENRE_ROW;

    // Inflate views if this is a new row
    if (convertView == null) {
        // Get the right type of view depending on the type of data
        if (isGenreRow) {
            row = inflater.inflate(R.layout.category_row, null);
            holder = new GenreViewHolder();
            ((GenreViewHolder) holder).textViewGenreTitle = (TextView) row.findViewById(R.id.textViewCategoryTitle);
        }
        else {
            row = inflater.inflate(R.layout.list_row, null);
            holder = new ArtistViewHolder();
            ((ArtistViewHolder) holder).textViewArtistName = (TextView) row.findViewById(R.id.textViewAuthorNameHome);
        }
        row.setTag(holder);
    }
    // Otherwise, use the recycled row
    else {
        row = convertView;
        holder = (ViewHolder) row.getTag();
    }

    // Bind data to views, depending on data type
    ViewableItem item = (ViewableItem) targetList.get(position);
    if (isGenreRow) {
        ((GenreViewHolder) holder).setTitle(((Genre) item).getTitle());
    }
    else {
        ((ArtistViewHolder) holder).setArtistName(((Artist) item).getArtist());
    }

    return row;
}

您还必须覆盖其他一些方法,让BaseAdapter知道有多种数据类型,因此它知道如何回收行的View

private static final int GENRE_ROW = 0;
private static final int ARTIST_ROW = 1;

@Override
public int getItemViewType(int position) {
    // Return the row type at the position
    ViewableItem item = (ViewableItem) targetList.get(position);
    return item.isTypeOf().equalsIgnoreCase("genre") ? GENRE_ROW : ARTIST_ROW;
}

@Override
public int getViewTypeCount() {
    // Return the number of row types there are
    return 2;
}

假设我没有犯任何错误,您应该可以复制并粘贴此解决方案。