Android列表视图显示错误数据

时间:2017-04-22 13:39:28

标签: android listview position

我正在开发Android应用,目前我正在使用一个列表视图。问题是列表视图没有返回在getView方法中显示数据的正确位置。这是我的代码:

public class CustomAdapter extends ArrayAdapter<Custom> {

    private final static int NUMBER_OF_VIEW_TYPES = 3;

    private Activity activity;
    private List<Custom> custom;
    private List<String> headerTitles;
    private List<Card> getStartedCards;

    public CustomAdapter(Activity activity, List<Custom> custom,
                               List<String> headerTitles, List<Card> getStartedCards) {
        super(activity, R.layout.row_custom, custom);
        this.activity = activity;
        this.custom = custom;
        this.headerTitles = headerTitles;
        this.getStartedCards = getStartedCards;
    }


    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
                ViewHolder viewHolder;

                if (convertView == null) {
                    int viewType = getItemViewType(position);
                    switch (viewType) {
                        case RowType.HEADER:
                            String title;
                            if (position == 0)
                                title = headerTitles.get(0);
                            else
                                title = headerTitles.get(1);
                            convertView = LayoutInflater.from(activity).inflate(R.layout.card_header, parent, false);
                            TextView textView = (TextView) convertView.findViewById(R.id.header_title);
                            textView.setText(title);
                            break;
                        case RowType.GET_STARTED_CARD:
                            Card card;
                            if (position == 1)
                                card = getStartedCards.get(0);
                            else
                                card = getStartedCards.get(1);
                            convertView = LayoutInflater.from(activity).inflate(R.layout.card_get_started, parent, false);
                            TextView textViews = (TextView) convertView.findViewById(R.id.get_started_name);
                            textViews.setText(card.getTitle());
                            break;
                        case  RowType.CARD:
                            Custom custom = custom.get(4); // hardcoded
                            convertView = LayoutInflater.from(activity).inflate(R.layout.row_custom, parent, false);
                            viewHolder = new CustomAdapter.ViewHolder(convertView, activity);
                            convertView.setTag(viewHolder);

                            Glide.with(activity)
                                    .load(custom.getAppIconLink())
                                    .placeholder(R.drawable.ic_image_placeholder)
                                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                                    .into(viewHolder.imageView);
                            viewHolder.nameTextView.setText(custom.getName());
                            viewHolder.descriptionTextView.setText(custom.getDescription());
                            break;
                        default:
                            convertView = LayoutInflater.from(activity).inflate(R.layout.row_custom, parent, false);
                            break;
                    }
                }

                return convertView;
            }
    @Override
        public int getItemViewType(int position) {
            if (headerTitles.size() > 0 && getStartedCards.size() > 0 && position == 0) {
                return RowType.HEADER;
            }
            if (headerTitles.size() > 0 && getStartedCards.size() == 1 && position == 2) {
                return  RowType.HEADER;
            }
            if (headerTitles.size() > 0 && getStartedCards.size() == 2 && position == 3) {
                return  RowType.HEADER;
            }
            if (headerTitles.size() > 0 && getStartedCards.size() > 0 && position == 1) {
                return RowType.GET_STARTED_CARD;
            }
            if (headerTitles.size() > 0 && getStartedCards.size() > 0 && position == 2) {
                return RowType.GET_STARTED_CARD;
            }
            return RowType.CARD;
        }

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

        @Override
        public int getCount() {
            return custom.size() +
                    headerTitles.size() +
                    getStartedCards.size();
        }

        @Override
        public Custom getItem(int position) {
            return custom.get(position);
        }

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

这是我的数据列表List<Custom> custom。当我尝试使用Custom从该列表中获取Custom custom = custom.get(position); // in the example code above is hardcoded 4的位置时,我从RowType.CARD的列表中获取了错误的元素,并且元素正在重复。我有三种视图类型,每种视图类型都有自己的行。我的问题是,当所有行的总和为10时,为什么position为4,5或6?如何从0检索元素到custom.size() - 1并在RowType.CARD列表的行中显示其属性?谢谢你的回答。

0 个答案:

没有答案