ExpandableListView_二级列表(数据是用TypeToken解出来的【外层是集合】)

package com.example.expandablelistview;

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 org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.expandablelistview.bean.Nextcontent;
import com.example.expandablelistview.bean.Root;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.database.DataSetObserver;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;

public class MainActivity extends Activity {
    private ExpandableListView elv;
    private List<Root> list;
    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {

            List<Root> l = (List<Root>) msg.obj;

            elv.setAdapter(new Myadapter(MainActivity.this, l));
        };
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 寻找控件
        elv = (ExpandableListView) findViewById(R.id.elv);

        new Thread() {
            @SuppressWarnings("unchecked")
            public void run() {
                String path = "http://169.254.88.166:8080/json/an.json";

                HttpClient hc = new DefaultHttpClient();
                HttpGet httpgget = new HttpGet(path);
                try {
                    HttpResponse httpResponse = hc.execute(httpgget);
                    int code = httpResponse.getStatusLine().getStatusCode();
                    if (code == 200) {
                        // System.out.println(code);
                        HttpEntity httpEntity = httpResponse.getEntity();
                        String json = EntityUtils.toString(httpEntity, "UTF-8");

                        Type type = new TypeToken<List<Root>>() {
                        }.getType();

                        Gson gson = new Gson();
                        List<Root> list = gson.fromJson(json, 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();
                }

            };
        }.start();
    }

}

----------------------------------------------------------------------------------------

微笑准备适配器


package com.example.expandablelistview;

import java.util.List;

import com.example.expandablelistview.bean.Root;

import android.database.DataSetObserver;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListAdapter;
import android.widget.TextView;

public class Myadapter implements ExpandableListAdapter {

    private MainActivity mainActivity;
    private List<Root> l;

    public Myadapter(MainActivity mainActivity, List<Root> l) {
        this.mainActivity = mainActivity;
        // TODO Auto-generated constructor stub
        this.l = l;
    }

    @Override
    public int getGroupCount() {
        // TODO Auto-generated method stub
        return l.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
        
        if(l.get(groupPosition).getNextcontent()!=null){
            return l.get(groupPosition).getNextcontent().size();
        }
        return  0;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {

        View inflate = View.inflate(mainActivity,
                android.R.layout.simple_expandable_list_item_2, null);
        TextView text1 = (TextView) inflate.findViewById(android.R.id.text1);
        text1.setText(l.get(groupPosition).getClassname());
        return inflate;

    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View inflate = View.inflate(mainActivity,
                android.R.layout.simple_expandable_list_item_2, null);

        TextView text2 = (TextView) inflate.findViewById(android.R.id.text2);

        String name = l.get(groupPosition).getNextcontent().get(childPosition)
                .getSclassname();
        text2.setText(name);
        return inflate;
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {
        // TODO Auto-generated method stub

    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {
        // TODO Auto-generated method stub

    }

    @Override
    public Object getGroup(int groupPosition) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean areAllItemsEnabled() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isEmpty() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onGroupExpanded(int groupPosition) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onGroupCollapsed(int groupPosition) {
        // TODO Auto-generated method stub

    }

    @Override
    public long getCombinedChildId(long groupId, long childId) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public long getCombinedGroupId(long groupId) {
        // TODO Auto-generated method stub
        return 0;
    }

}
-------------------------------------------

封装类

package com.example.expandablelistview.bean;

import java.util.List;

 
    public class Root {
        private String cid;

        private String classname;

        private String next;

        private List<Nextcontent> nextcontent ;

        public void setCid(String cid){
        this.cid = cid;
        }
        public String getCid(){
        return this.cid;
        }
        public void setClassname(String classname){
        this.classname = classname;
        }
        public String getClassname(){
        return this.classname;
        }
        public void setNext(String next){
        this.next = next;
        }
        public String getNext(){
        return this.next;
        }
        public void setNextcontent(List<Nextcontent> nextcontent){
        this.nextcontent = nextcontent;
        }
        public List<Nextcontent> getNextcontent(){
        return this.nextcontent;
        }
}

------------------------------------------------------------------------

package com.example.expandablelistview.bean;

public class Nextcontent {
    private String scid;

    private String sclassname;

    private String next;

    public void setScid(String scid) {
        this.scid = scid;
    }

    public String getScid() {
        return this.scid;
    }

    public void setSclassname(String sclassname) {
        this.sclassname = sclassname;
    }

    public String getSclassname() {
        return this.sclassname;
    }

    public void setNext(String next) {
        this.next = next;
    }

    public String getNext() {
        return this.next;
    }

}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值