1、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);
}
}
2、public class MyAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> group_list;
private SQLiteDatabase db;
private String name;
public MyAdapter(Context context, List<String> group_list, String name) {
this.context = context;
this.group_list = group_list;
this.name = name;
// 获取数据库
MyHelper helper = new MyHelper(context, "yuekao", null, 1);
db = helper.getWritableDatabase();
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// 获取点击的一级条目
String item = group_list.get(groupPosition);
// 查询数据库
Cursor c = db.rawQuery("select * from childdata where ctitle = ?",
new String[] { item });
List<MyInfo> child_list = new ArrayList<MyInfo>();
// 遍历
while (c.moveToNext()) {
String ctitle = c.getString(c.getColumnIndex("ctitle"));
String ctime = c.getString(c.getColumnIndex("ctime"));
String descript = c.getString(c.getColumnIndex("descript"));
String picurl = c.getString(c.getColumnIndex("picurl"));
String title = c.getString(c.getColumnIndex("title"));
child_list.add(new MyInfo(ctitle, ctitle, picurl, ctime, descript));
}
// 填充子布局
View view = View.inflate(context, R.layout.item_child, null);
// 找到该布局文件下的 控件
TextView tv_desc = (TextView) view.findViewById(R.id.tv_desc);
TextView tv_time = (TextView) view.findViewById(R.id.tv_time);
TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
ImageView img = (ImageView) view.findViewById(R.id.img);
// 为控件赋值
tv_desc.setText(child_list.get(childPosition).getDescript());
tv_time.setText(child_list.get(childPosition).getCtime());
tv_title.setText(child_list.get(childPosition).getTitle());
BitmapUtils bit = new BitmapUtils(context);
bit.display(img, child_list.get(childPosition).getPicurl());
// 为description设置着色
// 获取文字
String str = tv_desc.getText().toString();
if (name != null) {
int startIndex = str.indexOf(name);
int endIndex = startIndex + name.length();
if (startIndex != -1) {
SpannableStringBuilder span = new SpannableStringBuilder(str);
// 设置要着的颜色
ForegroundColorSpan color = new ForegroundColorSpan(Color.RED);
// 设置的哪个部分
span.setSpan(color, startIndex, endIndex,
Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
tv_desc.setText(span);
}
}
return view;
}
@Override
public int getChildrenCount(int groupPosition) {
// 获取点击的一级条目
String item = group_list.get(groupPosition);
// 查询数据库
Cursor c = db.rawQuery("select * from childdata where ctitle = ?",
new String[] { item });
return c.getCount();
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return null;
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return group_list.size();
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// 填充父布局
View view = View.inflate(context,
android.R.layout.simple_expandable_list_item_1, null);
// 找到该布局文件下的控件
TextView text1 = (TextView) view.findViewById(android.R.id.text1);
// 为控件赋值
text1.setText(group_list.get(groupPosition));
return view;
}
@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 groupPosition == childPosition;
}
}
原文:http://www.cnblogs.com/8023-itxinde/p/5158680.html