英文:
Grouping data from JSON and display on a list
问题
我有一个类似这样的JSON数据:
{
"items": [
{
"kodeHp": "C-1",
"firstName": "iman",
"lastName": "firmansyah",
"email": "ggag@gmail.com"
},
{
"kodeHp": "C-2",
"firstName": "dini",
"lastName": "yyyy",
"email": "ggag@gmail.com"
},
{
"kodeHp": "C-1",
"firstName": "tanu",
"lastName": "xxxxx",
"email": "bb@gmail.com"
}
]
}
我想在列表上显示“firstName”,但按“kodeHp”对象排序。所以我的列表将如下所示:
{
"iman": "C1",
"tanu": "C1",
"dini": "C2"
}
这是我尝试过的:
private String parseItems(String jsonResponse) {
ArrayList<HashMap<String,String>> list = new ArrayList<>();
try {
JSONObject jObj = new JSONObject(jsonResponse);
JSONArray jArray = jObj.getJSONArray("items");
for(int i = 0; i < jArray.length(); i++){
JSONObject jo = jArray.getJSONObject(i);
String kodeHp = jo.getString("kodeHp");
String firstName = jo.getString("firstName");
String lastName = jo.getString("lastName");
String email = jo.getString("email");
HashMap<String, String> item = new HashMap<>();
if (!item.containsKey(kodeHp)){
item.get(kodeHp);
}
item.put("firstName", firstName);
item.put("lastName", lastName);
item.put("email",email);
item.put("kodeHp", kodeHp);
list.add(item);
}
} catch (JSONException e){
}
adapter = new SimpleAdapter(this, list, R.layout.item_list_row,
new String[]{"firstName", "kodeHp"}, new int[] {R.id.TvFirstName,R.id.TvKodeHp});
listView.setAdapter(adapter);
loading.dismiss();
return jsonResponse;
}
它可以显示我的JSON数据,但尚未按我想要的方式排序。有什么建议吗?
英文:
I have a JSON data like this:
{
"items": [
{
"kodeHp": "C-1",
"firstName": "iman",
"lastName": "firmansyah",
"email": "ggag@gmail.com"
},
{
"kodeHp": "C-2",
"firstName": "dini",
"lastName": "yyyy",
"email": "ggag@gmail.com"
},
{
"kodeHp": "C-1",
"firstName": "tanu",
"lastName": "xxxxx",
"email": "bb@gmail.com"
},
I want to display the "firstName" on a list but sorted by the "kodeHp" object. So my list gonna looks like this:
{
iman: C1
tanu: C1
dini: C2
}
This is what I have tried:
private String parseItems(String jsonResponse) {
ArrayList<HashMap<String,String>> list = new ArrayList<>();
try {
JSONObject jObj = new JSONObject(jsonResponse);
JSONArray jArray = jObj.getJSONArray("items"); //edited on gscript
for(int i = 0; i < jArray.length(); i++){
JSONObject jo = jArray.getJSONObject(i);
String kodeHp = jo.getString("kodeHp");
String firstName = jo.getString("firstName");
String lastName = jo.getString("lastName");
String email = jo.getString("email");
HashMap<String, String> item = new HashMap<>();
if (!item.containsKey(kodeHp)){
item.get(kodeHp);
}
item.put("firstName", firstName);
item.put("lastName", lastName);
item.put("email",email);
item.put("kodeHp", kodeHp);
list.add(item);
}
} catch (JSONException e){
}
adapter = new SimpleAdapter(this, list, R.layout.item_list_row,
new String[]{"firstName", "kodeHp"}, new int[] {R.id.TvFirstName,R.id.TvKodeHp});
listView.setAdapter(adapter);
loading.dismiss();
return jsonResponse;
}
It works to display my JSON data but not yet sorted like I want. Any suggestion?
答案1
得分: 2
要对列表进行排序,您可以使用 List.sort 方法,配合适当的 Comparator,例如使用 Comparator.comparing:
list.sort(Comparator.comparing(map -> map.get("kodeHp")));
请注意,这将按照 "kodeHp" 的值按字母顺序进行排序。还请注意,如果您的输入数据中可能存在 null 值,则必须添加处理方式。
如果您需要一个 Java 8 之前的解决方案,例如用于旧版本的 Android,您需要创建一个老式的匿名类,例如:
list.sort(new Comparator<Map<String,String>>() {
@Override
public int compare(Map<String,String> o1, Map<String,String> o2) {
return o1.get("kodeHp").compareTo(o2.get("kodeHp"));
}
});
英文:
To sort your list, you can use List.sort with an appropriate Comparator, for example using Comparator.comparing:
list.sort(Comparator.comparing(map -> map.get("kodeHp")));
Note that this will sort alphabetically by the values of "kodeHp". Also note that you will have to add handling for null values if they are possible in you input data.
In case you need a pre-Java8 solution, e.g. for old Android versions, you'll have to create an old-fashioned anonymous class instead, for example:
list.sort(new Comparator<Map<String,String>>() {
@Override
public int compare(Map<String,String> o1, Map<String,String> o2) {
return o1.get("kodeHp").compareTo(o2.get("kodeHp"));
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论