song1
package com.bwie.yuekao_1406a;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.bwie.adapter.MyAdapter;
import com.bwie.helper.MyHelper;
import com.bwie.utils.MyNetUtil;
import com.bwie.vo.MyInfo;
import com.bwie.vo.SuperClass;
import com.google.gson.Gson;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.exception.DbException;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
private EditText edit_name;
private Button button_select;
private ExpandableListView expand_lv;
private String name;
private Set<String> ctitles;
private ArrayList<String> group;
private ArrayList<My_info> child;
private SQLiteDatabase db;
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
// 接受传来的数据
List<MyInfo> li = (List<MyInfo>) msg.obj;
// 将数据添加到数据库中
MyHelper helper = new MyHelper(MainActivity.this, "test", null, 1);
db = helper.getWritableDatabase();
// 创建一级列表的集合,收集所有一级导航(ctitle字段)
ctitles = new HashSet<String>();
for (int i = 0; i < li.size(); i++) {
//得到每一个课程的ctitle,存放到hashset中,同时过滤重复
String ctitle = li.get(i).getCtitle();
ctitles.add(ctitle);
//保存到数据库
ContentValues values = new ContentValues();
values.put("ctime", li.get(i).getCtime());
values.put("ctitle", li.get(i).getCtitle());
values.put("descript", li.get(i).getDescript());
values.put("picurl", li.get(i).getPicurl());
values.put("title", li.get(i).getTitle());
db.insert("data", null, values);
}
//把hashset转成arraylist
group = new ArrayList(ctitles);
// group.addAll(ctitles);
// 为二级列表设置数据
expand_lv.setAdapter(new BaseExpandableListAdapter() {
@Override
public boolean isChildSelectable(int groupPosition,
int childPosition) {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// 填充父布局
View view = View.inflate(MainActivity.this,
android.R.layout.simple_expandable_list_item_1,
null);
// 找到布局文件下的控件
TextView text = (TextView) view
.findViewById(android.R.id.text1);
// 为控件富孩子
text.setText(group.get(groupPosition));
return view;
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return group.size();
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return group.get(groupPosition);
}
@Override
public int getChildrenCount(int groupPosition) {
// 获取点击的一级列表的名称
String title_group = group.get(groupPosition);
System.out.println(title_group+"====================");
// 通过向数据库查询出对应的二级列表的信息
Cursor c = db.rawQuery(
"select * from data where ctitle = ?",
new String[] { title_group });
// //创建一个集合,收集
// ArrayList<My_info> qq = new ArrayList<My_info>();
//
// // 遍历
// while (c.moveToNext()) {
//
// 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"));
// String ctitle = c.getString(c.getColumnIndex("ctitle"));
//
// child.add(new My_info(title, picurl, descript, ctime, ctitle));
// }
return c.getCount();
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// 获取点击的一级列表的名称,通过名称查询出相关的课程
String title_group = group.get(groupPosition);
// 通过向数据库查询出对应的二级列表的信息
Cursor c = db.rawQuery(
"select * from data where ctitle = ?",
new String[] { title_group });
// //创建一个集合,收集
ArrayList<My_info> qq = new ArrayList<My_info>();
// // 遍历
while (c.moveToNext()) {
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"));
String ctitle = c.getString(c.getColumnIndex("ctitle"));
qq.add(new My_info(title, picurl, descript, ctime, ctitle));
}
//找到该布局文件
View view = View.inflate(MainActivity.this, R.layout.item_item, null);
ImageView img=(ImageView) view.findViewById(R.id.img);
TextView tv_title=(TextView) view.findViewById(R.id.tv_title);
TextView tv_desc=(TextView) view.findViewById(R.id.tv_desc);
TextView tv_time=(TextView) view.findViewById(R.id.tv_time);
My_info my_info = qq.get(childPosition);
//为找到的控件赋值
tv_title.setText(my_info.getTitle());
tv_desc.setText(my_info.getDescript());
tv_time.setText(my_info.getCtitle());
BitmapUtils bit=new BitmapUtils(MainActivity.this);
bit.display(img,my_info.getPicurl());
//着色
String str = my_info.getTitle();//得到数据title
//得到搜索关键字的开始索引
if(name != null){
int indexOf = str.indexOf(name);
if(indexOf != -1){
int end = indexOf+name.length();
SpannableStringBuilder builder = new SpannableStringBuilder(str);
ForegroundColorSpan redSpan = new ForegroundColorSpan(Color.RED);
builder.setSpan(redSpan, indexOf, end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
tv_title.setText(builder);
}
}
return view;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return child.get(childPosition);
}
});
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化数据
init();
showData("web前端");
// 为按钮设置点击监听事件
setonClick();
}
private void setonClick() {
// 点击按钮获取输入的信息
button_select.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
name = edit_name.getText().toString();
showData(name);
}
});
}
private void init() {
// 寻找控件
edit_name = (EditText) findViewById(R.id.edit_name);
button_select = (Button) findViewById(R.id.button_select);
expand_lv = (ExpandableListView) findViewById(R.id.expand_lv);
}
private void showData(final String str) {
// 请求网络数据
new Thread() {
public void run() {
String data = MyNetUtil
.getData("http://www.hengboit.com/json/json_search.php?wd="
+ str);
// 解析json文件
Gson g = new Gson();
SuperClass sc = g.fromJson(data, SuperClass.class);
List<MyInfo> list = sc.getInfo();
handler.sendMessage(handler.obtainMessage(1, list));
};
}.start();
}
}