首先我们理解下思路,获取json解析后的数据通过listview显示出来.
第一步:我们要首先得到一个含有json数据的文件,这样我们才可以解析出来,获得到数据,创建一个本地的json数据
第二步:有了文件,我们要有获取到文件的方法readfile
第三步:解析json数据,返回一个map类型数组的数据
第四步:我们自定义个MyAdapter来显示出我们得到的l数组元素
好的 步骤我们已经讲解完了,接下来就是代码的实现过程
------------------------------------------------------------------------------
主布局的代码如下
<RelativeLayout 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" >
<ListView
android:id="@+id/main_listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
----------------------------------------------------------------------------------------------------------
我们还需要见一个list_item的布局文件,来填充我们的listview
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/list_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"/>
<LinearLayout
android:id="@+id/list_item_linearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/list_item_image"
android:orientation="vertical" >
<TextView
android:id="@+id/list_item_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/list_item_name" />
<TextView
android:id="@+id/list_item_student_num"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/list_item_student_num" />
<TextView
android:id="@+id/list_item_hobby"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/list_item_hobby" />
</LinearLayout>
</RelativeLayout>
----------------------------------------------------------------------------------------------
好了,以上就是我们所有的布局文件,下面是我们主程序
----------------------------------------------------------------------------------------------
读取jsonstr文件的方法,方法如下
/**
* 读取jsonstr文件的方法
*
* @param inputStream需要读取文件的输入流字符串
* @return
*/
private String readFile(InputStream inputStream) {
try {
// 用来写入interstream
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
// 设置每次读写数据的缓冲器
byte[] buffer = new byte[1024];
// 主要是用来设置最后一次丛书如流读出数据的长度
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
// 那读出来的数据写入outputstream
outputStream.write(buffer, 0, len);
}
byte[] resultByteArray = outputStream.toByteArray();
String result = new String(resultByteArray);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
-------------------------------------------------------------------------------------------------------
/**
* 解析Json字符串,返回一个map类型数组的数据
* @param jsonStr需要解析的数据
* @return
*/
private List<Map<String, String>> parseJsonArray(String jsonStr) {
try {
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
// 通过我们的字符串,创建出一个jsonobject对象
JSONObject jsonObject = new JSONObject(jsonStr);
// 从jsonObject中对象中拿到一个数组
JSONArray jsonArray = jsonObject.getJSONArray("students");
// 通过循环来拿到一个每个jsonarray中的数据
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
// 用来存放每一个object中的数据
Map<String, String> map = new HashMap<String, String>();
map.put("name", object.getString("name"));
map.put("num", object.getInt("num") + "");
map.put("hobby", object.getString("hobby"));
list.add(map);
}
return list;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
---------------------------------------------------------------------------------------------------------
/**
* 显示一个简单的jsonStr数组数据
*/
private void showJsonArray(String fileName) {
try {
InputStream inputStream = getResources().getAssets().open(fileName);
String jsonStr = readFile(inputStream);
// 解析字符串,list来显示
List<Map<String, String>> list = parseJsonArray(jsonStr);
adapter=new MyAdapter(MainActivity.this,list);
listview.setAdapter(adapter);
} catch (Exception e) {
}
}
---------------------------------------------------------------------------------------------------------------
/**
* 初始化控件
*/
private void initView() {
listview = (ListView) findViewById(R.id.main_listView);
}
/**
* 设置数据
*/
private void setData() {
try {
showJsonArray("jsonArray.txt");
} catch (Exception e) {
e.printStackTrace();
}
}
----------------------------------------------------------------------------------------
public class MainActivity extends Activity {
ListView listview;
List<Map<String,String>> list;
MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
setData();
}