Json解析数据载入ListView中

代码片段,双击复制
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
public class JSONExample extends Activity {
        private static final String TAG = "JSONExample" ;
        private static final boolean D = true ;
  
        private static String mJsonData = "[{\"name\":\"zhangsan\"},{\"name\":\"lisi\"},{\"name\":\"wangwu\"}]" ;
        private ArrayList<String> mList = null ;
  
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super .onCreate(savedInstanceState);
                mList = new ArrayList<String>();
                try {
                        parse(mJsonData, mList);
                } catch (JSONException e) {
                        e.printStackTrace();
                }
                ListView lv = new ListView( this );
                lv.setLayoutParams( new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
//                lv.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1, mList));
                lv.setAdapter( new MyAdapter( this , mList));
                setContentView(lv);
  
        }
  
        /**
          * 解析JSON数据
         
          * @param content json数据
          * @param list
          * @throws JSONException
          */
        private void parse(String content, ArrayList<String> list)
                        throws JSONException {
                if (content == null || content.length() == 0 )
                        return ;
                if (list == null )
                        return ;
  
                // 此处是解析数组型的json
                JSONArray jsonArray = new JSONArray(content);
                if (jsonArray != null ) {
                        for ( int i = 0 ; i < jsonArray.length(); i++) {
                                // 如果你的json数据不是数组,直接使用下面的方式
                                // JSONObject jsonObj = new JSONObject(content);
                                JSONObject jsonObj = jsonArray.optJSONObject(i);
                                if (jsonObj == null )
                                        continue ;
  
                                String name = jsonObj.optString( "name" );
                                list.add(name);
                        }
                }
  
        }
          
        class MyAdapter extends BaseAdapter {
                private ArrayList<String> mList;
                private Context mContext;
                private LayoutInflater         mInflater;
                  
                public MyAdapter(Context context, ArrayList<String> list) {
                        mContext = context;
                        mList = list;
                        mInflater = LayoutInflater.from(context);
                }
  
                @Override
                public int getCount() {
                        return mList.size() == 0 ? 0 : mList.size();
                }
  
                @Override
                public Object getItem( int position) {
                        if (mList == null || mList.size() == 0 )
                                return null ;
                          
                        return mList.get(position);
                }
  
                @Override
                public long getItemId( int position) {
                        return position;
                }
  
                @Override
                public View getView( int position, View convertView, ViewGroup parent) {
                        if (convertView == null )
                                convertView = mInflater.inflate(android.R.layout.simple_list_item_1, null );
                          
                        String s = getItem(position).toString();
                        TextView tv = (TextView) convertView.findViewById(android.R.id.text1);
                        tv.setText(s);
                        return convertView;
                }
                  
        }
  
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值