二级列表展示网络数据

<span style="font-size:18px;">public class MainActivity extends Activity {

	private ExpandableListView ep;
	private HttpResponse httpResponse;
	private HttpEntity entity;
	private List<Bean> list;
	String path = "http://10.1.4.27:8080/an.json";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ep = (ExpandableListView) findViewById(R.id.expandableListView1);
		new Thread() {

			@Override
			public void run() {
				sendGet();
			}

		}.start();
	}

	public void sendGet() {
		HttpClient httpClient = new DefaultHttpClient();
		HttpGet get = new HttpGet(path);
		try {
			httpResponse = httpClient.execute(get);
			entity = httpResponse.getEntity();
			String str = EntityUtils.toString(entity,"utf-8");
			System.out.println(str);
			Gson gson = new Gson();
			Type type = new TypeToken<List<Bean>>() {
			}.getType();
			list = gson.fromJson(str, type);
			System.out.println(list.size());
			runOnUiThread(new Runnable() {

				@Override
				public void run() {
					ep.setAdapter(new Myad());
				}
			});
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	class Myad extends BaseExpandableListAdapter {

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

		@Override
		public int getChildrenCount(int groupPosition) {
			if (list.get(groupPosition).nextcontent == null) {
				return 0;
			} else {
				return list.get(groupPosition).nextcontent.size();
			}

		}

		@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 View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			View v = View.inflate(MainActivity.this, R.layout.yiji, null);
			TextView tv1 = (TextView) v.findViewById(R.id.textView1);
			tv1.setText(list.get(groupPosition).classname);
			return v;
		}

		@Override
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			View v = View.inflate(MainActivity.this, R.layout.erji, null);
			TextView tv2 = (TextView) v.findViewById(R.id.textView2);
			tv2.setText(list.get(groupPosition).nextcontent.get(childPosition).sclassname);
			return v;
		}

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

	}

}</span>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值