2012-01-19 36 views
0

我有一個ArraylistHashMap。每個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(); 

這樣我得到ABCsymbol等等。

但是,通過這種方法,應用程序完全失去了它的可靠性

我也試過

while (myVeryOwnIterator.hasNext()) { 

key = (String) myVeryOwnIterator.next(); 
value[ind] = (String) addList1.get(key); 
       } 

但它不會放棄應有的價值。相反,它會爲例如返回隨機符號。 ABC或PQR或XYZ。

我做錯了什麼?

在此先感謝!

回答

2

HashMap的put函數不會按特定順序插入值。所以最好的辦法是把HashMap中的鍵集中在一個ArrayList和在檢索值

ArrayList<HashMap<String, String>> list1 = new ArrayList<HashMap<String, String>>(); 
HashMap<String, String> addList1; 
ArrayList<String> listKeySet; 
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]); 

listKeySet.add(COLUMN1); 
listKeySet.add(COLUMN2); 
listKeySet.add(COLUMN3); 

list1.add(addList1); 

RecentAdapter adapter1 = new RecentAdapter(CompanyView.this, 
       CompanyView.this, list1); 
listrecent.setAdapter(adapter1); 
} 

使用ArrayList的索引和檢索時使用

addList1.get(listKeySet.get(position)); 

這裏,只是使用ArrayList中listKeySet保存HashMap鍵的插入順序。將數據放入HashMap時,將該鍵插入ArrayList。

1

我不認爲使用HashMap爲此目的是個好主意。我將執行類incapsulating您的數據,如

class myData { 
    public String Column1; 
    public String Column2; 
    public String Column3; 
    // better idea would be making these fields private and using 
    // getters/setters, but just for the sake of example these fields 
    // are left public 

    public myData(String col1, String col2, String col3){ 
     Column1 = col1; 
     Column2 = col2; 
     Column3 = col3; 
    } 
} 

,並使用它像

ArrayList<myData> list1 = new ArrayList<myData>(); 
for (int i = 0; i < count; i++) { 
    list1.add(new myData(symbol[i], current[i], change[i])); 
} 
//no need to create new adapter on each iteration, btw 
RecentAdapter adapter1 = new RecentAdapter(CompanyView.this, 
       CompanyView.this, list1); 
listrecent.setAdapter(adapter1); 

您需要在您的適配器,而不是使用HashMap<String,String> myData的,當然變化。