How to update the ratingbar in android -
trying update rating value obtained json rating bar
i achieved updating value textvie ............ how update value json rating rating bar
these classes have used
mainactivity.java
public class mainactivity extends activity { // declare variables jsonobject jsonobject; jsonarray jsonarray; listview listview; listviewadapter adapter; progressdialog mprogressdialog; arraylist<hashmap<string, string>> arraylist; static string name = "rank"; static string type = "country"; static string distance = "distance"; static string rating = "rating"; static string flag = "flag"; static string price= "price"; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // view listview_main.xml setcontentview(r.layout.listview_main); // locate listview in listview_main.xml listview = (listview) findviewbyid(r.id.listview); // execute downloadjson asynctask new downloadjson().execute(); } // downloadjson asynctask private class downloadjson extends asynctask<void, void, void> { @override protected void onpreexecute() { super.onpreexecute(); // create progressdialog mprogressdialog = new progressdialog(mainactivity.this); // set progressdialog title //mprogressdialog.settitle("fetching information"); // set progressdialog message mprogressdialog.setmessage("loading..."); mprogressdialog.setindeterminate(false); // show progressdialog mprogressdialog.show(); } @override protected void doinbackground(void... params) { // create array arraylist = new arraylist<hashmap<string, string>>(); // retrieve json objects given url address jsonobject = jsonfunctions.getjsonfromurl("url"); try { // locate array name in json jsonarray = jsonobject.getjsonarray("restaurants"); (int = 0; < jsonarray.length(); i++) { hashmap<string, string> map = new hashmap<string, string>(); jsonobject = jsonarray.getjsonobject(i); // retrive json objects map.put(mainactivity.name, jsonobject.getstring("restaurantname")); map.put(mainactivity.type, jsonobject.getstring("restauranttype")); map.put(mainactivity.flag, "http://54.218.73.244:7005/"+jsonobject.getstring("restaurantimage")); map.put(mainactivity.distance, jsonobject.getstring("restaurantdistance")); map.put(mainactivity.rating, jsonobject.getstring("restaurantrating")); map.put(mainactivity.price, jsonobject.getstring("restaurantprice")); // set json objects array arraylist.add(map); } } catch (jsonexception e) { log.e("error", e.getmessage()); e.printstacktrace(); } return null; } @override protected void onpostexecute(void args) { // pass results listviewadapter.java adapter = new listviewadapter(mainactivity.this, arraylist); // set adapter listview listview.setadapter(adapter); // close progressdialog mprogressdialog.dismiss(); } } }
listviewadapter.java
public class listviewadapter extends baseadapter { // declare variables context context; layoutinflater inflater; arraylist<hashmap<string, string>> data; imageloader imageloader; hashmap<string, string> resultp = new hashmap<string, string>(); public listviewadapter(context context, arraylist<hashmap<string, string>> arraylist) { this.context = context; data = arraylist; imageloader = new imageloader(context); } @override public int getcount() { return data.size(); } @override public object getitem(int position) { return null; } @override public long getitemid(int position) { return 0; } public view getview(final int position, view convertview, viewgroup parent) { // declare variables textview name; textview type; textview distance; textview rating; button price; imageview flag; ratingbar ratingbar; inflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); view itemview = inflater.inflate(r.layout.listview_item, parent, false); // position resultp = data.get(position); // locate textviews in listview_item.xml name = (textview) itemview.findviewbyid(r.id.restaurantnameid); type = (textview) itemview.findviewbyid(r.id.restauranttypeid); distance = (textview) itemview.findviewbyid(r.id.restaurantdistanceid); rating = (textview) itemview.findviewbyid(r.id.restaurantratingid); price=(button) itemview.findviewbyid(r.id.buybutton); ratingbar=(ratingbar) itemview.findviewbyid(r.id.ratingindicator); // locate imageview in listview_item.xml flag = (imageview) itemview.findviewbyid(r.id.flag); // capture position , set results textviews name.settext(resultp.get(mainactivity.name)); type.settext(resultp.get(mainactivity.type)); distance.settext(resultp.get(mainactivity.distance)); rating.settext(resultp.get(mainactivity.rating)); price.settext(resultp.get(mainactivity.price)); //ratingbar.setrating(resultp.get(mainactivity.rating)); // capture position , set results imageview // passes flag images url imageloader.class imageloader.displayimage(resultp.get(mainactivity.flag), flag); // capture listview item click itemview.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { // position resultp = data.get(position); intent intent = new intent(context, restaurantdesc.class); // pass data rank intent.putextra("rest", resultp.get(mainactivity.name)); // pass data country //intent.putextra("type", resultp.get(mainactivity.type)); // pass data flag //intent.putextra("flag", resultp.get(mainactivity.flag)); // start singleitemview class context.startactivity(intent); } }); return itemview; } }
listview_item.xml .
<ratingbar android:id="@+id/ratingindicator" style="?android:attr/ratingbarstylesmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/restaurantpicimagelinearviewid" android:layout_centerhorizontal="true" />
any ideas !
in onpostexecute()
:
ratingbar ratingbar = (ratingbar)findviewbyid(r.id.ratingindicator); ratingbar.setenabled(false); ratingbar.setmax(5); // assume 5 max rating ratingbar.setrating(integer.parseint(arraylist.get(mainactivity.rating)));
Comments
Post a Comment