Home » Android » android – Cannot get right data from ListView ArrayList-Exceptionshub

android – Cannot get right data from ListView ArrayList-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

I’m using ListView’s ArrayList and add items by adapter.
But after ” convertView == null ” code, I cannot get right data from the arrayList.

For example, I push datas
< position 0 – “data0” > , < position 1 – “data1” >
in the arrayList,
but arrayList[1] return “data0”, not “data1”.

In other parts, I can get right data from ArrayList, but it failed only
after ” convertView == null ” code.

Here’s my adapter code

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;
    }
}
How to&Answers:

The position of the adapter is relative to the view and not the object of the listview, meaning it will include ITEM_VIEW_TYPE_MAIN views & ITEM_VIEW_TYPE_YM views you need to subtract the position depending on how you build your listviews in order to get the right item of your listview