package com.lixu.TestJson; import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast; import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List; public class MainActivity extends Activity {
private ListView mListView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 第一步实例化控件
mListView = (ListView) findViewById(R.id.list); // 第二步数据源
List<Book> bookList = getBooks(); MyBooksAdapter adapter = new MyBooksAdapter(this);
// 使用适配器中的方法将数据设置进去
adapter.setBookList(bookList);
mListView.setAdapter(adapter); } // 第三步适配器
public class MyBooksAdapter extends BaseAdapter {
private List<Book> bookList = new ArrayList<Book>();
private LayoutInflater mLayoutInflater; public MyBooksAdapter(Context context) {
mLayoutInflater = LayoutInflater.from(context);
} // 添加这个方法,便于外部传递数据源进来
public void setBookList(List<Book> bookList) {
this.bookList = bookList;
notifyDataSetChanged();
} @Override
public int getCount() {
return bookList.size();
} @Override
public Object getItem(int i) {
return bookList.get(i);
} @Override
public long getItemId(int i) {
return i;
} @Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view == null) {
view = mLayoutInflater.inflate(android.R.layout.simple_list_item_1, null);
}
TextView textView = (TextView) view; Book book = (Book) getItem(i);
textView.setText(book.getId() + " " + book.getName()); Toast.makeText(getApplicationContext(), book.getId() + " " + book.getName(), 1).show(); return view;
}
} public List<Book> getBooks() {
// 从Asset文件中找到文件,获取json字符串
AssetManager assetManager = this.getAssets();
InputStream inputStream = null;
List<Book> bookList = new ArrayList<Book>();
try {
inputStream = assetManager.open("myjson.json");
String jsonStr = readInPutStream(inputStream);
// 解析jason
JSONObject jsonObjectRoot = null;
jsonObjectRoot = new JSONObject(jsonStr);
JSONArray jsonArray = jsonObjectRoot.getJSONArray("books");
int length = jsonArray.length();
for (int i = 0; i < length; i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
JSONObject jsonBook = jsonObject.getJSONObject("book");
String bookName = jsonBook.getString("name");
int bookId = jsonBook.getInt("id"); Book book = new Book();
book.setId(bookId);
book.setName(bookName);
bookList.add(book);
}
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bookList; } // 从字节流中读取数据转换为String字符串
public String readInPutStream(InputStream inputStream) {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
StringBuilder stringBuilder = new StringBuilder();
try {
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} return stringBuilder.toString();
}
}
package com.lixu.TestJson;
import java.io.Serializable; /**
* Created by Administrator on 2015/12/31.
*/
public class Book implements Serializable{
private int id;
private String name; public Book() { } public Book(int id, String name) {
this.id = id;
this.name = name;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} }
package com.lixu.TestJson; import java.util.List; /**
* Created by Administrator on 2015/12/31.
*/
public class BookInfo { private List<BookResult> books; public List<BookResult> getBooks() {
return books;
} public void setBooks(List<BookResult> books) {
this.books = books;
} }
package com.lixu.TestJson; /**
* Created by Administrator on 2015/12/31.
*/
public class BookResult {
private Book book; public Book getBook() {
return book;
} public void setBook(Book book) {
this.book = book;
} }