我正在尝试显示列表中的所有数据。不幸的是,我遇到了此错误“ java.lang.IndexOutOfBoundsException:无效的索引1,大小为1”。
当我尝试更改holder.country_timezone.setText(pozycja.getTimezones().get(0));
-时,它仅显示第一项。我想显示列表中的所有项目。
模型
@SerializedName("timezones")
@Expose
private List<String> timezones = null;
适配器
private Context context;
private List<JsonMain> dataList;
private List<JsonMain> filtr;
public DataAdapter(Context context, List<JsonMain> dataList) {
this.context = context;
this.dataList = dataList;
this.filtr= dataList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_row, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
final JsonMain pozycja = filtr.get(position);
holder.country_flag.setImageResource(pozycja.getLogo());
holder.country_name.setText(pozycja.getName());
holder.country_subregion.setText(pozycja.getSubregion());
holder.country_nativename.setText(pozycja.getNativeName());
holder.country_capital.setText(pozycja.getCapital());
holder.country_topleveldomain.setText(pozycja.getTopLevelDomain().get(0));
holder.country_population.setText(String.valueOf(pozycja.getPopulation()));
holder.country_gini.setText(String.valueOf(pozycja.getGini()));
for(int i =0; i<filtr.size(); i++) {
holder.country_timezone.setText(pozycja.getTimezones().get(i));
}
答案 0 :(得分:2)
您从时区列表中获取具有filtr.size索引的值,将其更改为:
for(int i =0; i<timezones.size(); i++) {
holder.country_timezone.setText(pozycja.getTimezones().get(i));
}
此外,为什么要在onBindViewHolder中循环?您的代码应如下所示:
String timezone = pozycja.getTimezones().get(adapterPosition);
holder.country_timezone.setText(timezone);
答案 1 :(得分:0)
使用notifyDataSetChanged();在您的代码中更改数据时