Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How can we prepare JSON Array by using List View in Android

    • 0
    • 0
    • 0
    • 3
    • 0
    • 0
    • 0
    • 793
    Answer it

    I would like to prepare the JSON array by using the list view. Prepared the CustomListView and adapted the data and performing onClikclistener() to give input and set the data. Now trying to use data in list adapter with some custom data to prepare JSON array

    Sample JSON Array :

    "keyName": [{ "id": "2343", "name": "user_1", "value": "234" }, { "id": "2393", "name": "user_2", "value": "data_field" }]

    From list adapter i getting the value field data only. the other two fields getting by using position

 3 Answer(s)

  • Create custom Model/Bean Class and create ArrayList of Model/Bean class insted of String.
    Suppose you create Model Class with name MyData and params are id,name,email.


    Create Adapter with ArrayList.


    set Text to CustomView from Mydata object and when you click on any List Item then write code to get object of MyData ArryList from clicked item position. and do what do do with MyData.

  • Here is the code to create json array and json object.

    try {
                // outer json object
                JSONObject mainObject = new JSONObject();
                JSONArray arr=new JSONArray();
                for(int i=0;i<size;i++)// size is listview size
                {
                    JSONObject object=new JSONObject();
                    object.put("id","id"+i);// set id here 
                    object.put("name","name"+i); //set name here
                    object.put("value","value"+i); // set value here
                    arr.put(object);
                }
                mainObject.put("Array",arr);
           }catch(JsonException e)
          {
          } 
    

    If you want complete jsonobject than use mainObject If you want only jsonArray then use arr

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: