TypeToken解析二级列表

package com.bwie.main;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ExpandableListView;

import com.bwie.bean.MyAdapter;
import com.bwie.bean.Nextcontent;
import com.bwie.bean.Root;
import com.example.android_15_exlistview.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.db.sqlite.WhereBuilder;
import com.lidroid.xutils.exception.DbException;

public class MainActivity extends Activity {
private DbUtils dbUtils;
private ExpandableListView exLv;
@SuppressLint(“HandlerLeak”)
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
@SuppressWarnings(“unchecked”)
List list = (List(Root)) msg.obj;
// 插入数据库
Insert(list);
// 从数据中查询数据,构建二级列表的条件
List(List(Nextcontent)) childList = select(list);
// 传给适配器
MyAdapter adapter = new MyAdapter(MainActivity.this, list,
childList);
exLv.setAdapter(adapter);
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    dbUtils = DbUtils.create(this, "Context.db");
    exLv = (ExpandableListView) findViewById(R.id.ExLv);
    new Thread() {
        @Override
        public void run() {
            NetWork();
        }
    }.start();
}

// 请求网络
public void NetWork() {
    // 请求数据地址
    String path = "http://169.254.224.177:8080/an.json";
    HttpClient httpClient = new DefaultHttpClient();
    // 请求方式
    HttpGet httpGet = new HttpGet(path);
    // 传递给服务器
    try {
        HttpResponse response = httpClient.execute(httpGet);
        // 从服务器中取回数据
        HttpEntity httpEntity = response.getEntity();
        // 转换为String类型
        String str = EntityUtils.toString(httpEntity, "utf-8");
        // TypeToken解析 方括号的Json串
        Type type = new TypeToken<List<Root>>() {
        }.getType();
        // 解析
        Gson gson = new Gson();
        List<Root> list = gson.fromJson(str, type);
        Message msg = Message.obtain();
        msg.obj = list;
        handler.sendMessage(msg);
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

// 添加方法
public void Insert(List<Root> list) {
    for (Root root : list) {
        String className = root.getClassname();
        // 一级列表
        List<Nextcontent> nextcontent = root.getNextcontent();
        if (nextcontent != null) {
            for (Nextcontent next : nextcontent) {
                next.setClassname(className);
            }
        }
        // 多添加一个字段 为了判断出 二级列表的分类 添加二级列表的第一端
        try {
            dbUtils.saveAll(nextcontent);
        } catch (DbException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

// 查询方法
public List<List<Nextcontent>> select(List<Root> list) {
    List<List<Nextcontent>> childlist = new ArrayList<List<Nextcontent>>();
    for (Root root : list) {
        try {
            // 判断条件是否一样
            List<Nextcontent> child = dbUtils.findAll(Nextcontent.class,
                    WhereBuilder.b("classname", "=", root.getClassname()));
            childlist.add(child);
        } catch (DbException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        for (List<Nextcontent> list2 : childlist) {
            if (list2 != null) {
                for (Nextcontent nextcontent : list2) {
                    Log.i("zzz", "有数据:" + nextcontent.getClassname());
                }
            }
        }
    }
    return childlist;

}

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值