关闭

TypeToken解析二级列表

110人阅读 评论(0) 收藏 举报
分类:

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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:12875次
    • 积分:357
    • 等级:
    • 排名:千里之外
    • 原创:24篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档