java - how to sort trimmed values in hash map -
i'm having trouble sorting trimmed values in hash map after parsing json string.
//read data string line; while ((line = bufferedreader.readline()) != null) { result.append(line); } jsonobject jsonobjectresult = new jsonobject(result.tostring().trim()); jsonarray jsonarrayapps = jsonobjectresult.getjsonarray("apps"); try { (int i=0; i<jsonarrayapps.length(); i++) { hashmap<string, string> map = new hashmap<string, string>(); jsonobject e = jsonarrayapps.getjsonobject(i); map.put("name", e.getstring("package").trim() ); map.put("cate", e.getstring("category").trim()); mylist.add(map); //mylist variable listview } } catch (jsonexception e) { log.e("log_tag", "error parsing data "+e.tostring()); }
the following results
list of installed apps , categories (click view)
i need in sorting out trimmed values sorted data can in own categories. click here
here json code
public class extends asynctask <httpurlconnection, void,void> { private void getappcategories() throws ioexception, jsonexception { bufferedreader bufferedreader = null; httpurlconnection urlconnection = null; bufferedwriter bufferedwriter = null; stringbuilder result = new stringbuilder(); //create json object send webservice jsonobject jsonobjectsend = new jsonobject(); jsonarray jsonarraypakages = new jsonarray(); packagemanager packagemanager; list<resolveinfo> listapps; //this list store app in device try { packagemanager = getactivity().getpackagemanager(); intent filterapp = new intent(intent.action_main); filterapp.addcategory(intent.category_launcher); listapps = packagemanager.queryintentactivities(filterapp, packagemanager.get_meta_data); (resolveinfo app : listapps) { jsonarraypakages.put(app.activityinfo.packagename.trim()); } jsonobjectsend.put("packages", jsonarraypakages); log.d("json", jsonobjectsend.tostring()); url url = new url("http://getdatafor.appspot.com/data?key=53972606b926d38191a5446fdff89e377873d767fabedf6d"); urlconnection = (httpurlconnection) url.openconnection(); urlconnection.setconnecttimeout(30000); /* milliseconds */ urlconnection.setreadtimeout(30000); /* milliseconds */ urlconnection.setrequestmethod("post"); urlconnection.setrequestproperty("content-type", "application-json"); urlconnection.setdooutput(true); /* allow output send data */ urlconnection.connect(); outputstream outputstream = urlconnection.getoutputstream(); bufferedwriter = new bufferedwriter(new outputstreamwriter(outputstream)); bufferedwriter.write(jsonobjectsend.tostring()); bufferedwriter.flush(); inputstream inputstream = urlconnection.getinputstream(); bufferedreader = new bufferedreader(new inputstreamreader(inputstream)); //continues (//read line) } } } @override protected void doinbackground(httpurlconnection... params) { try { getappcategories(); } catch (ioexception e) { log.d("tag", "net work error: " + e.getmessage(), e); } catch (jsonexception e) { log.d("tag", "json not valid: " + e.getmessage(), e); } return null; }
for (int i=0; i<jsonarrayapps.length(); i++) { hashmap<string, string> map = new hashmap<string, string>(); jsonobject e = jsonarrayapps.getjsonobject(i); map.put("name", e.getstring("package").trim() ); map.put("cate", e.getstring("category").trim()); mylist.add(map); //mylist variable listview }
here making hashmap each item. need first check hashmap category added if yes, added in existing list otherwise create new list.
hashmap<string, list<string>> mylist = new hashmap<string, list<string>>(); (int i=0; i<jsonarrayapps.length(); i++) { jsonobject e = jsonarrayapps.getjsonobject(i); string appname = e.getstring("package").trim(); string category = e.getstring("category").trim(); list<string> applist = mylist.get(category); if(applist == null) { applist = new arraylist<string>(); //add category if not exist mylist.put(category, applist); } applist.add(appname); }
btw, need bit tweak inside listview adapter class though (i think).
Comments
Post a Comment