java - IndexOutOfBoundsException when clicking RecyclerView item -


having problem when clicking items of recyclerview, app crashes , giving output:

java.lang.indexoutofboundsexception: invalid index 24, size 20         @ java.util.arraylist.throwindexoutofboundsexception(arraylist.java:255)         @ java.util.arraylist.get(arraylist.java:308)         @ com.google.gson.jsonarray.get(jsonarray.java:147)         @ com.devpocket.kvartirka.mainactivity.itemclicked(mainactivity.java:334)         @ com.devpocket.kvartirka.adapters.offersadapter$viewholder.onclick(offersadapter.java:161)         @ android.view.view.performclick(view.java:5162)         @ android.view.view$performclick.run(view.java:20873)         @ android.os.handler.handlecallback(handler.java:739)         @ android.os.handler.dispatchmessage(handler.java:95)         @ android.os.looper.loop(looper.java:145)         @ android.app.activitythread.main(activitythread.java:5834)         @ java.lang.reflect.method.invoke(native method)         @ java.lang.reflect.method.invoke(method.java:372)         @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1388)         @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1183)  java.lang.indexoutofboundsexception: invalid index 24, size 20 

the code i'm using this, adapter:

public class offersadapter extends recyclerview.adapter<offersadapter.viewholder> {  private clicklistener clicklistener;  private citydata citydata;  private arraylist<citydata> cityitemlist = new arraylist<citydata>(); private context mcontext; private string text;  public static button headerbutton1;  public offersadapter(context context, arraylist<citydata> cityitemlist, string text) {     this.cityitemlist = cityitemlist;     this.mcontext = context;     this.text = text; }  @override public int getitemviewtype(int position) {      int viewtype;      if (position == 0) {         viewtype = 0;     } else {         viewtype = 1;     }      return viewtype; }  @override public offersadapter.viewholder oncreateviewholder(viewgroup viewgroup, final int viewtype) {      view itemlayoutview;     viewholder viewholder;      if (viewtype == 0) {         itemlayoutview = layoutinflater.from(viewgroup.getcontext())                 .inflate(r.layout.logo_layout, viewgroup, false);         viewholder = new viewholder(itemlayoutview, viewtype);     } else {         itemlayoutview = layoutinflater.from(viewgroup.getcontext())                 .inflate(r.layout.offers_singleitem, viewgroup, false);         viewholder = new viewholder(itemlayoutview, viewtype);     }      return viewholder; }  @override public void onbindviewholder(offersadapter.viewholder viewholder, int position) {      if (position > 0) {         citydata = cityitemlist.get(position-1);         string type = citydata.gettype();          if ("flat".equals(type)) {             viewholder.address.settext(citydata.getaddress());             viewholder.description.settext(citydata.getdescription());             viewholder.roomnumbers.settext(citydata.getroomnumbers());             if (textutils.isempty(citydata.getmetro())) {                 viewholder.metroimageview.setvisibility(view.gone);             }             viewholder.metro.settext(citydata.getmetro());             viewholder.prices.settext(citydata.getprices() + " ₽");             ion.with(viewholder.offerimage)                     .fitxy()                     .load(citydata.geturl());         } else if("cottage".equals(type)) {             viewholder.address.settext(citydata.getaddress());             viewholder.description.settext(citydata.getdescription());             viewholder.roomnumbers.setvisibility(view.gone);             viewholder.cottageimage.setvisibility(view.visible);             if (textutils.isempty(citydata.getmetro())) {                 viewholder.metroimageview.setvisibility(view.gone);             }             viewholder.metro.settext(citydata.getmetro());             viewholder.prices.settext(citydata.getprices() + " ₽");             ion.with(viewholder.offerimage)                     .fitxy()                     .load(citydata.geturl());         }     }  }  public class viewholder extends recyclerview.viewholder implements view.onclicklistener {      public textview roomnumbers; //количество комнат     public textview address; //адресс квартиры     public textview description; //описание квартиры     public textview metro; //описание квартиры     public textview prices; //цена за день     public imageview offerimage; //картинка квартиры     public imageview cottageimage; //обозначение коттеджа     public imageview metroimageview; //значок метро      public viewholder(view itemview, int position) {         super(itemview);           if(position > 0) {             roomnumbers = (textview) itemview.findviewbyid(r.id.roomnumbers);             address = (textview)  itemview.findviewbyid(r.id.addresstextview);             description = (textview) itemview.findviewbyid(r.id.conditionstextview);             metro = (textview) itemview.findviewbyid(r.id.metrotextview);             offerimage = (imageview) itemview.findviewbyid(r.id.imageview);             prices = (textview) itemview.findviewbyid(r.id.pricetv);             cottageimage = (imageview) itemview.findviewbyid(r.id.cottageimage);             metroimageview = (imageview) itemview.findviewbyid(r.id.metroimageview);             itemview.setonclicklistener(this);         } else {             headerbutton1 = (button) itemview.findviewbyid(r.id.headerbutton1);             headerbutton1.settext(html.fromhtml(text));             headerbutton1.setonclicklistener(new view.onclicklistener() {                 @override                 public void onclick(view v) {                     intent intent = new intent(mcontext, filteractivity.class);                     mcontext.startactivity(intent);                 }             });         }       }      @override     public void onclick(view v) {          if(clicklistener != null) {             notifydatasetchanged();             clicklistener.itemclicked(v, getposition());         }     } }  @override public int getitemcount() {     return (null != cityitemlist ? cityitemlist.size() : 0); }  public void setclicklistener(clicklistener clicklistener) {     this.clicklistener = clicklistener; }  public interface clicklistener {     public void itemclicked(view view, int position); }  } 

and how adding data adapter:

private arraylist<citydata> citylist = new arraylist<citydata>();  citydata cityitem = new citydata(); cityitem.setaddress(string.valueof(flataddress)); cityitem.settype(type); cityitem.setdescription(description); cityitem.setroomnumbers(rooms); cityitem.setmetro(metro); cityitem.seturl(imageurl); cityitem.setprices(day); citylist.add(cityitem); adapter.notifydatasetchanged(); adapter.setclicklistener(mainactivity.this); 

and on itemclicked

@override public void itemclicked(view view, int position) {      string offerdata = null;      for(int = 0; <= position; i++ ) {          if(i == position) {             jsonelement obj = jsonobjecttopass.get(position-1);             offerdata = string.valueof(obj);         }     }      intent intent = new intent(mainactivity.this, offerinfo.class);     intent.putextra("offerdata", offerdata);     intent.putextra("position", position);     startactivity(intent);  } 

change itemclicked() this

@override public void itemclicked(view view, int position) {      string offerdata = null;       jsonelement obj = jsonobjecttopass.get(position-1);      offerdata = string.valueof(obj);       intent intent = new intent(mainactivity.this, offerinfo.class);      intent.putextra("offerdata", offerdata);      intent.putextra("position", position);      startactivity(intent); } 

no need of loop while getting position directly


Comments

Popular posts from this blog

How to run C# code using mono without Xamarin in Android? -

c# - SharpSsh Command Execution -

python - Specify path of savefig with pylab or matplotlib -