无法从ListView ArrayList获取正确的数据

问题描述 投票:0回答:1

我正在使用ListView的ArrayList并通过适配器添加项目。但是在“ convertView == null”代码之后,我无法从arrayList获取正确的数据。

例如,我推送数据,在arrayList中,但是arrayList [1]返回“ data0”,而不是“ data1”。

在其他部分,我可以从ArrayList获取正确的数据,但是仅失败了在“ convertView == null”代码之后。

这是我的适配器代码

public class KeikoItemAdapter extends BaseAdapter {
    private static final int ITEM_VIEW_TYPE_MAIN = 0;
    private static final int ITEM_VIEW_TYPE_YM = 1;

    final ArrayList<Keiko_item_Data> items;

    public KeikoItemAdapter (ArrayList <Keiko_item_Data> itemList){
        if ( itemList == null) {
            items = new ArrayList<Keiko_item_Data>();
        } else {
            items = itemList;
        }
    }

    public void addMainItem(String month, String date, String day, String diaryLog, String year) {
        Keiko_item_Data item = new Keiko_item_Data(month, date, day, diaryLog, year);
        item.setType(ITEM_VIEW_TYPE_MAIN);
        items.add(item);

    }

    public void addYmItem (String year, String month) {
        Keiko_item_Data item = new Keiko_item_Data(year, month);
        item.setType(ITEM_VIEW_TYPE_YM);
        items.add(item);
    }


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

    @Override
    public int getItemViewType(int position) {
        return items.get(position).getType();
    }

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

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

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

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

        final Context context = parent.getContext();
        int viewType = getItemViewType(position); 

        Keiko_item_Data listViewItem = items.get(position);



        if ( convertView == null ){
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            switch (viewType) {
                case ITEM_VIEW_TYPE_MAIN:
                    convertView = inflater.inflate(R.layout.keiko_item_design, parent, false);

                    Keiko_item_Data itemHolder = new Keiko_item_Data();

                    itemHolder.dayView = (TextView) convertView.findViewById(R.id.dayText);
                    itemHolder.dateView = (TextView) convertView.findViewById(R.id.dateText);
                    itemHolder.diaryView = (TextView) convertView.findViewById(R.id.diaryEdit);

                    convertView.setTag(itemHolder);

                    break;

                case ITEM_VIEW_TYPE_YM:
                    convertView = inflater.inflate(R.layout.keiko_month_item_design, parent, false);

                    Keiko_item_Data ymItemHolder = new Keiko_item_Data();

                    ymItemHolder.monthView = (TextView) convertView.findViewById(R.id.monthText);
                    ymItemHolder.yearView = (TextView) convertView.findViewById(R.id.yearText);

                    convertView.setTag(ymItemHolder);

            }

            final Keiko_item_Data finalHolder;


            switch (viewType) {
                case ITEM_VIEW_TYPE_MAIN:
                    finalHolder = (Keiko_item_Data) convertView.getTag();
                    finalHolder.dateView.setText(String.valueOf(listViewItem.getDate()));
                    finalHolder.dayView.setText(String.valueOf(listViewItem.getDay()));
                    finalHolder.diaryView.setText(listViewItem.getDiaryLog());
                   break;

                case ITEM_VIEW_TYPE_YM:
                    finalHolder = (Keiko_item_Data) convertView.getTag();
                    finalHolder.yearView.setText(String.valueOf(listViewItem.getYearData()));
                    finalHolder.monthView.setText(String.valueOf(listViewItem.getMonthData()));
                    break;

            }


            }

        return convertView;
    }
}
android listview arraylist adapter
1个回答
0
投票

适配器的位置是相对于视图的,而不是列表视图的对象,这意味着它将包括ITEM_VIEW_TYPE_MAIN个视图和ITEM_VIEW_TYPE_YM视图,您需要根据创建列表视图的方式减去该位置才能获得您列表视图的正确项目

© www.soinside.com 2019 - 2024. All rights reserved.