song2
package com.bwie.yuekao_test;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import com.bwie.adapter.MyAdapter;
import com.bwie.help.MyHelper;
import com.bwie.utils.MyUtil;
import com.bwie.vo.MyInfo;
import com.bwie.vo.SuperClass;
import com.google.gson.Gson;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
public class MainActivity extends Activity {
private Button button;
private EditText edit_name;
private ExpandableListView expand;
private String name;
Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
//获取传来的数据
List<MyInfo> list=(List<MyInfo>) msg.obj;
System.out.println(list);
//创建数据库
MyHelper helper = new MyHelper(MainActivity.this, "yuekao", null, 1);
SQLiteDatabase db = helper.getWritableDatabase();
//创建集合来存放一级列表的名称
final Set<String> group_list=new HashSet<String>();
//将数据添加到数据库中
for(int i=0;i<list.size();i++)
{
//获取集合中的数据
MyInfo info=list.get(i);
String ctime = info.getCtime();
String ctitle = info.getCtitle();
String desc = info.getDescript();
String picurl = info.getPicurl();
String title = info.getTitle();
ContentValues values=new ContentValues();
values.put("ctitle", ctitle);
values.put("ctime", ctime);
values.put("descript", desc);
values.put("picurl", picurl);
values.put("title", title);
db.insert("childdata", null, values);
group_list.add(ctitle);
}
//将set集合转化为list集合
final ArrayList<String> group = new ArrayList<String>(group_list);
//为二级导航设置适配器
expand.setAdapter(new MyAdapter(MainActivity.this,group,name));
//设置条目点击监听
expand.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
//点击跳转
Intent intent=new Intent(MainActivity.this,NewActivity.class);
intent.putExtra("list", group);
intent.putExtra("gp", groupPosition);
intent.putExtra("cp", childPosition);
startActivity(intent);
return false;
}
});
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 寻找控件
findView();
// 请求网络数据
getData("web前端");
//为按钮设置点击监听事件
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//获取输入的数据
name = edit_name.getText().toString();
getData(name);
}
});
}
private void getData(final String name) {
new Thread() {
public void run() {
String data = MyUtil
.getDataByNet("http://www.hengboit.com/json/json_search.php?wd="
+ name);
//解析请求的数据
Gson g = new Gson();
SuperClass sc = g.fromJson(data, SuperClass.class);
List<MyInfo> info = sc.getInfo();
//向主线程发送消息
handler.sendMessage(handler.obtainMessage(1, info));
};
}.start();
}
private void findView() {
button = (Button) findViewById(R.id.button);
edit_name = (EditText) findViewById(R.id.edit_name);
expand = (ExpandableListView) findViewById(R.id.expand);
}
}