如何从android中的json对象呈现和选择联系人?
Posted
技术标签:
【中文标题】如何从android中的json对象呈现和选择联系人?【英文标题】:How to render and select contacts from json object in android? 【发布时间】:2015-10-10 00:25:26 【问题描述】:我从服务器收到响应为 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);
【问题讨论】:
【参考方案1】:请为您的数据创建一个 Java pojo 类。例如: 类人 字符串标识; 字符串名称; boolean is selected;// 知道这个联系人是否被选中
将所有 Person 对象添加到 Arraylist
如果复选框被选中,则将 isSelected 设置为 true,反之亦然,对于位于该位置的 Person 对象。然后您可以将您的联系人发送到服务器。
我希望这会有所帮助。
【讨论】:
【参考方案2】:就像 Priya Singhal 所说的那样:
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 个项目。 希望对您有所帮助!
【讨论】:
以上是关于如何从android中的json对象呈现和选择联系人?的主要内容,如果未能解决你的问题,请参考以下文章
如何从android中的url解析多个JSON对象和数组?我正在使用一个示例,我希望在该示例中使用它,
如何从 android 的电话簿中选择联系人号码到我的应用程序中?
如何从 java / android 中的嵌套 json 对象访问数据? [复制]