java插入HashMap会弄乱排序
基于这个类
public class Record {
public static final String TABLE_NAME = "records";
public static final String COLUMN_ID = "id";
public static final String COLUMN_LONGITUDE = "longitude";
public static final String COLUMN_LATITUDE = "latitude";
public static final String COLUMN_SPEED = "speed";
public static final String COLUMN_TIMESTAMP = "timestamp";
private int id;
private String longitude;
private String latitude;
private String speed;
private String timestamp;
public static final String CREATE_TABLE =
"CREATE TABLE " + TABLE_NAME + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ COLUMN_LONGITUDE + " TEXT,"
+ COLUMN_LATITUDE + " TEXT,"
+ COLUMN_SPEED + " TEXT,"
+ COLUMN_TIMESTAMP + " TIMESTAMP DEFAULT (DATETIME('now','localtime'))"
+ ")";
public Record(){
}
public Record(int id, String longitude, String latitude, String speed, String timestamp){
this.id = id;
this.longitude = longitude;
this.latitude = latitude;
this.speed = speed;
this.timestamp = timestamp;
}
public int getId(){
return id;
}
public String getLongitude(){
return longitude;
}
public String getLatitude(){
return latitude;
}
public String getSpeed(){
return speed;
}
public String getTimestamp(){
return timestamp;
}
public void setId(int id){
this.id = id;
}
public void setLongitude(String longitude){
this.longitude = longitude;
}
public void setLatitude(String latitude){
this.latitude = latitude;
}
public void setSpeed(String speed){
this.speed = speed;
}
public void setTimestamp(String timestamp){
this.timestamp = timestamp;
}
}
我已经创建了我的SQLite模型
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "speeds";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(Record.CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + Record.TABLE_NAME);
onCreate(db);
}
public void insertRecord(String longitude, String latitude, String speed){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Record.COLUMN_LONGITUDE, longitude);
values.put(Record.COLUMN_LATITUDE, latitude);
values.put(Record.COLUMN_SPEED, speed);
System.out.println("VALUEEEEEEEES" + values);
db.insert(Record.TABLE_NAME, null, values);
db.close();
}
public List<Record> getAllRecords(){
List<Record> records = new ArrayList<>();
String selectQuery = "SELECT * FROM " + Record.TABLE_NAME + " ORDER BY " + Record.COLUMN_TIMESTAMP + " DESC";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if(cursor.moveToFirst()){
do{
Record record = new Record();
record.setId(cursor.getInt(cursor.getColumnIndex(Record.COLUMN_ID)));
record.setLongitude(cursor.getString(cursor.getColumnIndex(Record.COLUMN_LONGITUDE)));
record.setLatitude(cursor.getString(cursor.getColumnIndex(Record.COLUMN_LATITUDE)));
record.setSpeed(cursor.getString(cursor.getColumnIndex(Record.COLUMN_SPEED)));
record.setTimestamp(cursor.getString(cursor.getColumnIndex(Record.COLUMN_TIMESTAMP)));
records.add(record);
} while (cursor.moveToNext());
}
db.close();
return records;
}
public List<Record> getLastTenRecords(){
List<Record> records = new ArrayList<>();
String selectQuery = "SELECT * FROM " + Record.TABLE_NAME + " ORDER BY " + Record.COLUMN_TIMESTAMP + " DESC LIMIT 10";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if(cursor.moveToFirst()){
do{
Record record = new Record();
record.setId(cursor.getInt(cursor.getColumnIndex(Record.COLUMN_ID)));
record.setLongitude(cursor.getString(cursor.getColumnIndex(Record.COLUMN_LONGITUDE)));
record.setLatitude(cursor.getString(cursor.getColumnIndex(Record.COLUMN_LATITUDE)));
record.setSpeed(cursor.getString(cursor.getColumnIndex(Record.COLUMN_SPEED)));
record.setTimestamp(cursor.getString(cursor.getColumnIndex(Record.COLUMN_TIMESTAMP)));
records.add(record);
} while (cursor.moveToNext());
}
db.close();
return records;
}
}
现在,我试图显示ExpandableListView
中的一些数据。在我的场景中,我按COLUMN_TIMESTAMP
对sql查询的响应进行排序,并尝试以HashMap<String , List<String>
类型对其进行解析,以便ExpandableListView
能够识别它
问题是,即使对结果进行了排序(我在逐个循环和打印时可以看到),在我使用此方法解析并放入HashMap后,排序也消失了
public static HashMap<String, List<String>> getLastTenData(Context context) {
HashMap<String, List<String>> expandableListDetail = new HashMap<>();
DatabaseHelper databaseHelper = new DatabaseHelper(context);
List<Record> recordList = databaseHelper.getLastTenRecords();
for(int i=0; i<recordList.size(); i++){
List<String> myList = new ArrayList<>();
myList.add("Y: "+recordList.get(i).getLatitude());
myList.add("X: " + recordList.get(i).getLongitude());
myList.add("Km/h: "+recordList.get(i).getSpeed());
expandableListDetail.put(recordList.get(i).getTimestamp(), myList);
System.out.println("check "+String.valueOf(i)+" "+recordList.get(i).getTimestamp());
}
System.out.println(expandableListDetail);
return expandableListDetail;
}
我的意思是expandableListDetail
在应该按键排序时没有按键排序。
有什么帮助吗
# 1 楼答案
如果你已经回答了一个类似的问题(关于
HashSet
)here类似地,
HashMap
也是无序的,因此向它添加值,然后在映射上迭代,将不会按照最初添加的顺序生成项目为什么
LinkedHashMap
解决了这个问题?(从@Deadpool了解到这个想法)为什么
TreeMap
解决了这个问题?(从@tomgeraghty3中学习这个想法)