2

我为我的 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 中显示的数据。(在构造函数中)有问题!列表中的艺术家不断重复,类别不在正确的位置。

有什么想法吗?

4

1 回答 1

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知道存在不止一种数据类型,因此它知道如何回收行的Views。

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

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

于 2013-04-05T22:36:33.160 回答