How to create a custom ListView In Android? -


here mainactivity.java contains listview.

public class mainactivity extends activity {      string maintext[] = { "item 1", "item 2", "item 3", "item 4", "item 5",             "item 1", "item 2", "item 3", "item 4", "item 5", "item 1",             "item 2", "item 3", "item 4", "item 5" };     string text[] = { "342", "3542", "3523", "23654", "3545", "342", "3542",             "3523", "23654", "3545", "342", "3542", "3523", "23654", "3545",             "342", "3542", "3523", "23654", "3545" };     listview list;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         list = (listview) findviewbyid(r.id.listview1);          custadapter adp = new custadapter(mainactivity.this, maintext, text);         list.setadapter(adp);     } } 

here custadapter adapter class it's working, when scroll last item, shows null pointer error.

public class custadapter extends baseadapter {     context context;     string maintext[];     string text[];      public custadapter(context context, string maintext[], string text[]) {         // todo auto-generated constructor stub          this.context = context;         this.maintext = maintext;         this.text = text;     }      @override     public int getcount() {         // todo auto-generated method stub         return maintext.length;     }      @override     public object getitem(int arg0) {         // todo auto-generated method stub         return maintext[arg0];     }      @override     public long getitemid(int arg0) {         // todo auto-generated method stub         return arg0;     }       imageview imgv;     textview maintext1;     textview text1;      @override     public view getview(int arg0, view arg1, viewgroup arg2) {         // todo auto-generated method stub         if (arg1 == null) {             layoutinflater inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service);              arg1 = inflater.inflate(r.layout.listview, null);              imgv=(imageview) arg1.findviewbyid(r.id.imageview1);              maintext1=(textview) arg1.findviewbyid(r.id.textview1);              text1=(textview) arg1.findviewbyid(r.id.textview2);              imgv.setbackgroundresource(r.drawable.ic_launcher);              maintext1.settext(maintext[arg0]);              text1.settext(text[arg0]);          } else {             arg1 = (view)arg1.gettag();         }          return arg1;     }  } 

getview() of baseadapter should looks this:

 @override  public view getview(final int position, view convertview, viewgroup parent) {      if (convertview == null) {             holder = new viewholder();             convertview = mlayoutinflater.inflate(r.layout.row_push_notification, null);                 // bind views                 holder.txttitle = (textview) convertview.findviewbyid(r.id.txttitle);                 holder.txtdatetime = (textview) convertview.findviewbyid(r.id.txtdatetime);                  // set tag                 convertview.settag(holder);             } else {                 // tag                 holder = (viewholder) convertview.gettag();             }              holder.txttitle.settext(your_one_array[position]);             holder.txtdatetime.settext(your_two_array[position]);         return convertview;   } 

viewholder class in baseadapter.

private class viewholder {      private textview txttitle, txtdatetime; } 

hope you.


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -