android - List is empty in arrayadapter -


i have arrayadapter in android follows :

public class smsarrayadapter extends arrayadapter<string> {      list<string> smsbody;     list<boolean> status;     list<string> time;     list<string> smsmessageid;      context context;     private static layoutinflater inflater = null;     string fromnumber;      public smsarrayadapter(context context, int resource, list<string> smsbody,             list<boolean> status, list<string> time, list<string> smsmessageid,             string fromnumber) {         super(context, resource, smsbody);         this.smsbody = smsbody;         this.status = status;         this.context = context;         inflater = (layoutinflater) context                 .getsystemservice(context.layout_inflater_service);         this.fromnumber = fromnumber;         this.time = time;         this.smsmessageid=smsmessageid;     }      public string getstr(int position) {         return smsbody.get(position);     }     public string getid(int position)     {         return smsmessageid.get(position);     }     public void setread(int position,string smsmessageid)     {         status.set(position, true);         contentvalues values = new contentvalues();         values.put("read", true);         context.getcontentresolver().update(uri.parse("content://sms/inbox"), values, "_id=" +smsmessageid, null);     }     @override     public string getitem(int position) {         // todo auto-generated method stub         return smsbody.get(position);     }      public static class viewholder {          public textview textfrom;         public textview text_sms;         public textview text_time;      }      @override     public view getview(int position, view convertview, viewgroup parent) {          viewholder holder;         if (convertview == null) {              /****** inflate tabitem.xml file each row ( defined below ) *******/             convertview = inflater.inflate(r.layout.row_item, null);              /****** view holder object contain tabitem.xml file elements ******/              holder = new viewholder();             holder.textfrom = (textview) convertview                     .findviewbyid(r.id.textview_from);             holder.textfrom.settext(" " + fromnumber);             holder.text_sms = (textview) convertview                     .findviewbyid(r.id.textview_sms);             string smstexttodisplay = smsbody.get(position);             if (smstexttodisplay.length() > 100)                 smstexttodisplay = smstexttodisplay.substring(0, 99) + " ...";              holder.text_sms.settext(smstexttodisplay);              holder.text_time = (textview) convertview                     .findviewbyid(r.id.textview_time);             holder.text_time.settext(time.get(position));             if (status.get(position) == false) {                 convertview.setbackgroundcolor(context.getresources().getcolor(                         r.color.light_blue_overlay));              }              /************ set holder layoutinflater ************/             convertview.settag(holder);         } else             holder = (viewholder) convertview.gettag();          return convertview;     }  } 

i have initialized arrayadapter follows :

arrayadapter = new smsarrayadapter(this, r.layout.row_item, smsbody,                 status, time, new arraylist<string>(), fromnumber);         smslistview.setadapter(arrayadapter); 

i want access item of list follows :

public void onitemclick(adapterview<?> parent, view view, int pos, long id) {   try {         string smsmessagestr = (string) arrayadapter.getitem(pos);         string smsmessageid = ((smsarrayadapter) arrayadapter).getid(pos);         ((smsarrayadapter) arrayadapter).setread(pos,smsmessageid);         intent intent = new intent(smsactivity.this,                 showindividualsms.class);         intent.putextra("sms", smsmessagestr);         startactivity(intent);      } catch (exception e) {         e.printstacktrace();         toast.maketext(this,"exception "+e.getmessage(), toast.length_long).show();      } } 

but exception : "invalid index 1 , size 0" . exception , have understood smsmessageid list empty . why ? how can solve error ?

when initialize adapter, give new arraylist<string>() smsmessageid. arraylist empty. somewhere between when initialize , when try access first element of smsmessageid have populate array elements.

you also, if wanted populate array later, modify getid if element trying didn't exist, returned falsey value. practice.

i don't have idea of program trying do, not having comments, i'm sorry can't more helpful.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -