如何在哈希映射中对修剪后的值进行排序

时间:2017-04-07 11:02:09

标签: java android

在解析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;
}

1 个答案:

答案 0 :(得分: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 Adapter类中调整一下(我认为)。

相关问题