ListView项目重复

时间:2015-03-09 11:15:42

标签: android listview listadapter

现在我有一个列表视图,其中包含一个显示不同布局类型的自定义适配器。 列表视图项以随机方式重复,我无法弄清楚问题。 那么,有人可以帮忙吗? 这是我的代码。

public class NewsFeedAdapter extends BaseAdapter {

private final int IMAGE_COMPARISON = 0;
private final int POLL = 1;
private final int EMO = 2;
private final int TYPE_MAX_COUNT = EMO + 1;
private ArrayList<Post> posts = new ArrayList<Post>();
private ArrayList<Post> originalPosts = new ArrayList<Post>();
private MaterialDialog alert;
private Context context;
private ApiClient apiClient;
private SharedPreferences sharedPreferences;
private int id;
private Bitmap bitmap;
private ArrayList<View> views ;
private Intent sharingIntent;
private String link;
private String type;

public NewsFeedAdapter(Context context, ArrayList<Post> questionItems) {
    this.context = context;
    this.posts = questionItems;
    this.originalPosts = new ArrayList<Post>(posts);
    this.apiClient = ApiClient.getInstance(context);
    sharedPreferences = context.getSharedPreferences(context.getResources().getStringArray(R.array.preferences)[4], 0);
    views = new ArrayList<>();
}

@Override
public int getCount() {
    return posts.size();
}

@Override
public Object getItem(int position) {
    return posts.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public int getViewTypeCount() {
    return TYPE_MAX_COUNT;
}

@Override
public int getItemViewType(int position) {

    String type = ((Post)getItem(position)).getQ().getType();
    if(type.equalsIgnoreCase("radio")){
        return POLL;
    }else if(type.equalsIgnoreCase("image")){
        return IMAGE_COMPARISON;
    }else{
        return EMO;
    }
}

public int getItemViewType(Post post) {

    String type = (post).getQ().getType();
    if(type.equalsIgnoreCase("radio")){
        return POLL;
    }else if(type.equalsIgnoreCase("image")){
        return IMAGE_COMPARISON;
    }else{
        return EMO;
    }
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

        int type = getItemViewType(position);

        switch (type) {
            case IMAGE_COMPARISON:

                ImageListRowView imageListRowView;

             //   if (convertView == null) {
                    imageListRowView = new ImageListRowView(context, new MenuListener() {
                        @Override
                        public void onReceive(MenuItemAction menuItemAction,int position) {
                            preformMenuAction(menuItemAction, position);
                        }
                    });
                    imageListRowView.setData(posts.get(position));
                    imageListRowView.setTag(R.string.view_tag,position);
                views.add(imageListRowView);
                return imageListRowView;
            //    }
           // else
            //    imageListRowView = (ImageListRowView) convertView;
            //return imageListRowView;

                {
              case EMO:

                EmoListRowView emoListRowView;

             //   if (convertView == null) {
                    emoListRowView = new EmoListRowView(context, new MenuListener() {
                        @Override
                        public void onReceive(MenuItemAction menuItemAction,int position) {
                            preformMenuAction(menuItemAction, position);
                        }
                    });
                    emoListRowView.setData(posts.get(position));
                    emoListRowView.setTag(R.string.view_tag,position);
                  views.add(emoListRowView);
                  return emoListRowView;
             //   }
             //   else
             //       emoListRowView = (EmoListRowView) convertView;
             //   return emoListRowView;

            case POLL:

                PollListRowView pollListRowView;

              //  if (convertView == null) {
                    pollListRowView = new PollListRowView(context, new MenuListener() {
                        @Override
                        public void onReceive(MenuItemAction menuItemAction,int position) {
                            preformMenuAction(menuItemAction, position);
                        }
                    });
                    pollListRowView.setData(posts.get(position));
                    pollListRowView.setTag(R.string.view_tag,position);
                    views.add(pollListRowView);
                    return pollListRowView;
                }
       //     else
       //         pollListRowView = (PollListRowView) convertView;
       //     return pollListRowView;
        }
    return convertView;
}

2 个答案:

答案 0 :(得分:0)

getView()需要始终自定义行。根据{{​​1}}是否convertView的唯一差异是您是否还需要对行进行充气。

null个阻止版块中,如果case不是convertView,则只需返回null而不进行修改。

答案 1 :(得分:0)

看看下面的问题。我认为你的问题类似于这个问题。

After scrolling listview, listview items getting repeatedly in android