2012-01-19 3 views
0

ArraylistHashMap입니다. 각 HashMap 요소에는 열 이름과 해당 값의 두 열이 포함됩니다. 이 HashMapListView에 3 TextView으로 추가됩니다. 해시 맵에서 데이터를 올바르게 가져올 수 없습니다.

나는 다음과 같이 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 = 123}

{1, 0 = ABC, 2 = 1}

01,235,

심지어 {= 123 27 = 1 28 26 = ABC}

처음에 I 사용 :

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 일 경우에, I 사용할 가지고

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 함수는 특정 순서로 값을 삽입하지 않습니다.그래서 가장 좋은 방법은 여기에 사용을

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

을의 ArrayList에서의 HashMap의 키 집합을 넣어 값

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의 인덱스를 사용하여 검색 할 때하는 것입니다, ArrayList의 listKeySet은 사용 HashMap 키가 삽입되는 순서를 보존합니다. HashMap에 데이터를 넣으면 키를 ArrayList에 삽입하십시오.

1

나는이 목적으로 HashMap을 사용하는 것이 좋다고 생각하지 않습니다. 나는

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; 
    } 
} 

같은 데이터를 incapsulating 클래스를 구현하고 당신은 물론, 대신 HashMap<String,String>의 MYDATA를 사용하려면 어댑터를 변경해야합니다

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); 

처럼 사용할 수 있습니다.

관련 문제