arraylist - Why my hashmap is null Android? -
i trying value hashmap (from listviewrestaurants) can pass latitude , longitude mapsactivity reason hashmap return null ... can me.
public class listviewrestaurants extends activity { static listview listview; static arraylist<hashmap<string, string>> arrlist; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_list); listview = (listview) findviewbyid(r.id.listiew); arrlist = new arraylist<hashmap<string, string>>(); string json_str = getjsondata(); try { jsonarray jarray = new jsonarray(json_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 map1.put("id", json.getstring("id")); map1.put("businessname", json.getstring("businessname")); map1.put("addressline1", json.getstring("addressline1")); map1.put("addressline2", json.getstring("addressline2")); map1.put("addressline3", json.getstring("addressline3")); map1.put("postcode", json.getstring("postcode")); map1.put("ratingvalue", json.getstring("ratingvalue")); map1.put("ratingdate", json.getstring("ratingdate")); map1.put("distancekm", json.getstring("distancekm")); map1.put("latitude", json.getstring("latitude")); map1.put("longitude", json.getstring("longitude")); int name = json.getint("ratingvalue"); if(name == 5){ map1.put("image", string.valueof(r.drawable.ic_launcher_web)); } else if(name==4){ //map1.put("image", string.valueof(r.drawable.ic_launcher_web)); } else if(name==3){ // map1.put("image", string.valueof(r.drawable.ic_launcher_web)); } else if(name==2){ // map1.put("image", string.valueof(r.drawable.ic_launcher_web)); } else if(name==1){ // map1.put("image", string.valueof(r.drawable.ic_launcher_web)); } else if(name==0){ // map1.put("image", string.valueof(r.drawable.ic_launcher_web)); } else if(name==-1){ map1.put("ratingvalue", "exempt"); } // adding hashlist arraylist arrlist.add(map1); // intent intent = new intent(this,mapsactivity.class); // intent.putextra("map", map1); } } catch (jsonexception e) { e.printstacktrace(); } if (!arrlist.isempty()) { listadapter adapter = new simpleadapter(this, arrlist, r.layout.list_item, new string[]{"id", "businessname", "addressline1", "addressline2", "addressline3", "postcode", "ratingvalue", "ratingdate", "distancekm", "image"}, new int[]{r.id.restaurantid, r.id.businessname, r.id.adr1, r.id.adr2, r.id.adr3, r.id.postcode, r.id.rating, r.id.ratingdate, r.id.distance, r.id.image}); listview.setadapter(adapter); } } private string getjsondata() { strictmode.setthreadpolicy(new strictmode.threadpolicy.builder() .detectdiskreads() .detectdiskwrites() .detectnetwork() // or .detectall() detectable problems .penaltylog() .build()); string str = ""; intent intent = getintent(); string latitude = intent.getstringextra("lat"); string longtitude = intent.getstringextra("lon"); httpresponse response; httpclient myclient = new defaulthttpclient(); httppost myconnection = new httppost("http://sandbox.kriswelsh.com/hygieneapi/hygiene.php?op=s_loc&lat="+latitude+"&long="+longtitude); try { response = myclient.execute(myconnection); str = entityutils.tostring(response.getentity(), "utf-8"); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } return str; } }
here trying retrieve details hashamp (latitude , longitude) listviewrestuarants null values.
import android.content.intent; import android.support.v4.app.fragmentactivity; import android.os.bundle; import android.util.log; import com.google.android.gms.maps.cameraupdatefactory; import com.google.android.gms.maps.googlemap; import com.google.android.gms.maps.onmapreadycallback; import com.google.android.gms.maps.supportmapfragment; import com.google.android.gms.maps.model.latlng; import com.google.android.gms.maps.model.markeroptions; import java.util.hashmap; public class mapsactivity extends fragmentactivity implements onmapreadycallback { private googlemap mmap; static string x; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_maps); // obtain supportmapfragment , notified when map ready used. supportmapfragment mapfragment = (supportmapfragment) getsupportfragmentmanager() .findfragmentbyid(r.id.map); mapfragment.getmapasync(this); //get value array class intent intent = getintent(); hashmap<string, string> hashmap = (hashmap<string, string>)intent.getserializableextra("map"); system.out.println(hashmap) // log.v("hashmaptest", hashmap.get("longtitude")); } /** * manipulates map once available. * callback triggered when map ready used. * can add markers or lines, add listeners or move camera. in case, * add marker near sydney, australia. * if google play services not installed on device, user prompted install * inside supportmapfragment. method triggered once user has * installed google play services , returned app. */ @override public void onmapready(googlemap googlemap) { mmap = googlemap; // add marker in sydney , move camera latlng sydney = new latlng(-34, 151); mmap.addmarker(new markeroptions().position(sydney).title(x)); mmap.movecamera(cameraupdatefactory.newlatlng(sydney)); } }
this main class display tab view (for more information)
import android.app.tabactivity; import android.content.intent; import android.os.bundle; import android.widget.tabhost; public class mainactivity extends tabactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // create tabhost contain tabs tabhost tabhost = (tabhost)findviewbyid(android.r.id.tabhost); tabhost.tabspec tab1 = tabhost.newtabspec("first tab"); tabhost.tabspec tab2 = tabhost.newtabspec("second tab"); // set tab name , activity // opened when particular tab selected tab1.setindicator("restaurant"); tab1.setcontent(new intent(this, listviewrestaurants.class)); tab2.setindicator("map"); tab2.setcontent(new intent(this, restaurantmap.class)); /** add tabs tabhost display. */ tabhost.addtab(tab1); tabhost.addtab(tab2); } }
the issue setting different key in map trying fetch value corresponding different key -
map1.put("longitude", json.getstring("longitude")); //setting "longitude" log.v("hashmaptest", hashmap.get("longtitude")); //getting "longtitude"
apparently, there no key "longtitude
" in map.
best practice have constants keys , use insertion , retrieval , avoid such issues , not waste time debugging in also.
Comments
Post a Comment