2013-08-19 2 views
0

이 문제는 매우 간단하지만 주위를 둘러 볼 수 없습니다. 여기api의 hashMaps를 통해 루핑

protected Void doInBackground(Void... params) { 
      UserFunctions user = new UserFunctions(); 
      JSob = user.allprojects(); 
      try { 
       JSar = JSob.getJSONArray("data"); 

      } catch (JSONException e1) { 
       e1.printStackTrace(); 
      } 
      for(int i=0; i<JSar.length(); i++){ 
       try { 
        JSONObject newobj = JSar.getJSONObject(i); 
        project_title = newobj.getString("title"); 
        project_sector = newobj.getString("sector"); 

        all_list.put("title", project_title); 
        all_list.put("sector", project_sector); 

       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 

      } 
      return null; 
     } 

I합니다 (all_list는 해시 맵이다)에 HashMap에 노력하고 있습니다 :

다음 API는 그래서 내 doInBackground에서 나는 다음과 같은 코드를 실행하고

{ "data" : [ { "id" : "102", 
     "sector" : "projectSector1", 
     "title" : "projectTitle1" 
     }, 
     { "id" : "100", 
     "sector" : "projectSector2", 
     "title" : "projectTitle2" 
     }, 
     { "id" : "98", 
     "sector" : "projectSector3", 
     "title" : "projectTitle3" 
     } 
    ], 
    "status" : "success" 
} 

를 처리 할 수있다 "sector"및 "title"을 키로 사용하여 해당 값을 값으로 사용합니다. 하지만 몇 가지 이유 때문에 나는 projectTitle3과 projectSector3을 두 번 액세스 할 수 밖에 없습니다. 도와주세요 !

+0

내가 이렇게 당신이 해시 맵 – Pavlos

+0

@Pavlos에 키의 이전 값을 덮어 있다고 생각 수도 좋은 해결책이 될까요? –

+0

누군가가 아래에 답변했습니다. 귀하의 경우에는 대신 데이터베이스를 사용합니다! 특히 json이 아주 길면! – Pavlos

답변

2
다음과 같이 할 수

//Create a list of hashmap 
ArrayList<HashMap<String, String>> lst = new ArrayList<HashMap<String, String>>(); 

//in doInBackground method 
HashMap<String, String> all_list = new HashMap<String, String>();      
all_list.put("title", project_title); 
all_list.put("sector", project_sector); 
lst.add(map); 
+0

이 ArrayList를 사용하는 경우 ...이 활동에서 사용하는 listView에 대한 CustomAdapter를 어떻게 만듭니 까? –

+0

@ user2247689이 자습서는 http://www.androidbegin.com/tutorial/android-json-parse-images-and-texts-tutorial/에서 확인할 수 있습니다. –

+0

잘 구현 한 항목이지만 checkBox가 있으므로 customAdapter는 listView 클릭과 checkBox 클릭을 모두 활성화하는 데 필요합니다. –

1

HashMap에서 동일한 키를 사용하여 값을 재정의했기 때문입니다. 루프 반복마다 다른 키를 사용해야합니다.

키를 사용하여 i를 연결할 수도 있습니다. 예를 들어

는 :

for(int i=0; i<JSar.length(); i++){ 
       try { 
        JSONObject newobj = JSar.getJSONObject(i); 
        project_title = newobj.getString("title"); 
        project_sector = newobj.getString("sector"); 

        all_list.put("title"+i, project_title); 
        all_list.put("sector"+i, project_sector); 

       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 

      } 
1

당신은 HashMap에 동일한 키를 저장할 수 없습니다. 실제로 마지막 열쇠 만 저장할 수 있습니다. HashMap만의 키만 저장합니다. 이것은 단지 2 개의 열쇠 만 가지고있는 이유입니다. 그것을 해결하기 위해

는 수행

for(int i=0; i<JSar.length(); i++){ 
      try { 
       JSONObject newobj = JSar.getJSONObject(i); 
       project_title = newobj.getString("title"); 
       project_sector = newobj.getString("sector"); 

       all_list.put("title" + i, project_title); 
       all_list.put("sector" + i, project_sector); 

      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

     } 
관련 문제