2017-04-07 80 views
-1

我在解析json字符串之後在我的散列映射中排序修剪值時遇到問題。如何對散列值中的修剪值進行排序

//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 
{ 
    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 is a variable for a listview 
    } 
} 
catch (JSONException e) 
{ 
    Log.e("log_tag", "Error parsing data "+e.toString()); 
} 

以下是結果

list of installed apps and their categories (Click to view)

我需要整理的修整值幫助,使排序的數據可以在自己的類別。 click here

這裏是我的JSON代碼

public class now 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 to send to webservice 
     JSONObject jsonObjectSend = new JSONObject(); 
     JSONArray jsonArrayPakages = new JSONArray(); 
     PackageManager packageManager; 
     List<ResolveInfo> listApps; //this list store all 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); 

      for (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 to 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 from (//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 is not valid: " + e.getMessage(), e); 
     } 
     return null; 
} 
+0

你有固定數量的分類清單嗎? –

+0

不,我沒有@patel ..它提取在線類別... – vitus

+0

@vitus可以請你分享你的json格式,你想只顯示每個應用程序在它的類別下,而不是在未分類列表下嗎? –

回答

0
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 is a variable for a listview 
    } 

這裏你正在爲每個項目做HashMap中。但是,你需要做的只是首先檢查HashMap到已經添加的類別,如果是,然後添加到現有列表中,否則創建新列表。

HashMap<String, List<String>> mylist = new HashMap<String, List<String>>(); 
for (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); 
    } 

順便說一句,你還需要你的ListView適配器類中,以位調整,但(我認爲)。

+0

請詳述@Vayalu ..感謝您的答覆 – vitus

+0

我試過了我認爲我知道的一切@當我聲明這個HashMap > mylist = new HashMap(,List ); – vitus

+0

已更新的答案。 –

相關問題