1、首先先看json文件,这个json并不算复杂分三层
2、分析完json文件我们开始创建Bean,json其实可以自动生成,由于我用的版本较低的eclipse所以手动敲的,注释不全见谅
public class PersonsBean {
// 属性私有化,参数名一定要与json文件保持一致
private String info;
private String status;
private DataBean data;
// set、get方法
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public DataBean getData() {
return data;
}
public void setData(DataBean data) {
this.data = data;
}
// 静态内部类
public static class DataBean {
private String count;
private String type;
private List<PhonesBean> phones;
public String getCount() {
return count;
}
public void setCount(String count) {
this.count = count;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public List<PhonesBean> getPhones() {
return phones;
}
public void setPhones(List<PhonesBean> phones) {
this.phones = phones;
}
// 静态内部类
public static class PhonesBean {
private String brand;
private String name;
private String price;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
}
}
3、我用的Fragment写的所以下面是Fragment,和activity不同的就是获取上下文
布局很简单相信一看就知道只有一个ListView控件
public class AFragment extends Fragment {
private List<PersonsBean.DataBean.PhonesBean> list;
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = View.inflate(getActivity(), R.layout.fragment1, null);
ListView listView = (ListView) view.findViewById(R.id.listView_f1);
initDatas();
//设置适配器
AFAdapter afAdapter = new AFAdapter(list, getActivity());
listView.setAdapter(afAdapter);
return view;
}
private void initDatas() {
// 从sd卡中读取json文件并Gson解析
File file = new File(Environment.getExternalStorageDirectory(),
"persons.json");
// 读取文件
try {
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(new FileInputStream(file)));
// 创建StringBuffer
StringBuffer stringBuffer = new StringBuffer();
String temp = "";
try {
// 一行一行的读
while ((temp = bufferedReader.readLine()) != null) {
stringBuffer.append(temp);
}
String presonsString = stringBuffer.toString();
// 解析,创建Gson,需要导入gson的jar包
Gson gson = new Gson();
PersonsBean fromJson = gson.fromJson(presonsString,
PersonsBean.class);
// 得到list数据
list = fromJson.getData().getPhones();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
适配器中的代码,都是一样的这里我们做了优化
public class AFAdapter extends BaseAdapter {
private List<PersonsBean.DataBean.PhonesBean> list;
private Context mContext;
public AFAdapter(List<PhonesBean> list, Context mContext) {
super();
this.list = list;
this.mContext = mContext;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = View.inflate(mContext, R.layout.a_l_item, null);
holder = new ViewHolder();
holder.brand = (TextView) convertView.findViewById(R.id.brand_al);
holder.name = (TextView) convertView.findViewById(R.id.name_al);
holder.price = (TextView) convertView.findViewById(R.id.price_al);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// 设置内容
PhonesBean phonesBean = list.get(position);
holder.brand.setText(phonesBean.getBrand());
holder.name.setText(phonesBean.getName());
holder.price.setText(phonesBean.getPrice());
return convertView;
}
class ViewHolder {
TextView brand, name, price;
}
}