1

I am parsing json for the link and I want to show the data after parsing in a list view by using only ArrayLists and Array Adapter.

But I am getting an only the last value of the index.

Here is my code

JSONPARSE.java

public class JsonParse extends Activity {

    ArrayList<contactsGS> contacts = new ArrayList<contactsGS>();

    ListView lv_contacts;
    InputStream is = null;
    String jsonString;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_json_parse);

        lv_contacts = (ListView)findViewById(R.id.lv_jsonParse);

        is = CommonUtilJson.getInputStream("http://api.androidhive.info/contacts/");
        jsonString = CommonUtilJson.inputStreamToString(is);
        ParseCode parse = new ParseCode(jsonString);
        contacts=parse.parseValues();

        ArrayAdapter adapter = new ArrayAdapter<contactsGS>(this, android.R.layout.simple_list_item_1,contacts);
        lv_contacts.setAdapter(adapter);

    }

    public String getJsonSring(String api_url) throws URISyntaxException,
    ClientProtocolException, IOException {
        try {
            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet();
            URL url = new URL(api_url);
            URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(),
                    url.getQuery(), null);
            request.setURI(uri);

            HttpResponse response = client.execute(request);
            InputStream ips = response.getEntity().getContent();
            BufferedReader buf = new BufferedReader(new InputStreamReader(ips));

            StringBuilder sb = new StringBuilder();
            String s;
            while (true) {
                s = buf.readLine();
                if (s == null || s.length() == 0)
                    break;
                sb.append(s);

            }
            buf.close();
            ips.close();
            return sb.toString();

        }

        finally {
            // any cleanup code...
        }

    }   
    }

Here is the class where I have parsed the code.

PARSECODE.java

    public class ParseCode {

        String jsonString;
        public static String url = "http://api.androidhive.info/contacts/";
        ArrayList<contactsGS> contactsData = new ArrayList<contactsGS>();
        ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String,String>>();
        contactsGS gtrSeter = new contactsGS();
        public ParseCode(String jsonString){
            this.jsonString = jsonString;
        }

        JSONObject jsonObj = null;

        public ArrayList<contactsGS> parseValues(){

            try{

                jsonObj = new JSONObject(jsonString);

                JSONArray jsonArray = jsonObj.getJSONArray("contacts");
                for(int index =0 ; index<jsonArray.length();index++){

                    JSONObject contacts = jsonArray.getJSONObject(index);

                    String id = contacts.getString("id");
                    gtrSeter.setId(id);
                    String name = contacts.getString("name");
                    gtrSeter.setName(name);
                    String email = contacts.getString("email");
                    gtrSeter.setEmail(email);
                    String address = contacts.getString("address");
                    gtrSeter.setAddress(address);
                    String gender = contacts.getString("gender");

                    JSONObject phoneObj = contacts.getJSONObject("phone");
                    String mobile = phoneObj.getString("mobile");
                    gtrSeter.setMobile(mobile);
                    String home = phoneObj.getString("home");
                    String office = phoneObj.getString("office");


                   contactsData.add(gtrSeter);

                }
            }
            catch(Exception e){

                e.printStackTrace();
            }
            return contactsData;
        }

    }

Here is my Getter Setter Class

ContactGS.java

public class contactsGS {

    public String id;
    public String name;
    public String email;
    public String address;
    public String mobile;

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getMobile() {
        return mobile;
    }
    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    private static contactsGS singletonObject;

    public static contactsGS getSingletonObject(){

        if(singletonObject==null){
            singletonObject = new contactsGS();
        }
        return singletonObject;
    }

}

Please help me. I want to get the details of one person in one row of the list view.

Thanks in advance

RevanthKrishnaKumar V.
  • 1,855
  • 1
  • 21
  • 34
Gaurav Arora
  • 8,282
  • 21
  • 88
  • 143

2 Answers2

3

Add every contactGS to arraylist in the loop, create ContactGS in the for loop

contactsGS gtrSeter = new contactsGS();

add this line at the end of the for loop

contactsData.add(gtrSeter );

like this

for(int index =0 ; index<jsonArray.length();index++){

   contactsGS gtrSeter = new contactsGS();
   JSONObject contacts = jsonArray.getJSONObject(index);

   String id = contacts.getString("id");
   gtrSeter.setId(id);
   String name = contacts.getString("name");
   gtrSeter.setName(name);
   String email = contacts.getString("email");
   gtrSeter.setEmail(email);
   String address = contacts.getString("address");
   gtrSeter.setAddress(address);
   String gender = contacts.getString("gender");

   JSONObject phoneObj = contacts.getJSONObject("phone");
   String mobile = phoneObj.getString("mobile");
   gtrSeter.setMobile(mobile);
   String home = phoneObj.getString("home");
   String office = phoneObj.getString("office");

   contactsData.add(gtrSeter);

}
RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
  • RajaReddy is right, you are just reusing the same instance and you need to create a new instance and put it in the list on every iteration – digitaljoel Sep 27 '12 at 04:27
  • Ok Raja, It helped me but I am only getting the references in the list view example contacsGS@44f6e150... and so on now, How to get the values, where I am doing a mistake? – Gaurav Arora Sep 27 '12 at 04:28
  • Raja, I have done the same, but I am not getting the values. I am simply getting these things in list view - contactsGS@4567238,contactsGS@756345g,... and so on?? – Gaurav Arora Sep 27 '12 at 04:36
  • this instance will change the process contactsGS gtrSeter = new contactsGS();, and show me your adapter class – RajaReddy PolamReddy Sep 27 '12 at 04:38
  • I have already shown the adapter class above in the code. Please have a look Raja. Its in the JSONPARSE class – Gaurav Arora Sep 27 '12 at 04:43
  • ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,contacts); lv_contacts.setAdapter(adapter); – Gaurav Arora Sep 27 '12 at 04:45
  • this is adapter declaration, this is not adapter code. when your using custom values to display in list view it requires custom adapter also ..look at this link and make changes according to your requirement..http://stackoverflow.com/a/12603117/964741 – RajaReddy PolamReddy Sep 27 '12 at 04:48
1

please change in your code as following.

in public class ParseCode

you defined, just declare here and initialize it every time in loop.

contactsGS gtrSeter = new contactsGS();

change with

contactsGS gtrSeter;

and in public ArrayList parseValues() method in for loop write first statement.

gtrSeter = new contactsGS();

so your code should like this.

for(int index =0 ; index<jsonArray.length();index++){
   gtrSeter = new contactsGS();
   JSONObject contacts = jsonArray.getJSONObject(index);
.
.
.
.
.
.
.
}
Jignesh Ansodariya
  • 12,583
  • 24
  • 81
  • 113