1 package com.lixu.TestJson;
2
3 import android.app.Activity;
4 import android.content.Context;
5 import android.content.res.AssetManager;
6 import android.os.Bundle;
7 import android.view.LayoutInflater;
8 import android.view.View;
9 import android.view.ViewGroup;
10 import android.widget.BaseAdapter;
11 import android.widget.ListView;
12 import android.widget.TextView;
13 import android.widget.Toast;
14
15 import org.json.JSONArray;
16 import org.json.JSONException;
17 import org.json.JSONObject;
18 import java.io.BufferedReader;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.InputStreamReader;
22 import java.util.ArrayList;
23 import java.util.List;
24
25 public class MainActivity extends Activity {
26 private ListView mListView;
27
28 @Override
29 protected void onCreate(Bundle savedInstanceState) {
30 super.onCreate(savedInstanceState);
31 setContentView(R.layout.activity_main);
32 // 第一步实例化控件
33 mListView = (ListView) findViewById(R.id.list);
34
35 // 第二步数据源
36 List<Book> bookList = getBooks();
37
38 MyBooksAdapter adapter = new MyBooksAdapter(this);
39 // 使用适配器中的方法将数据设置进去
40 adapter.setBookList(bookList);
41 mListView.setAdapter(adapter);
42
43 }
44
45 // 第三步适配器
46 public class MyBooksAdapter extends BaseAdapter {
47 private List<Book> bookList = new ArrayList<Book>();
48 private LayoutInflater mLayoutInflater;
49
50 public MyBooksAdapter(Context context) {
51 mLayoutInflater = LayoutInflater.from(context);
52 }
53
54 // 添加这个方法,便于外部传递数据源进来
55 public void setBookList(List<Book> bookList) {
56 this.bookList = bookList;
57 notifyDataSetChanged();
58 }
59
60 @Override
61 public int getCount() {
62 return bookList.size();
63 }
64
65 @Override
66 public Object getItem(int i) {
67 return bookList.get(i);
68 }
69
70 @Override
71 public long getItemId(int i) {
72 return i;
73 }
74
75 @Override
76 public View getView(int i, View view, ViewGroup viewGroup) {
77 if (view == null) {
78 view = mLayoutInflater.inflate(android.R.layout.simple_list_item_1, null);
79 }
80 TextView textView = (TextView) view;
81
82 Book book = (Book) getItem(i);
83 textView.setText(book.getId() + " " + book.getName());
84
85 Toast.makeText(getApplicationContext(), book.getId() + " " + book.getName(), 1).show();
86
87 return view;
88 }
89 }
90
91 public List<Book> getBooks() {
92 // 从Asset文件中找到文件,获取json字符串
93 AssetManager assetManager = this.getAssets();
94 InputStream inputStream = null;
95 List<Book> bookList = new ArrayList<Book>();
96 try {
97 inputStream = assetManager.open("myjson.json");
98 String jsonStr = readInPutStream(inputStream);
99 // 解析jason
100 JSONObject jsonObjectRoot = null;
101 jsonObjectRoot = new JSONObject(jsonStr);
102 JSONArray jsonArray = jsonObjectRoot.getJSONArray("books");
103 int length = jsonArray.length();
104 for (int i = 0; i < length; i++) {
105 JSONObject jsonObject = jsonArray.getJSONObject(i);
106 JSONObject jsonBook = jsonObject.getJSONObject("book");
107 String bookName = jsonBook.getString("name");
108 int bookId = jsonBook.getInt("id");
109
110 Book book = new Book();
111 book.setId(bookId);
112 book.setName(bookName);
113 bookList.add(book);
114 }
115 } catch (JSONException e) {
116 e.printStackTrace();
117 } catch (IOException e) {
118 e.printStackTrace();
119 }
120 return bookList;
121
122 }
123
124 // 从字节流中读取数据转换为String字符串
125 public String readInPutStream(InputStream inputStream) {
126 BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
127 String line = "";
128 StringBuilder stringBuilder = new StringBuilder();
129 try {
130 while ((line = reader.readLine()) != null) {
131 stringBuilder.append(line);
132 }
133 } catch (IOException e) {
134 e.printStackTrace();
135 } finally {
136 if (reader != null) {
137 try {
138 reader.close();
139 } catch (IOException e) {
140 e.printStackTrace();
141 }
142 }
143 }
144
145 return stringBuilder.toString();
146 }
147 }
1 package com.lixu.TestJson;
2 import java.io.Serializable;
3
4 /**
5 * Created by Administrator on 2015/12/31.
6 */
7 public class Book implements Serializable{
8 private int id;
9 private String name;
10
11 public Book() {
12
13 }
14
15 public Book(int id, String name) {
16 this.id = id;
17 this.name = name;
18 }
19
20 public String getName() {
21 return name;
22 }
23
24 public void setName(String name) {
25 this.name = name;
26 }
27
28 public int getId() {
29 return id;
30 }
31
32 public void setId(int id) {
33 this.id = id;
34 }
35
36
37 }
1 package com.lixu.TestJson;
2
3 import java.util.List;
4
5 /**
6 * Created by Administrator on 2015/12/31.
7 */
8 public class BookInfo {
9
10 private List<BookResult> books;
11
12 public List<BookResult> getBooks() {
13 return books;
14 }
15
16 public void setBooks(List<BookResult> books) {
17 this.books = books;
18 }
19
20
21
22 }
1 package com.lixu.TestJson;
2
3 /**
4 * Created by Administrator on 2015/12/31.
5 */
6 public class BookResult {
7 private Book book;
8
9 public Book getBook() {
10 return book;
11 }
12
13 public void setBook(Book book) {
14 this.book = book;
15 }
16
17 }