java - Android adapter pass JSON object data? -


very new java , android, think need layout inflater, tried implement failed, erased code.

i can turn object array of strings , use arrayadapter loop layout assigning strings textview

so arrayadapter limited

listadapter dribbblefeedadapter = new arrayadapter<string>(mainactivity.this, r.layout.feed_card, r.id.info_text, titlearr); 

i can pass in context, layout, textview, , array of strings.

what want pass image , other data views of layout. question how build adapter can assign values object view id?

public class mainactivity extends appcompatactivity {    @override   protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      // json , use adapter listview     ion.with(this)             .load("http://api.dribbble.com/shots/everyone")             .asjsonobject()             .setcallback(new futurecallback<jsonobject>() {               @override               public void oncompleted(exception e, jsonobject result) {                  // set titles array                 list<string> titles = new arraylist<string>();                  // build array                 jsonarray shots = result.getasjsonarray("shots");                 (int = 0; < shots.size(); i++) {                   string title = shots.get(i).getasjsonobject().get("title").getasstring();                   titles.add(title);                 }                 string[] titlearr = new string[titles.size()];                 titlearr = titles.toarray(titlearr);                  // todo: put more data feed_card                 //dribbbleimage.setimageresource(r.drawable.icon_1);                 listadapter dribbblefeedadapter = new arrayadapter<string>(mainactivity.this,                         r.layout.feed_card, r.id.info_text, titlearr);                 listview dribbbledatalistview = (listview) findviewbyid(r.id.dribbblefeedlistview);                 dribbbledatalistview.setadapter(dribbblefeedadapter);               }             });    }  } 

you should write own adapter extends baseadapter.

just code below:

public class testadapter extends baseadapter {      public testadapter(context context, list<data> datalist) {         this.datalist = datalist;         this.context = context;     }      private list<data> datalist;     private context context;      @override     public int getcount() {         return datalist.size();     }      @override     public object getitem(int position) {         return datalist.get(position);     }      @override     public long getitemid(int position) {         return position;     }      @override     public view getview(int position, view convertview, viewgroup parent) {          viewholder holder;         if (convertview == null) {             convertview = layoutinflater.from(context).inflate(layout.your_list_item_layout, parent, false);             holder = new view();             holder.tv_title = convertview.findviewbyid(r.id.title);             holder.iv_data = convertview.findviewbyid(r.id.data);             convertview.settag(holder);         }else {             holder = (viewholder)convertview.gettag();         }          data data = datalist.get(position);         holder.tv_title.settext(data.gettitle());          return convertview;     }      private class viewholder {         textview tv_title;         imageview iv_data;     } } 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -

jdbc - Not able to establish database connection in eclipse -