[英]How can I sort data in a hashmap for my android app?
I am trying to sort the arr.get() values in ascending order in my listdataHeader so that it prints out the listDataheader in the form: name1 1.0 name3 2.0 name5 3.0 name2 4.0 etc 我试图在我的listdataHeader中以升序对arr.get()值进行排序,以便它以以下形式打印出listDataheader:name1 1.0 name3 2.0 name5 3.0 name2 4.0等
rather than in my predefined format 而不是我的预定义格式
I tried to use sort the hashmap but the app just crashes when i open this pane, how can the listdataheader in ascending order of the randomly generated array value? 我尝试使用对哈希图进行排序,但是当我打开此窗格时,应用程序只会崩溃,如何以随机生成的数组值的升序排列listdataheader?
package com.example.listview;
import info.androidhive.expandablelistview.R;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.Toast;
public class MainActivity extends Activity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the listview
expListView = (ExpandableListView) findViewById(R.id.lvExp);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
// Listview Group click listener
expListView.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// Toast.makeText(getApplicationContext(),
// "Group Clicked " + listDataHeader.get(groupPosition),
// Toast.LENGTH_SHORT).show();
return false;
}
});
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition),
Toast.LENGTH_SHORT).show();
}
});
// Listview Group collasped listener
/*expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();
}
});*/
// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
}
/*
* Preparing the list data
*/
String[] stations = {"shell","Esso","Tesco","Asda","BP","Texaco",};
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
ArrayList<Float> arr = new ArrayList<Float> ();
for (int i = 0 ; i <= 12 ; i++)
{
arr.add(i, (float) (((Math.random() * 90) + 10)/10)); // add random values to array
}
// Adding child data
listDataHeader.add("Shell distance: "+ arr.get(0));
listDataHeader.add("Esso Distance:" +arr.get(1));
listDataHeader.add("Tesco Distance "+arr.get(2));
listDataHeader.add("Asda Distance "+arr.get(3));
listDataHeader.add("BP Distance "+arr.get(4));
listDataHeader.add("Texaco Distance "+arr.get(5));
// Adding child data
List<String> shell = new ArrayList<String>();
shell.add("Distance: "+arr.get(0));
shell.add("Fuel price: 141p/litre");
shell.add("Petrol price: 134p/litre");
//esso details
List<String> esso = new ArrayList<String>();
esso.add("Distance: "+arr.get(1));
esso.add("Fuel price: 145p/litre");
esso.add("Petrol price: 131p/litre");
//tesco
List<String> tesco_energy = new ArrayList<String>();
tesco_energy.add("Distance: "+arr.get(2));
tesco_energy.add("Fuel price: 144p/litre");
tesco_energy.add("Petrol price: 139p/litre");
//asda
List<String> asda = new ArrayList<String>();
asda.add("Distance: "+arr.get(3));
asda.add("Fuel price: 139p/litre");
asda.add("Petrol price: 131p/litre");
//bp
List<String> bp = new ArrayList<String>();
bp.add("Distance: "+arr.get(4));
bp.add("Fuel price: 144p/litre");
bp.add("Petrol price: 139p/litre");
//texaco
List<String> texaco = new ArrayList<String>();
texaco.add("Distance: "+arr.get(5));
texaco.add("Fuel price: 144p/litre");
texaco.add("Petrol price: 139p/litre");
listDataChild.put(listDataHeader.get(0), shell); // Header, Child data
listDataChild.put(listDataHeader.get(1), esso);
listDataChild.put(listDataHeader.get(2), tesco_energy);
listDataChild.put(listDataHeader.get(3), asda);
listDataChild.put(listDataHeader.get(4), bp);
listDataChild.put(listDataHeader.get(5), texaco);
/*HashMap<String, Double> sorted = new HashMap<String, Double>();
sorted = sortHashMap((HashMap<String, Double>) listDataHeader);
for (String cityName : sorted.keySet()){
System.out.println(cityName + " " + sorted.get(cityName));
}*/
}
//sort map
private HashMap<String, Double> sortHashMap(HashMap<String, Double> input){
Map<String, Double> tempMap = new HashMap<String, Double>();
for (String wsState : input.keySet()){
tempMap.put(wsState,input.get(wsState));
}
List<String> mapKeys = new ArrayList<String>(tempMap.keySet());
List<Double> mapValues = new ArrayList<Double>(tempMap.values());
HashMap<String, Double> sortedMap = new LinkedHashMap<String, Double>();
TreeSet<Double> sortedSet = new TreeSet<Double>(mapValues);
Object[] sortedArray = sortedSet.toArray();
int size = sortedArray.length;
for (int i=0; i<size; i++){
sortedMap.put(mapKeys.get(mapValues.indexOf(sortedArray[i])),
(Double)sortedArray[i]);
}
return sortedMap;
}
} }
I guess you want to sort by value. 我想您想按值排序。 It this case you can do something like this:
在这种情况下,您可以执行以下操作:
public Map<String, Double> sortMap(Map<String, Double> unsortMap) {
List<Map.Entry<String, Double>> list = new LinkedList<Entry<String, Double>>(
unsortMap.entrySet());
// sort list based on comparator
Collections.sort(list, new Comparator<Object>() {
@SuppressWarnings("unchecked")
public int compare(Object o1, Object o2) {
return ((Map.Entry<String, Double>) o1).getValue().compareTo(
((Map.Entry<String, Double>) o2).getValue());
}
});
// put sorted list into map again
Map<String, Double> sortedMap = new LinkedHashMap<String, Double>();
for (Iterator<Entry<String, Double>> it = list.iterator(); it.hasNext();) {
Map.Entry<String, Double> entry = it.next();
sortedMap.put(entry.getKey(), entry.getValue());
}
return sortedMap;
}
However, if you want to sort by key change: 但是,如果要按密钥更改进行排序:
return ((Map.Entry<String, Double>) o1).getValue().compareTo(
((Map.Entry<String, Double>) o2).getValue());
to 至
return ((Map.Entry<String, Double>) o1).getKey().compareTo(
((Map.Entry<String, Double>) o2).getKey());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.