![](/img/trans.png)
[英]Having a trouble getting List of Strings from array.xml file and parsing them to array of floats android
[英]Android: Compare strings in array list and combine them
我想比較從我的數據庫中獲取的數組列表中的字符串,並將它們連接在一起。這是從我的數據庫中收集數據的代碼。
public List<String> getData2List() {
String[] columns = new String[]{ KEY_ROWID, KEY_DATE, KEY_NAME, KEY_PRICE};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, "1", null, null, null, null);
List<String> results = new ArrayList<String>();
int iCM = c.getColumnIndex(KEY_DATE);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
results.add(c.getString(iCM));
}
return results;
}
這是將它們放置在列表中的代碼。
Database info = new Database(this);
info.open();
List<String> dates = info.getData2List();
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, dates));
info.close();
這一切都很好,但是如果有多個相同的條目,我最終會列出相同的東西(如果這有意義!?)。
示例:如果列表像{"01/01/13", "02/01/13", "01/01/13", "03/02/13", "01/01/13"}
試圖使輸出像{"01/01/13", "02/01/13", "03/02/13"}
以便將所有相同值的條目編譯成一個。
任何幫助或想法,我們將不勝感激。
只需使用Set而不是List。
HashSet將為您提供唯一的字符串,而List可以包含相同的字符串。
為什么不只使用HashSet<String>
?
例:
List<String> dates = info.getData2List();
Set<String> uniqueDates = new HashSet<String>(dates);
您需要一個Set集合類,基本上“ Set”的作用是,僅存儲唯一值,如果嘗試設置集合中已存在的新值,它將被忽略,請確保您覆蓋了equals和hashCode方法如果您要存儲自己的對象,請查看文檔以獲取有關其工作方式的更多信息。
http://docs.oracle.com/javase/6/docs/api/java/util/Set.html
問候!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.