-1

I am trying to show all data from List. Unfortunately i ve got this error " java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1". When i try to change holder.country_timezone.setText(pozycja.getTimezones().get(0));- it shows only first item. I would like to show all items from List.

Model

@SerializedName("timezones")
@Expose
private List<String> timezones = null;

Adapter

 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));
    }
  • What is the size of filtr and timezones? – Nishikanto Sarkar Simul Jul 27 '18 at 18:51
  • Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Tyler V Jul 27 '18 at 18:56

2 Answers2

2

You getting values from timezones List with filtr.size indexes change it to:

for(int i =0; i<timezones.size(); i++) {
   holder.country_timezone.setText(pozycja.getTimezones().get(i));
}

Also, why you looping inside onBindViewHolder? Your code should looks like this:

String timezone = pozycja.getTimezones().get(adapterPosition);
holder.country_timezone.setText(timezone);
Marcos Vasconcelos
  • 17,773
  • 29
  • 104
  • 165
  • Thank you for your reply . I changed for(int i =0; i – smitzer vojtech Jul 27 '18 at 19:05
  • You have a problem of what you want to display on ONLY ONE textField, you only will see the last value, also by the simptons (index is 1 outofbounds) you only have ONE value on getTimezones() list, if you have more you can simply loop the list and add then all to a String before setting it as the text of the TextView – Marcos Vasconcelos Jul 27 '18 at 19:08
0

use notifyDataSetChanged(); in your code when your data changes