我有一個Arraylist
的HashMap
。每個HashMap
元素包含兩列:列名和相應的值。這HashMap
將被添加到ListView
與3 TextView
。無法正確地從Hashmap中獲取數據
我填充ArrayList
如下,然後賦值給一個適配器,以顯示它:
ArrayList<HashMap<String, String>> list1 = new ArrayList<HashMap<String, String>>();
HashMap<String, String> addList1;
for (int i = 0; i < count; i++) {
addList1 = new HashMap<String, String>();
addList1.put(COLUMN1, symbol[i]);
addList1.put(COLUMN2, current[i]);
addList1.put(COLUMN3, change[i]);
list1.add(addList1);
RecentAdapter adapter1 = new RecentAdapter(CompanyView.this,
CompanyView.this, list1);
listrecent.setAdapter(adapter1);
}
。
現在listItemClick
,提取的數據是不同形式在不同時間。
例如,我的列表包含以下數據:
ABC 123 1
PQR 456 4
XYZ 789 7
即當我登錄點擊第一個列表項後所取得的字符串,我得到了一些成果之一:
{1 = ABC,2 = 123,3 = 1}
{首先= ABC,二= 123,第三= 1}
{1 = 123,0 = ABC,2 = 1}
甚至 {27 = 123,28 = 1,26 = ABC}
最初我使用:
int pos1 = item.indexOf("1=");
int pos2 = item.indexOf("2=");
int pos3 = item.indexOf("3=");
String symbol = item.substring(pos1 + 2,pos1 - 2).trim();
String current = item.substring(pos2 + 2, pos3 - 2).trim();
String change = item.substring(pos3 + 2, item.length() - 1).trim();
然後,對於第4的情況下,我必須使用:
int pos1 = item.indexOf("26=");
int pos2 = item.indexOf("27=");
int pos3 = item.indexOf("28=");
String symbol = item.substring(pos1 + 3, item.length() - 1).trim();
String current = item.substring(pos2 + 3, pos3 - 3).trim();
String change = item.substring(pos3 + 3, pos1 - 3).trim();
這樣我得到ABC
在symbol
等等。
但是,通過這種方法,應用程序完全失去了它的可靠性。
我也試過
while (myVeryOwnIterator.hasNext()) {
key = (String) myVeryOwnIterator.next();
value[ind] = (String) addList1.get(key);
}
但它不會放棄應有的價值。相反,它會爲例如返回隨機符號。 ABC或PQR或XYZ。
我做錯了什麼?
在此先感謝!