生成阵列适配器的数据

时间:2017-11-12 17:31:46

标签: java android arrays android-arrayadapter

您好我是编程新手。我正在尝试创建一个类似于contanct list的android项目。我正在为适配器类生成数据。我得到IndexOutOfBoundsException。我知道这是java的基础,但我无法弄清楚为什么我会收到此错误。提前致谢。

这是我的代码:

// How i am setting adapter from activity 

  customAdapter = new CustomAdapter(getActivity(), getDataSetForPojo(), mSectionsForPojo, mMapIndexForPojo);
        mRecyclerView.setAdapter(customAdapter);

//获取适配器列表

 private ArrayList<Pojo> getDataSetForPojo() {

        List<Pojo> pojoList = genData();

        getListIndexedForPojo(pojoList);

        ArrayList results = new ArrayList<>();
        ElementList obj;
        int section = 0;
        int normal = 0;
        String str;
        String ch;
        int total = pojoList.size() + mSectionsForPojo.length;
        for (int index = 0; index < total; index++) {

            str = pojoList.get(normal).getStr(); //here I am getting error
            ch = str.substring(0, 1);

            if (index == 0 || ch.equals(mSectionsForPojo[section])) {

                obj = new ElementList(ch, true);
                mMapIndexForPojo.put(ch, index);
                if (section < mSectionsForPojo.length - 1) {
                    section++;
                } else {
                    section = 0;
                }
            } else {
                obj = new ElementList(pojoList.get(normal).getStr(), false);
                normal++;
            }

            results.add(index, obj);
        }
        return results;
    }

//生成部分列表

public void getListIndexedForPojo(List<Pojo> fruitList) {

    mMapIndexForPojo = new LinkedHashMap<>();

    for (int x = 0; x < fruitList.size(); x++) {
        String str = fruitList.get(x).getStr();
        String ch = str.substring(0, 1);
        ch = ch.toUpperCase(Locale.US);

        // HashMap will prevent duplicates
        mMapIndexForPojo.put(ch, x);
    }

    Set<String> sectionLetters = mMapIndexForPojo.keySet();

    // create a list from the set to sort
    ArrayList<String> sectionList = new ArrayList<>(sectionLetters);

    Collections.sort(sectionList);

    mSectionsForPojo = new String[sectionList.size()];

    sectionList.toArray(mSectionsForPojo);
}

//生成数据

private List<Pojo> genData() {
        List<Pojo> pojoList = new ArrayList<>();

        Pojo pojo;

        pojo = new Pojo();
        pojo.setId(1);
        pojo.setStr("aback");
        pojoList.add(pojo);

      // adding more objects to list.

      return pojoList;
    }

1 个答案:

答案 0 :(得分:0)

我认为问题是你的for循环方法“getDataSetForPojo()”的运行速度超过了pojoList的大小。

 private ArrayList<Pojo> getDataSetForPojo() {
        ...
        List<Pojo> pojoList = genData();
        ...
        // total can have larger value than pojoList.size() if mSectionsForPojo.length isn't zero.
        int total = pojoList.size() + mSectionsForPojo.length;
        for (int index = 0; index < total; index++) {
            str = pojoList.get(normal).getStr(); //here I am getting error
        if (index == 0 || ch.equals(mSectionsForPojo[section])) {

        } else {
            ...
            normal++;
        }

        }
        return results;
    }

因此,正常可以增加超过pojoList大小。