自定义适配器在字符串数组中不返回任何内容

时间:2018-08-28 23:32:52

标签: android

我的Android应用中有一个自定义微调器;

public void onDataChange(DataSnapshot dataSnapshot) {
        resultList.clear();
        listSpinner.clear();
        listSpinner.add("All teams");

        for(DataSnapshot matchSnapshot : dataSnapshot.getChildren()) {
            matches match = matchSnapshot.getValue(matches.class);
            resultList.add(match);
            listSpinner.add(matchSnapshot.child("homeTeam").getValue().toString());
            listSpinner.add(matchSnapshot.child("awayTeam").getValue().toString());
        }

        listSpinner = new ArrayList<String>(new LinkedHashSet<String>(listSpinner));
        spinnerTitles = listSpinner.toArray(new String[0]);
        CustomAdapter adapterSpin = new CustomAdapter(getContext(), spinnerTitles, spinnerImages, spinnerPopulation);
        spinner.setAdapter(adapterSpin);
        resultList.sort(Comparator.comparing(matches::getDateFormatted).thenComparing(matches::getTime));
        resultList adapter = new resultList (getActivity(), resultList);
        listViewResult.setAdapter(adapter);
    }

这对于填充微调器非常有用,但是当我尝试使用微调器并从列表中选择一个项目时,我会不断获得该信息;

IndexOutOfBoundsException: Index: 2, Size: 0

错误指向下面的第四行;

 spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int position,long id ) {
            if(++check > 1) {
                String getSpinTeam =(String)parent.getSelectedItem();
                ....
            }
        }
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });
}

当我调试并检查返回的值时,我仍然可以在数组中看到2条记录,所以我很困惑为什么它错误地指出大小为0,而它显然是在告诉我某些东西。调试器。

我的问题是,这是我构建的第一个自定义微调器,我如何处理setOnItemSelectedListener是处理微调器返回的数据的通用方法(适用于内置的非自定义版本)或具有我错过了什么吗?

任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:0)

我认为错误在这里:

DisplayUnlockCaptcha

它必须是:

String getSpinTeam =(String)parent.getSelectedItem();

答案 1 :(得分:0)

也许您应该尝试一下,替换:

 String getSpinTeam =(String)parent.getSelectedItem();

通过

 String getSpinTeam = spinnerTitles.get(position);

希望获得帮助