zoukankan      html  css  js  c++  java
  • ANDROID笔记:JSON和Adapter的使用

     1 package com.example.android_json;
     2 
     3 import java.io.BufferedReader;
     4 import java.io.IOException;
     5 import java.io.InputStream;
     6 import java.io.InputStreamReader;
     7 import java.io.UnsupportedEncodingException;
     8 
     9 import org.json.JSONArray;
    10 import org.json.JSONException;
    11 
    12 import android.app.Activity;
    13 import android.os.Bundle;
    14 import android.widget.ListView;
    15 
    16 public class MainActivity extends Activity {
    17 
    18     @Override
    19     protected void onCreate(Bundle savedInstanceState) {
    20         super.onCreate(savedInstanceState);
    21         setContentView(R.layout.activity_main);
    22         ListView listView = (ListView) findViewById(R.id.listview);
    23         
    24         InputStream inputStream = getResources().openRawResource(R.raw.person);
    25         BufferedReader bufferedReader;
    26         JSONArray jsonArray = null;
    27         try {
    28             bufferedReader = new BufferedReader(new InputStreamReader(
    29                     inputStream));
    30             StringBuffer stringBuffer = new StringBuffer();
    31             String line;
    32             while ((line = bufferedReader.readLine()) != null) {
    33                 stringBuffer.append(line);
    34             }
    35             // 实列化JSONArray对象
    36             jsonArray = new JSONArray(stringBuffer.toString());
    37 
    38         } catch (UnsupportedEncodingException e1) {
    39             e1.printStackTrace();
    40         } catch (IOException e) {
    41 
    42             e.printStackTrace();
    43         } catch (JSONException e) {
    44             e.printStackTrace();
    45         }
    46         listView.setAdapter(new ListViewAdapter(getApplicationContext(),
    47                 jsonArray));
    48     }
    49 
    50 }
     1 package com.example.android_json;
     2 
     3 import org.json.JSONArray;
     4 import org.json.JSONException;
     5 import org.json.JSONObject;
     6 
     7 import android.content.Context;
     8 import android.view.LayoutInflater;
     9 import android.view.View;
    10 import android.view.ViewGroup;
    11 import android.widget.BaseAdapter;
    12 import android.widget.TextView;
    13 
    14 public class ListViewAdapter extends BaseAdapter {
    15     LayoutInflater inflater;
    16     JSONArray jsonArray;
    17 
    18     public ListViewAdapter(Context context, JSONArray jsonArray) {
    19         this.jsonArray = jsonArray;
    20         inflater = LayoutInflater.from(context);
    21     }
    22 
    23     @Override
    24     public int getCount() {
    25         return jsonArray.length();
    26     }
    27 
    28     @Override
    29     public Object getItem(int position) {
    30         try {
    31             return jsonArray.get(position);
    32         } catch (JSONException e) {
    33             e.printStackTrace();
    34         }
    35         return null;
    36     }
    37 
    38     @Override
    39     public long getItemId(int position) {
    40         return position;
    41     }
    42 
    43     @Override
    44     public View getView(int position, View convertView, ViewGroup parent) {
    45         View v = inflater.inflate(R.layout.listview_item, null);
    46         TextView idTextView = (TextView) v.findViewById(R.id.item_id);
    47         TextView nameTextView = (TextView) v.findViewById(R.id.item_name);
    48         TextView sexTextView = (TextView) v.findViewById(R.id.item_sex);
    49 
    50         try {
    51             // 得到JSONObject
    52             JSONObject jsonObject = jsonArray.getJSONObject(position);
    53             // 得到映射的数据
    54             int id = jsonObject.getInt("id");
    55             String name = jsonObject.getString("name");
    56             String sex = jsonObject.getString("sex");
    57             // 赋值
    58             idTextView.setText(id + "");
    59             nameTextView.setText(name);
    60             sexTextView.setText(sex);
    61         } catch (JSONException e) {
    62             e.printStackTrace();
    63         }
    64         return v;
    65     }
    66 
    67 }
    1 [{"id":1,"name":"xiaosan","sex":"男"},{"id":2,"name":"xiaowu","sex":"女"}]
  • 相关阅读:
    2017.02.20 ,学习C#第三天,基础运算符的运用~
    Linux上如何安装Mysql数据库并使用?
    Linux(CentOS6)上安装python3.6 并创建虚拟环境 -----最牛逼的方法
    python ----Linux上安装scrapy
    Scrapy selenium 设置头部信息headers-------UA
    python 将Excel表格中的一列数据转化成多行数据
    python中的一些小技巧(个人觉得哈 )
    scrapy selenium 设置chromedriver不加载图片
    Scrapy中添加随机User-Agent
    linux centOs 6.4版本 安装python 2.7.3 中的pip
  • 原文地址:https://www.cnblogs.com/afluy/p/3383588.html
Copyright © 2011-2022 走看看