android - ListView works in emulator, but not in phone -
i tried make simple application take api internet, rearrange them , show list.
in emulator works. phone shows white screen. phone has android 2.3.7. when tried on tablet, app stopped working. tablet has android 4.1.
mainactivity
public class mainactivity extends listactivity implements onitemclicklistener{ private progressdialog pdialog; private string url = "http://www.cscpro.org/secura/market/"; //food-29-5.json; private string res; private string ql; jsonarray market; final static string resources = "res"; final static string quality = "q"; private static final string tag_offer = "offer"; private static final string tag_price = "price"; private static final string tag_saller = "seller"; private static final string tag_saller_name = "name"; arraylist<hashmap<string, string>> listsaller; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); listsaller = new arraylist<hashmap<string, string>>(); intent in = getintent(); res = in.getstringextra(resources); ql = in.getstringextra(quality); listview lv = getlistview(); lv.setonitemclicklistener(this); // calling async task json new getcontacts().execute(); } @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { string rate = ((textview) view.findviewbyid(r.id.rate)).gettext().tostring(); string name = ((textview) view.findviewbyid(r.id.name)).gettext().tostring(); // starting single contact activity intent in = new intent(getapplicationcontext(), singlecontactactivity.class); in.putextra(tag_price, rate); in.putextra(tag_saller_name, name); startactivity(in); } private class getcontacts extends asynctask<void, void, void> { @override protected void onpreexecute() { super.onpreexecute(); // showing progress dialog pdialog = new progressdialog(mainactivity.this); pdialog.setmessage("please wait..."); pdialog.setcancelable(false); pdialog.show(); } @override protected void doinbackground(void... arg0) { // creating service handler class instance servicehandler sh = new servicehandler(); // making request url , getting response string myurl = url+res+"-29-"+ql+".json"; string jsonstr = sh.makeservicecall(myurl, servicehandler.get); log.d("response: ", "> " + jsonstr); if (jsonstr != null) { try { jsonobject jsonobj = new jsonobject(jsonstr); // getting json array node market = jsonobj.getjsonarray(tag_offer); // looping through contacts (int = 0; < market.length(); i++) { jsonobject c = market.getjsonobject(i); string rate = c.getstring(tag_price); // phone node json object jsonobject saller = c.getjsonobject(tag_saller); string name = saller.getstring(tag_saller_name); // tmp hashmap single contact hashmap<string, string> sallers = new hashmap<string, string>(); // adding each child node hashmap key => value sallers.put(tag_price, rate); sallers.put(tag_saller_name, name); // adding contact contact list listsaller.add(sallers); } } catch (jsonexception e) { e.printstacktrace(); } } else { log.e("servicehandler", "couldn't data url"); } return null; } @override protected void onpostexecute(void result) { super.onpostexecute(result); // dismiss progress dialog if (pdialog.isshowing()) pdialog.dismiss(); /** * updating parsed json data listview * */ listadapter adapter = new simpleadapter( mainactivity.this, listsaller, r.layout.list_item, new string[] { tag_price, tag_saller_name}, new int[] { r.id.rate, r.id.name}); setlistadapter(adapter); } } }
singlecontactactivity
public class singlecontactactivity extends activity { // json node keys private static final string tag_price = "price"; private static final string tag_saller_name = "name"; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_single_contact); // getting intent data intent in = getintent(); // json values previous intent string rate = in.getstringextra(tag_price); string name = in.getstringextra(tag_saller_name); // displaying values on screen textview lblname = (textview) findviewbyid(r.id.name_label); textview lblrate = (textview) findviewbyid(r.id.rate_label); lblname.settext(name); lblrate.settext(rate); } }
activity_main.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <!-- main listview give id value list(@android:id/list) --> <listview android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </linearlayout>
activity_single_contact.xml simple linearlayout
2 textview
s.
Comments
Post a Comment