json - Unable to add Icons to the first 3 list item in android ListView? -
recently, i've build app can retrieve results web via json. displays simple informations. thought myself, custom listview looked simple texts, want add icons left side of listitem first 3 list items only, things turn complicated,since i've got json data web. i'm finding difficult implement logic current code. afterall, want add icons first 3 itemlist. i've set sample code below (sorry untidy code!).
is possible set logic within current code ? or should start on again ? there solution ?
protected void onpostexecute(jsonobject jsonobject) { try { // locate array name in json jsonarray = jsonobject.getjsonarray("infolist"); world = new arraylist<infolistdata>(); // create array populate spinner worldlist = new arraylist<string>(); (int = 0; < jsonarray.length(); i++) { hashmap<string, string> map = new hashmap<string, string>(); jsonobject = jsonarray.getjsonobject(i); infolistdata worldpop = new infolistdata(); worldpop.set_loto_date(jsonobject.optstring("loto_date")); worldpop.set_info_id(jsonobject .optstring("takarakuji_id")); worldpop.set_numbertimes(jsonobject.optstring("id")); worldpop.set_title(jsonobject.optstring("title")); world.add(worldpop); worldlist.add(jsonobject.optstring("title") + "\n" + jsonobject.optstring("info_date")); } str = jsonarray.tostring(); try{ jsonarray jarray = new jsonarray(str); (int = 0; < jarray.length(); i++) { jsonobject json = null; json = jarray.getjsonobject(i); hashmap<string, string> map1 = new hashmap<string, string>(); // adding each child node hashmap key => value //altering map1.put("name", json.getstring("name")); string dates = json.getstring("loto_date"); //dates= dates.replace("-", "year"); dates = replacecharat(dates ,4,"year"); dates = replacecharat(dates ,7,"month"); dates = dates+="day"; map1.put("info_date",dates); string _titles = json.getstring("title"); map1.put("title", "("+_titles+")"); // adding hashlist arraylist// arrlist.add(map1); } } catch ( jsonexception e) { e.printstacktrace(); }
//i'm setting data in here,but i'm not sure how set icons current code
listview myspinner = (listview) findviewbyid(r.id.listviewpo); //try step 5 in here if(!arrlist.isempty()){ listadapter adapter = new simpleadapter(infolistactivity.this, arrlist, r.layout.customlistforinfo, new string[] {"name", "title", "info_date"}, new int[] {r.id.infoname,r.id.infontitle, r.id.dayk}); myspinner.setadapter(adapter); }
you have make own custom adapter listview add image in listview. going want extend arrayadapter. here code wrote while trying (i think). out bit. cheers
import java.util.arraylist; import android.content.context; import android.graphics.drawable.drawable; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.imageview; import android.widget.textview; public class standingsadapter extends arrayadapter<residentialcollege>{ public standingsadapter(context context, arraylist<residentialcollege> colleges) { super(context, r.layout.standings_row_layout, colleges); } @override public view getview(int position, view convertview, viewgroup parent){ layoutinflater inflater = layoutinflater.from(getcontext()); view view = inflater.inflate(r.layout.standings_row_layout, parent, false); residentialcollege college = (residentialcollege) getitem(position); textview collegeview = (textview) view.findviewbyid(r.id.standingstextview); collegeview.settext(getdisplayname(college.getname())); textview scoreview = (textview) view.findviewbyid(r.id.tyngscore); scoreview.settext("" + getdisplayscore(college.getscore())); imageview imageview = (imageview) view.findviewbyid(r.id.rescollegeimage); imageview.setimageresource(college.getimgresource()); return view; } //generate string display given residential college name avoid //long names messing display. e.g johnathan edwards private string getdisplayname(string name) { if(name.length() > 10){ return (name.substring(0, 7).touppercase() + " ..."); } else return name.touppercase(); } //to rid of .0's @ end of scores integers. //came @ cost of allowing scores have .5's forcing floating point numbers. private string getdisplayscore(double score){ if((score % 1) == 0){ return "" + score.intvalue(); } else{ return "" + score; } } public void updatestandings(arraylist<residentialcollege> newreslist) { this.clear(); for(residentialcollege res : newreslist){ this.add(res); } this.notifydatasetchanged(); } //perform simple insertion sort(only 12 elements, should fast enough) public arraylist<residentialcollege> sortbyscore(arraylist<residentialcollege> listtosort){ arraylist<residentialcollege> sorted = new arraylist<residentialcollege>(); sorted.add(listtosort.get(0)); listtosort.remove(0); for(residentialcollege college : listtosort){ for(int = 0; < sorted.size(); i++){ if(college.getscore() >= sorted.get(i).getscore()){ sorted.add(i, college); log.d("inserted:", college.getname() + " " + "at position " + i); break; } //add end has lowest score seen far. else if(i == (sorted.size() - 1)){ sorted.add(college); log.d("appended:", college.getname() + " " + "at position" + i); break; } } } return sorted; } }
Comments
Post a Comment