2015-07-20 56 views
-1

我得到服務器的響應,因爲json的名稱和聯繫人號碼鍵與他們的值。我想顯示姓名和聯繫號碼詳細信息以及複選框作爲列,以便用戶可以從這些聯繫人中選擇多個聯繫人,並且可以通過單擊按鈕將聯繫人發送到服務器。如何渲染和選擇android中的json對象的聯繫人?

我的JSON文件

{ 
"users": "[{\"id\":1,\"name\":\"test_name\",\"contact\":\"23456543\",\"gender\":\"F\",\"age\":234,\"city\":\"delhi\",\"state\":\"india\",\"created_at\":\"2015-07-19T17:58:42.000Z\",\"updated_at\":\"2015-07-19T17:58:42.000Z\",\"district\":\"test_district\"},{\"id\":2,\"name\":\"test_name\",\"contact\":\"23456543\",\"gender\":\"F\",\"age\":234,\"city\":\"delhi\",\"state\":\"india\",\"created_at\":\"2015-07-19T17:58:42.000Z\",\"updated_at\":\"2015-07-19T17:58:42.000Z\",\"district\":\"test_district\"},{\"id\":3,\"name\":\"qwrrtt\",\"contact\":\"1234567890\",\"gender\":\"F\",\"age\":12,\"city\":\"Bokaro\",\"state\":\"Jharkhand\",\"created_at\":\"2015-07-19T18:01:16.000Z\",\"updated_at\":\"2015-07-19T18:01:16.000Z\",\"district\":\"Bokaro\"},{\"id\":4,\"name\":\"wetur\",\"contact\":\"1234567890\",\"gender\":\"F\",\"age\":22,\"city\":\"Bokaro\",\"state\":\"Jharkhand\",\"created_at\":\"2015-07-19T18:41:17.000Z\",\"updated_at\":\"2015-07-19T18:41:17.000Z\",\"district\":\"Bokaro\"},{\"id\":5,\"name\":\"tfjko\",\"contact\":\"1234567990\",\"gender\":\"F\",\"age\":34,\"city\":\"Bokaro\",\"state\":\"Jharkhand\",\"created_at\":\"2015-07-19T19:30:09.000Z\",\"updated_at\":\"2015-07-19T19:30:09.000Z\",\"district\":\"Bokaro\"},{\"id\":6,\"name\":\"tfjko\",\"contact\":\"1234567990\",\"gender\":\"F\",\"age\":34,\"city\":\"Bokaro\",\"state\":\"Jharkhand\",\"created_at\":\"2015-07-19T19:30:22.000Z\",\"updated_at\":\"2015-07-19T19:30:22.000Z\",\"district\":\"Bokaro\"},{\"id\":7,\"name\":\"fghjk\",\"contact\":\"4567890123\",\"gender\":\"F\",\"age\":45,\"city\":\"Bokaro\",\"state\":\"Jharkhand\",\"created_at\":\"2015-07-19T19:35:14.000Z\",\"updated_at\":\"2015-07-19T19:35:14.000Z\",\"district\":\"Bokaro\"},{\"id\":8,\"name\":\"cvbnm\",\"contact\":\"7894561203\",\"gender\":\"F\",\"age\":23,\"city\":\"Bokaro\",\"state\":\"Jharkhand\",\"created_at\":\"2015-07-19T19:37:42.000Z\",\"updated_at\":\"2015-07-19T19:37:42.000Z\",\"district\":\"Bokaro\"},{\"id\":9,\"name\":\"tfjko\",\"contact\":\"1234567990\",\"gender\":\"F\",\"age\":34,\"city\":\"Bokaro\",\"state\":\"Jharkhand\",\"created_at\":\"2015-07-19T19:53:14.000Z\",\"updated_at\":\"2015-07-19T19:53:14.000Z\",\"district\":\"Bokaro\"},{\"id\":10,\"name\":\"edgujn\",\"contact\":\"4894521360\",\"gender\":\"F\",\"age\":45,\"city\":\"Bokaro\",\"state\":\"Jharkhand\",\"created_at\":\"2015-07-20T02:45:01.000Z\",\"updated_at\":\"2015-07-20T02:45:01.000Z\",\"district\":\"Bokaro\"},{\"id\":11,\"name\":\"qwert\",\"contact\":\"4568217390\",\"gender\":\"F\",\"age\":45,\"city\":\"Bokaro\",\"state\":\"Jharkhand\",\"created_at\":\"2015-07-20T06:12:57.000Z\",\"updated_at\":\"2015-07-20T06:12:57.000Z\",\"district\":\"Bokaro\"},{\"id\":12,\"name\":\"surbhi\",\"contact\":\"1334567890\",\"gender\":\"F\",\"age\":12,\"city\":\"Bokaro\",\"state\":\"Jharkhand\",\"created_at\":\"2015-07-20T07:17:53.000Z\",\"updated_at\":\"2015-07-20T07:17:53.000Z\",\"district\":\"Bokaro\"},{\"id\":13,\"name\":\"preefu\",\"contact\":\"5641287092\",\"gender\":\"F\",\"age\":56,\"city\":\"Bokaro\",\"state\":\"Jharkhand\",\"created_at\":\"2015-07-20T07:23:54.000Z\",\"updated_at\":\"2015-07-20T07:23:54.000Z\",\"district\":\"Bokaro\"}]" 

}

我現在投入單獨的列表接觸和名稱。我該怎麼做把複選框放在那裏?並選擇並轉發多個聯繫人到服務器?

JSONObject ob = new JSONObject(strres); 
     List<String> allNames = new ArrayList<String>(); 

     JSONArray cast = ob.getJSONArray("users"); 
     for (int i=0; i<cast.length(); i++) 
     { 
      JSONObject actor = cast.getJSONObject(i); 
      String name = actor.getString("name"); 
      allNames.add(name); 
     } 

回答

0

這會是這樣的普里亞辛格爾說:

public class Actor { //simplified 

    private int id; 
    private String name; 

    public Actor(){} 

    public Actor(int id, String name) { 
     this.id = id; 
     this.name = name; 
    } 
} 

JSONObject json = new JSONObject(); 
SparseArray<Actor> actors = new SparseArray<Actor>(); 

JSONArray actorsJSON = null; 
try { 
    actorsJSON = json.getJSONArray("users"); 
    for (int i=0; i<actorsJSON.length(); i++){ 
     JSONObject actorJSON = actorsJSON.getJSONObject(i); 
     Actor actor = new Actor(actorJSON.getInt("id"), actorJSON.getString("name")); 
     actors.put(id, actor); 
    } 
} catch (JSONException e) { 
    e.printStackTrace(); 
} 

Offtopic:可能很多人都知道的SparseArray對象,但可能是別人沒有。它的優點是ArrayList和HashMap行爲,並且更有效率的是一個HashMap(除非你有數百個項目)。在這種情況下是一個不錯的選擇,因爲你只有13個項目。 希望它有幫助!

1
  1. 請一個Java POJO類爲您的數據。例如: class Person { String Id; 字符串名稱; 布爾選擇; //知道該聯繫人是否選擇

    }

  2. 添加所有Person對象的ArrayList

  3. 如果複選框組在isSelected爲真,反之爲Person對象那個位置。

則U可以發送您的聯繫到服務器。

我希望這會有所幫助。

相關問題