If your application stores data locally and you are planning to transfer data across different platforms (e.g. between mobile apps and websites, it is a good idea to store the data in JSON format. In this Android tip, I am going to show you how to read JSON array from the assets directory to display in a ListView.
Now in the assets directory of your current working project, you create a JSON file called jsondata.json. In the file, you have JSON data as shown below. There is a JSON array. In the JSON array, there are three JSON objects. Each object contains information (id, name, and sex) of a student. So, there are three students in the JSON file that will be displayed in the ListView.
[{"id":"100","sex":"M","name":"Andrew"},{"id":"101","sex":"F","name":"Stephie"}, {"id":"102","sex":"M","name":"Kalvin"}]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
tools:context=".MainActivity"
>
<ListView
android:id="@+id/listv"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
>
<TextView
android:id="@+id/txtid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<TextView
android:id="@+id/txtname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<TextView
android:id="@+id/txtsex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</LinearLayout>
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
public class ListAdapter extends ArrayAdapter<JSONObject>{
int vg;
ArrayList<JSONObject> list;
Context context;
public ListAdapter(Context context, int vg, int id, ArrayList<JSONObject> list){
super(context,vg, id,list);
this.context=context;
this.vg=vg;
this.list=list;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(vg, parent, false);
TextView txtId=(TextView)itemView.findViewById(R.id.txtid);
TextView txtName=(TextView)itemView.findViewById(R.id.txtname);
TextView txtSex=(TextView)itemView.findViewById(R.id.txtsex);
try {
txtId.setText(list.get(position).getString("id"));
txtName.setText(list.get(position).getString("name"));
txtSex.setText(list.get(position).getString("sex"));
} catch (JSONException e) {
e.printStackTrace();
}
return itemView;
}
}
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
JSONArray jsonArray=getJSonData("jsondata.json");
ArrayList<JSONObject> listItems=getArrayListFromJSONArray(jsonArray);
ListView listV=(ListView)findViewById(R.id.listv);
ListAdapter adapter=new ListAdapter(this,R.layout.list_layout,R.id.txtid,listItems);
listV.setAdapter(adapter);
}
private JSONArray getJSonData(String fileName){
JSONArray jsonArray=null;
try {
InputStream is = getResources().getAssets().open(fileName);
int size = is.available();
byte[] data = new byte[size];
is.read(data);
is.close();
String json = new String(data, "UTF-8");
jsonArray=new JSONArray(json);
}catch (IOException e){
e.printStackTrace();
}catch (JSONException je){
je.printStackTrace();
}
return jsonArray;
}
private ArrayList<JSONObject> getArrayListFromJSONArray(JSONArray jsonArray){
ArrayList<JSONObject> aList=new ArrayList<JSONObject>();
try {
if (jsonArray != null) {
for (int i = 0; i < jsonArray.length(); i++) {
aList.add(jsonArray.getJSONObject(i));
}
}
}catch (JSONException je){je.printStackTrace();}
return aList;
}
}
|
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.