<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>
二级列表展示网络数据
最新推荐文章于 2020-11-02 14:43:21 发布