java - how to set different views dynamically in getView() of adpater in android -


i creating submit form getting data server, form may have different views (edittext, checkbox, radiobutton, ratingbar) according server suggest. have created view in adapter every time scroll down or user's answer cleared (like checked item unchecked automatically, edittext cleared etc) , views automatically exchanged position first last or swapped. don't know how solve that, here getview method:

@override     public view getview(int position, view convertview, viewgroup parent) { if (convertview == null) {             layoutinflater inflater = context.getlayoutinflater();             rowview = inflater.inflate(r.layout.customratinglist, parent, false);             final viewholder holder = new viewholder();             holder.tvquestion = (textview) rowview.findviewbyid(r.id.txtquestion);             holder.tvquestion.settag(list.get(position));             holder.layout = (linearlayout) rowview.findviewbyid(r.id.lnquestion);             holder.layout.settag(position);             rowview.settag(holder);          } else {              rowview = convertview;             ((viewholder) rowview.gettag()).layout.settag(position);         }         final viewholder viewholder = (viewholder) rowview.gettag();         viewholder.tvquestion.settypeface(font);         viewholder.tvquestion.settext(list.get(position).getquestion());         viewholder.layout.removeallviews();         switch (list.get(position).getans_type_id()) {             case "1":                 ratingbar = new ratingbar(context);                 ratingbar.setnumstars(5);                 ratingbar.setstepsize(0.1f);                 ratingbar.setlayoutparams(new linearlayout.layoutparams(linearlayout.layoutparams.wrap_content, linearlayout.layoutparams.match_parent));                  viewholder.layout.addview(ratingbar);                  ratingbar.setonratingbarchangelistener(new ratingbar.onratingbarchangelistener() {                     @override                     public void onratingchanged(ratingbar ratingbar, float rating, boolean fromuser) {                           answersetter((int) (viewholder.layout.gettag()), string.valueof(rating));                       }                 });                   break;               case "2":                   edittext = new edittext(context);                 edittext.setlayoutparams(new linearlayout.layoutparams(                         linearlayout.layoutparams.match_parent,                         linearlayout.layoutparams.match_parent));                 edittext.settextcolor(color.black);                  viewholder.layout.addview(edittext);                  edittext.addtextchangedlistener(new textwatcher() {                     @override                     public void beforetextchanged(charsequence s, int start, int count, int after) {                      }                      @override                     public void ontextchanged(charsequence s, int start, int before, int count) {                           answersetter((int) (viewholder.layout.gettag()), string.valueof(s));                       }                      @override                     public void aftertextchanged(editable s) {                       }                 });                   break; }  return rowview; } 

can please?

the solution using these 2 methods :

@override public int getitemviewtype(int position) {     return integer.parseint(list.get(position).getans_type_id()); }  @override public int getviewtypecount() {     return 2; } 

after can use getitemviewtype method inside of getview method position passed param in getview method.

for more info please take tutorial

here documentation these methods.


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -