-1

I get a response from server as json having name and contact number keys with their values. I want to display name and contact number details together with a check box as a column so that user can select multiple from those contacts and that contacts can be sent to the server on a button click.

My json file

{
"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\"}]"

}

I am right now putting contact and name in separate lists. What should I do to put check boxes there? And selecting and forwarding multiple contacts to the server?

 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);
        }
nerdy_me
  • 451
  • 1
  • 5
  • 7

2 Answers2

1
  1. Please make a Java pojo class for your data. For ex : class Person{ String Id; String name; boolean is selected;// to know whether this contact is selected

    }

  2. Add all Person object to Arraylist

  3. If check box is checked set isSelected to true and vice versa for Person object at that position .

Then u can send your contact to server.

I hope this helps.

Priya Singhal
  • 1,261
  • 11
  • 16
0

It would be like this as Priya Singhal says:

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: may be many of you know about the SparseArray object but may be others no. The advantages are that has, both ArrayList and HashMap behaviours, and is more efficiently that a HashMap (unless you have hundred of items). In this case is a good option because you only have 13 items. Hope it helps!

ARP
  • 531
  • 3
  • 8