【问题标题】:Generating data for the Array Adapter为阵列适配器生成数据
【发布时间】:2018-04-25 09:28:42
【问题描述】:

您好,我是编程新手。我正在尝试创建一个类似联系人列表的 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;
    }

【问题讨论】:

  • IndexOutOfBoundsException 在哪一行被抛出?
  • 没有人会通读所有代码,只将相关代码发布到错误中。
  • @Shn_Android_Dev 在我的第二个代码 sn-p 中看到,我在那里发表了评论说“我在这里遇到错误”。在那条线上。同时从列表中获取数据
  • @m0skit0 我会尽量减少代码
  • 由于您收到 IndexOutOfBoundsException,这意味着 pojoList.size() 小于变量“正常”。请记住,当您调用 pojoList.get(number) 时,“get”是基于 0 的,即调用 pojoList.get(0) 而不是 pojoList(1) 检索 pojoList 中的第一项。您需要调查为什么 pojoList.size() 小于“正常” - 根据我的经验,这通常是由于基于 0 的索引的人为错误。

标签: java android arrays android-arrayadapter


【解决方案1】:

我认为问题在于您在方法“getDataSetForPojo()”中的 for 循环运行的超出了 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;
    }

因此,normal 的增量可能超过 pojoList 的大小。

【讨论】:

    猜你喜欢
    • 2018-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多