Listview Data from Local JSON in Android

Listview is an important UI component of the Android Application. In most of the application we need to use this listview and bind data dynamically.

First I will explain the JSON. How to load JSON from asset folder. Here I am attaching the sample JSON below:

  1. {  
  2.   "devices": [  
  3.     {  
  4.       "registeredDate""14-05-2015",  
  5.       "deviceName""Z3"  
  6.     },  
  7.     {  
  8.       "registeredDate""14-05-2015",  
  9.       "deviceName""XOLO Q500s IPS"  
  10.     },  
  11.     {  
  12.       "registeredDate""15-05-2015",  
  13.       "deviceName""RIM Z3"  
  14.     },  
  15.     {  
  16.       "registeredDate""17-05-2015",  
  17.       "deviceName""XOLO Q500s IPS"  
  18.     },  
  19.     {  
  20.       "registeredDate""26-05-2015",  
  21.       "deviceName""RM-1031_1010"  
  22.     },  
  23.     {  
  24.       "registeredDate""15-06-2015",  
  25.       "deviceName""RM-1019_1003"  
  26.     },  
  27.     {  
  28.       "registeredDate""18-03-2016",  
  29.       "deviceName""COLLPAD"  
  30.     }  
  31.   ]  
  32. }  
This JSON contains an array of some devices details like registered date and device name. We need to display these details in a listview.

This is the code to load JSON from asset folder.
  1. try  
  2. {  
  3.     BufferedReader reader = new BufferedReader(new InputStreamReader(getAssets().open("out.json")));  
  4.     String line = null;  
  5.     StringBuffer buffer = new StringBuffer();  
  6.     while ((line = reader.readLine()) != null)  
  7.     {  
  8.         buffer.append(line);  
  9.     }  
  10. }  
  11. catch (IOException e)  
  12. {  
  13.     e.printStackTrace();  
  14. }  
Now the buffer variable contains json data.

Next step we will create a model class for Devices. And we will declare the variable name as same as the JSON data.
  1. public class Devices  
  2. {  
  3.     private String deviceName = null;  
  4.     private String registeredDate = null;  
  5.     public String getName()  
  6.     {  
  7.         return deviceName;  
  8.     }  
  9.     public void setName(String name)  
  10.     {  
  11.         this.deviceName = name;  
  12.     }  
  13.     public String getDate()  
  14.     {  
  15.         return registeredDate;  
  16.     }  
  17.     public void setDate(String date)  
  18.     {  
  19.         this.registeredDate = date;  
  20.     }  
  21. }  
Now we will create another class for handling the model.
  1. public class Response  
  2. {  
  3.     private ArrayList < Devices > devices = null;  
  4.     public ArrayList < Devices > getDevices()  
  5.     {  
  6.         return devices;  
  7.     }  
  8.     public void setDevices(ArrayList < Devices > devices)  
  9.     {  
  10.         this.devices = devices;  
  11.     }  
  12. }  
Here comes the important part of the application, that is creating the Adapter for the listview. This will bind data to the UI and will show the contents on the listview. Please see below:
  1. public class ListAdapter extends BaseAdapter  
  2. {  
  3.     private Context mContext = null;  
  4.     private String json = null;  
  5.     private ArrayList < Devices > deveices = null;  
  6.     public ListAdapter(Context context, String json)  
  7.     {  
  8.         this.mContext = context;  
  9.         this.json = json;  
  10.         Response response = new Gson().fromJson(json, Response.class);  
  11.         deveices = response.getDevices();  
  12.     }  
  13.     @Override  
  14.     public int getCount()  
  15.     {  
  16.         return deveices.size();  
  17.     }  
  18.     @Override  
  19.     public Devices getItem(int arg0)  
  20.     {  
  21.         return deveices.get(arg0);  
  22.     }  
  23.     @Override  
  24.     public long getItemId(int arg0)  
  25.     {  
  26.         return 0;  
  27.     }  
  28.     @Override  
  29.     public View getView(int arg0, View view, ViewGroup arg2)  
  30.     {  
  31.         ViewHolder holder = null;  
  32.         if (view == null)  
  33.         {  
  34.             LayoutInflater inflater = LayoutInflater.from(mContext);  
  35.             view = inflater.inflate(R.layout.item, null);  
  36.             holder = new ViewHolder();  
  37.             holder.titleText = (TextView) view.findViewById(R.id.title);  
  38.             holder.sumaTextView = (TextView) view.findViewById(R.id.summary);  
  39.             view.setTag(holder);  
  40.         }  
  41.         else  
  42.         {  
  43.             holder = (ViewHolder) view.getTag();  
  44.         }  
  45.         holder.titleText.setText(getItem(arg0).getName());  
  46.         holder.sumaTextView.setText(getItem(arg0).getDate());  
  47.         return view;  
  48.     }  
  49.     class ViewHolder  
  50.     {  
  51.         private TextView titleText = null;  
  52.         private TextView sumaTextView = null;  
  53.     }  
  54. }  
Here the getView method we will set the data to the UI element.

Now we need to add set the list adapter to the listview.
  1. try  
  2. {  
  3.     BufferedReader reader = new BufferedReader(new InputStreamReader(getAssets().open("out.json")));  
  4.     String line = null;  
  5.     StringBuffer buffer = new StringBuffer();  
  6.     while ((line = reader.readLine()) != null)  
  7.     {  
  8.         buffer.append(line);  
  9.     }  
  10.     ListAdapter adapter = new ListAdapter(MainActivity.this, buffer.toString());  
  11.     list.setAdapter(adapter);  
  12. }  
  13. catch (IOException e)  
  14. {  
  15.     e.printStackTrace();  
  16. }