public class MainActivity extends Activity {
private ExpandableListView elv;
String path="http://169.254.59.7:8080/json/an.json";
private List<Bean> list;
private List<List<Content>> listchild;
Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
DbUtils db=DbUtils.create(MainActivity.this, "1409A");
list=new ArrayList<Bean>();
try {
//查询到一级列表
list=db.findAll(Bean.class);
//查询二级列表
listchild=new ArrayList<List<Content>>();
for(int i=0;i<list.size();i++){
List<Content> list1= db.findAll(Selector.from(Content.class).where("classname",
"=", list.get(i).getClassname()));
listchild.add(list1);
}
MyAdapter adapter=new MyAdapter(MainActivity.this, list, listchild);
elv.setAdapter(adapter);
} catch (DbException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
elv=(ExpandableListView) findViewById(R.id.elv);
new Thread(){
public void run() {
httpGet();
};
}.start();
}
//解析
public void httpGet(){
HttpClient httpClient=new DefaultHttpClient();
HttpGet httpGet=new HttpGet(path);
try {
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity entity = httpResponse.getEntity();
String string = EntityUtils.toString(entity,"utf-8");
Gson gson=new Gson();
Bean[] bean = gson.fromJson(string, Bean[].class);
//创建数据库
DbUtils db=DbUtils.create(MainActivity.this, "1409A");
for(int i=0;i<bean.length;i++){
//得到一级列表的数据
Bean b=bean[i];
String classname = b.getClassname();
//将一级列表的数据存入数据库
db.save(b);
//得到二级列表的集合
List<Content> nextcontent = bean[i].getNextcontent();
if(nextcontent!=null){
for(int j=0;j<nextcontent.size();j++){
//得到二级列表的数据
Content content = nextcontent.get(j);
content.setClassname(classname);
//存入数据库(存对象)
db.save(content);
}
}
}
handler.sendEmptyMessage(1);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DbException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//二级标题的点击事件 (注意:需要将适配器中的 isChildSelectable 这个方法的返回值设置成true)
elv.setOnChildClickListener
//适配器
public class MyAdapter extends BaseExpandableListAdapter{
Context context;
List<Bean> list;
List<List<Content>> listchild;
public MyAdapter(Context context, List<Bean> list, List<List<Content>> listchild) {
super();
this.context = context;
this.list = list;
this.listchild = listchild;
}
@Override
public int getGroupCount() {
return list.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return listchild.get(groupPosition).size();
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View v1=View.inflate(context, android.R.layout.simple_expandable_list_item_2, null);
TextView text1=(TextView) v1.findViewById(android.R.id.text1);
text1.setText(list.get(groupPosition).getClassname());
return v1;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View v2=View.inflate(context, android.R.layout.simple_expandable_list_item_2, null);
TextView text2=(TextView) v2.findViewById(android.R.id.text2);
String name = listchild.get(groupPosition).get(childPosition).getSclassname();
text2.setText(name);
return v2;
}
@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 true;
}
}
private ExpandableListView elv;
String path="http://169.254.59.7:8080/json/an.json";
private List<Bean> list;
private List<List<Content>> listchild;
Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
DbUtils db=DbUtils.create(MainActivity.this, "1409A");
list=new ArrayList<Bean>();
try {
//查询到一级列表
list=db.findAll(Bean.class);
//查询二级列表
listchild=new ArrayList<List<Content>>();
for(int i=0;i<list.size();i++){
List<Content> list1= db.findAll(Selector.from(Content.class).where("classname",
"=", list.get(i).getClassname()));
listchild.add(list1);
}
MyAdapter adapter=new MyAdapter(MainActivity.this, list, listchild);
elv.setAdapter(adapter);
} catch (DbException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
elv=(ExpandableListView) findViewById(R.id.elv);
new Thread(){
public void run() {
httpGet();
};
}.start();
}
//解析
public void httpGet(){
HttpClient httpClient=new DefaultHttpClient();
HttpGet httpGet=new HttpGet(path);
try {
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity entity = httpResponse.getEntity();
String string = EntityUtils.toString(entity,"utf-8");
Gson gson=new Gson();
Bean[] bean = gson.fromJson(string, Bean[].class);
//创建数据库
DbUtils db=DbUtils.create(MainActivity.this, "1409A");
for(int i=0;i<bean.length;i++){
//得到一级列表的数据
Bean b=bean[i];
String classname = b.getClassname();
//将一级列表的数据存入数据库
db.save(b);
//得到二级列表的集合
List<Content> nextcontent = bean[i].getNextcontent();
if(nextcontent!=null){
for(int j=0;j<nextcontent.size();j++){
//得到二级列表的数据
Content content = nextcontent.get(j);
content.setClassname(classname);
//存入数据库(存对象)
db.save(content);
}
}
}
handler.sendEmptyMessage(1);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DbException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//二级标题的点击事件 (注意:需要将适配器中的 isChildSelectable 这个方法的返回值设置成true)
elv.setOnChildClickListener
//适配器
public class MyAdapter extends BaseExpandableListAdapter{
Context context;
List<Bean> list;
List<List<Content>> listchild;
public MyAdapter(Context context, List<Bean> list, List<List<Content>> listchild) {
super();
this.context = context;
this.list = list;
this.listchild = listchild;
}
@Override
public int getGroupCount() {
return list.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return listchild.get(groupPosition).size();
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View v1=View.inflate(context, android.R.layout.simple_expandable_list_item_2, null);
TextView text1=(TextView) v1.findViewById(android.R.id.text1);
text1.setText(list.get(groupPosition).getClassname());
return v1;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View v2=View.inflate(context, android.R.layout.simple_expandable_list_item_2, null);
TextView text2=(TextView) v2.findViewById(android.R.id.text2);
String name = listchild.get(groupPosition).get(childPosition).getSclassname();
text2.setText(name);
return v2;
}
@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 true;
}
}