自定义的ListView显示随机顺序的项目少 [英] Custom listView shows less items in random order

查看:132
本文介绍了自定义的ListView显示随机顺序的项目少的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个自定义的ListView膨胀为我的Andr​​oid应用程序,我有getView()方法。问题

I have made a custom inflated listView for my android app and I have a problem with getView() method.

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()方法

And here is my getView() method

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

我有我的名单2型布局。一个为类别标题,另一个是,以根据该分类中的项目

I have 2 type of layouts for my list. One is for category title and the other one is for the items under that category.

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

我已经充满了数据,我需要在我的ListView显示目标列。 (在构造函数中)
还有的问题!
列表中的艺术家不断重复和类别是不是在正确的地方。

I have made the targetList filled with data I need to show in my listView. (in the constructor) And there is the problem! The artists in the list keep repeating and the categories are not in the right place.

有什么想法?

推荐答案

您的问题是由于这样的事实,你是不是你的数据绑定到你的行时 convertView!= NULL ,而不是你返回 convertView 被回收给你,这就是为什么你看到相同的重复数据。

Your problem is due to the fact that you are not binding your data to your rows when convertView != null, instead you are returning the convertView that is recycled to you, which is why you see the same repeated data.

您需要将数据绑定到你行的查看 S代表每一行,根据数据类型,如:

You need to bind data to your row's Views for every row, according to the data type, like this:

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 知道,有多个数据类型,因此它知道如何回收行的查看秒。

You also must override a few other methods to let the BaseAdapter know that there is more than one data type, so it knows how to recycle the row's 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;
}

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

You should be able to copy and paste this solution in, assuming I didn't make any mistakes.

这篇关于自定义的ListView显示随机顺序的项目少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆