2014-02-20 4 views
0

사용자가 세부 정보 (사람 세부 정보 - 이름, 성 등)를 입력하는 대화 상자가 거의 없으므로 세부 정보를 캡처하고 JSON으로 변환해야합니다. 사용자는 여러 명을 입력 할 수 있습니다. person1, person2, person3 (JSONObject이어야 함). 언제든지 처음으로 벨로우 함수가 호출 될 때 JSONObject personJSON1 = new JSONObject(); 두 번째로 호출 됨 JSONObject personJSON2 = new JSONObject(); 등등. 어떻게하면 좋을까요?JSONObject의 이름을 동적으로 변경하는 방법

private void personAdded() { 
JSONObject personJSON = new JSONObject(); 
     JSONArray personArrayjson = new JSONArray(); 
     JSONObject personObjectJson = new JSONObject(); 
     try {   
       personObjectJson.put("otherFirstName", sOtherFirstName); 
       personObjectJson.put("otherLastName", sOtherLastName); 
       personObjectJson.put("otherAddress", sOtherAddress); 
       personObjectJson.put("otherTown", sOtherTown); 
       personObjectJson.put("otherCounty", sOtherCounty); 
       personObjectJson.put("otherPostcode", sOtherPostcode); 
       personObjectJson.put("otherTelephone", sOtherTelephone); 
       personObjectJson.put("otherMobilePhone", sOtherMobilePhone);  
       personObjectJson.put("otherEmail", sOtherEmail); 
       personObjectJson.put("otherPersonInvolvement", sHowWasTheOtherPersonInvolved);   

     } catch (JSONException e) { 

      e.printStackTrace(); 
     } 
} 

많은 도움과 제안을 주셨습니다. 고맙습니다.

+0

당신은'JSONObject'의 목록을 사용할 수 있고 새로운 사람마다 목록에 새로운 요소를 추가 할 수 있습니다. – user3182577

+0

JSONObject – BRDroid

답변

1
public List<JSONObject> mMyList = new ArrayList<JSONObject>(); 

    private void personAdded() { 
     JSONObject personJSON = new JSONObject(); 
       JSONArray personArrayjson = new JSONArray(); 
       JSONObject personObjectJson = new JSONObject(); 
       try {   
         personObjectJson.put("otherFirstName", sOtherFirstName); 
         personObjectJson.put("otherLastName", sOtherLastName); 
         personObjectJson.put("otherAddress", sOtherAddress); 
         personObjectJson.put("otherTown", sOtherTown); 
         personObjectJson.put("otherCounty", sOtherCounty); 
         personObjectJson.put("otherPostcode", sOtherPostcode); 
         personObjectJson.put("otherTelephone", sOtherTelephone); 
         personObjectJson.put("otherMobilePhone", sOtherMobilePhone);  
         personObjectJson.put("otherEmail", sOtherEmail); 
         personObjectJson.put("otherPersonInvolvement", sHowWasTheOtherPersonInvolved);   
     mMyList.add(personJSON) 
       } catch (JSONException e) { 

        e.printStackTrace(); 
       } 

목록 구현을 수행하는 것은 이와 유사합니다.

+0

목록에 대한 링크 나 예제를 제안 해 주셔서 감사합니다. 이것은 내가 찾고 있었던 바로 그 것이다. 이 질문 하나. 사용자가 사람을 삭제할 수 있습니다.이 경우 삭제하려면 어떻게해야합니까? person1이 삭제되어야한다고 말할 수 있습니다. – BRDroid

+0

API 참조는 http://developer.android.com/reference/java/입니다. util/List.html – Submersed

+0

'mMyList.remove()'를 제거하려면 위치 나 JSONObject로 제거하면됩니다. – user3182577

관련 문제