package com.example.josn_test;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
//{
// "_id": "56e7d93c531da44310cbcd73",
// "goodsname": "爱家庭-少儿健康保障B计划",
// "shelfstate": "1",
// "goodstype": "意健险",
// "age": "7周岁-17周岁",
// "planners": [
// {
// "coverage": "5万元",
// "name": "意外伤害保险金"
// },
// {
// "coverage": "5万元",
// "name": "疾病身故保险金"
// },
// {
// "coverage": "10万元",
// "name": "重大疾病保险金"
// },
// {
// "coverage": "3.5万元",
// "name": "住院医疗费用补偿"
// }
// ]
//}
public class MainActivity extends Activity {
private ListView lv;
private ArrayList<String> list_string;
private MyPagerAdater myPagerAdater;
private String json = "{" +
" \"_id\": \"56e7d93c531da44310cbcd73\", " +
" \"goodsname\": \"爱家庭-少儿健康保障B计划\", " +
" \"shelfstate\": \"1\", " +
" \"goodstype\": \"意健险\", " +
" \"age\": \"7周岁-17周岁\", " +
" \"planners\": [ " +
" { " +
" \"coverage\": \"5万元\", " +
" \"name\": \"意外伤害保险金\" " +
" }, " +
" { " +
" \"coverage\": \"5万元\", " +
" \"name\": \"疾病身故保险金\" " +
" }, " +
" { " +
" \"coverage\": \"10万元\", " +
" \"name\": \"重大疾病保险金\" " +
" }, " +
" { " +
" \"coverage\": \"3.5万元\", " +
" \"name\": \"住院医疗费用补偿\" " +
" } " +
"] " +
" }";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lv);
jsonTest();
myPagerAdater = new MyPagerAdater();
lv.setAdapter(myPagerAdater);
}
private void jsonTest() {
try {
JSONObject jsonObject = new JSONObject(json);
String id = jsonObject.getString("_id");
String goods_name = jsonObject.getString("goodsname");
String shelfstate = jsonObject.getString("shelfstate");
String goodstype = jsonObject.getString("goodstype");
String age = jsonObject.getString("age");
JSONArray jsonArray = jsonObject.getJSONArray("planners");
list_string = new ArrayList<>();
list_string.add(id);
list_string.add(goods_name);
list_string.add(shelfstate);
list_string.add(goodstype);
list_string.add(age);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject2 = (JSONObject) jsonArray.get(i);
String coverage = jsonObject2.getString("coverage");
String name = jsonObject2.getString("name");
list_string.add(coverage);
list_string.add(name);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
public class MyPagerAdater extends BaseAdapter {
@Override
public int getCount() {
return list_string.size();
}
@Override
public Object getItem(int position) {
return list_string.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = getLayoutInflater().inflate(R.layout.item, null);
viewHolder.textView = (TextView) convertView.findViewById(R.id.item);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.textView.setText(list_string.get(position));
return convertView;
}
}
final static class ViewHolder {
TextView textView;
}
}
json数据解析并显示在listView上
最新推荐文章于 2018-11-01 18:52:42 发布