Mahi Tej Gvp
Mahi Tej Gvp

Reputation: 1034

what is the best way to display JSON data in ListViews in android

I get a json array from my volley request

i get a list of Employees in json as a jsonObject what is the efficient way to display them in a ListView in android

 public class Employee{
        private  String Name;
        private  int Age,Pay;



    public  Movie(String Name,int Age ,int Pay){
        this.Name=Name;
        this.Age =Age ;
        this.Pay=Pay;
    }

    public String getName() {
        return Name;
    }

    public void setName(String Name) {
        this.Name= Name;
    }

    public int getAge() {
        return Age;
    }

    public void setAge(int Age) {
        this.Age= Age;
    }

    public int getPay() {
        return Pay;
    }

    public void setPay(int Pay) {
        this.Pay= Pay;
    }
}

in my json request i parse it as

     private List<Employee > empList= new ArrayList<Employee >();
     for (int i = 0; i < response.length(); i++) {
                            try {
     JSONObject obj = response.getJSONObject(i);
                                Employee emp= new Movie();
                                emp.setName(obj.getString("Name"));
                                emp.setAge((obj.getInt("Age"));
                                emp.setPay(obj.getInt("Pay"));
                                empList.add(movie);
}

My main concern is that do i have to really create these many objects for each employee entry , if i have to display 1000 employee details would it not be a concern to performance of the app

Upvotes: 1

Views: 191

Answers (3)

Bhadresh
Bhadresh

Reputation: 115

You should go with retrofit and Gson to avoid manual JSON parsing. And for performance enhancement you should use RecyclerView instead of ListView with lazy loading. If your data is too much large then you can divide in slot of 50 records and further request for load more data.

Upvotes: 0

Pushpendra
Pushpendra

Reputation: 2819

Yes you need to iterate the every object just like you are doing but if you don't want that overhead you can use retrofit with gson that will give you the object list directly(but behind the scene that also iterate every object).

Upvotes: 0

Mahan Azizmohammadi
Mahan Azizmohammadi

Reputation: 21

First of all, try using Gson for parsing your json. And if you want a better performance, try using RecyclerView instead of listview. Then you will have no problem to display 1000 employee as you said. Also using a constructor for your object is a better solution than setters.

Upvotes: 1

Related Questions