package com.exaple.zhonghe2;
import java.sql.SQLData;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import com.exaple.Exapa.ExpandBaptwe;
import com.exaple.Exapa.MyHelp;
import com.exaple.netUtil.NetUti;
import com.exaple.vo.Myinfo;
import com.exaple.vo.ShowA;
import com.exaple.vo.Super;
import com.google.gson.Gson;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
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 String title;
public List<Myinfo> li;
Handler ha = new Handler() {
private Set<String> set_ctitle;
private ArrayList<ShowA> al;
public void handleMessage(android.os.Message msg) {
String ma = (String) msg.obj;
Gson gs = new Gson();
Super fromJson = gs.fromJson(ma, Super.class);
li = fromJson.getInfo();
set_ctitle = new HashSet<String>();
for (Myinfo m : li) {
set_ctitle.add(m.getCtitle());
}
List<String> lis = new ArrayList<String>();
lis.addAll(set_ctitle);
//添加数据库
for (int i = 0; i < lis.size(); i++) {
String set = lis.get(i);
sql.execSQL("insert into user values(null,?)",
new String[] { set });
}
final List<ArrayList<ShowA>> child_list = new ArrayList<ArrayList<ShowA>>();
for (int i = 0; i < lis.size(); i++) {
al = new ArrayList<ShowA>();
for (Myinfo n : li) {
if (n.getCtitle().equals(lis.get(i))) {
ShowA sn = new ShowA();
sn.setCtime(n.getCtime());
sn.setCtitle(n.getCtitle());
sn.setDescript(n.getDescript());
sn.setPicurl(n.getPicurl());
sn.setTitle(n.getTitle());
al.add(sn);
}
}
child_list.add(al);
}
for (int i = 0; i < al.size(); i++) {
String time = al.get(i).getCtime();
String ctitle = al.get(i).getCtitle();
String descrip = al.get(i).getDescript();
String pic = al.get(i).getPicurl();
String title = al.get(i).getTitle();
sql.execSQL("insert into use values(null,?,?,?,?,?)",
new String[] { time, ctitle, descrip, pic, title });
}
ExpandBaptwe ba = new ExpandBaptwe(lis, MainActivity.this,
child_list, text);
exp.setAdapter(ba);
};
};
private EditText edtext;
private Button button;
private ExpandableListView exp;
private String text;
private SQLiteDatabase sql;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtext = (EditText) findViewById(R.id.edtext);
button = (Button) findViewById(R.id.button);
exp = (ExpandableListView) findViewById(R.id.exe);
MyHelp my = new MyHelp(MainActivity.this, "users", null, 1);
sql = my.getWritableDatabase();
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
text = edtext.getText().toString();
new Thread() {
public void run() {
String str = NetUti
.GetJson("http://www.hengboit.com/json/json_search.php?wd="
+ text);
ha.sendMessage(ha.obtainMessage(1, str));
};
}.start();
}
});
exp.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Intent ints = new Intent(MainActivity.this, NewActivity.class);
ints.putExtra("name", li.get(childPosition).getCtime());
startActivity(ints);
return true;
}
});
}
}
//适配器
package com.exaple.Exapa;
import java.util.ArrayList;
import java.util.List;
import com.exaple.Exapa.Apater.ViewHodler;
import com.exaple.vo.Myinfo;
import com.exaple.vo.ShowA;
import com.exaple.zhonghe2.MainActivity;
import com.exaple.zhonghe2.NewActivity;
import com.exaple.zhonghe2.R;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.db.sqlite.CursorUtils.FindCacheSequence;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class ExpandBaptwe extends BaseExpandableListAdapter {
List<String> lis;
Context co;
List<ArrayList<ShowA>> child_list;
String text;
private SQLiteDatabase sql;
public ExpandBaptwe(List<String> lis, Context co,
List<ArrayList<ShowA>> child_list, String text) {
super();
this.lis = lis;
this.text = text;
this.co = co;
this.child_list = child_list;
MyHelp my = new MyHelp(co, "users", null, 1);
sql = my.getReadableDatabase();
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return lis.size();
}
@Override
public int getChildrenCount(int groupPosition) {
/*String title_group = lis.get(groupPosition);
System.out.println(title_group + "====================");
// 通过向数据库查询出对应的二级列表的信息
Cursor c = sql.rawQuery("select * from use where ctitle = ?",
new String[] { title_group });
System.out.println(lis.get(groupPosition).length());*/
return child_list.get(groupPosition).size();
}
@Override
public Object getGroup(int groupPosition) {
return groupPosition;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@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(co,
android.R.layout.simple_expandable_list_item_1, null);
TextView text = (TextView) view.findViewById(android.R.id.text1);
text.setText(lis.get(groupPosition));
return view;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View view_layout = View.inflate(co, R.layout.apater, null);
TextView t1 = (TextView) view_layout.findViewById(R.id.des);
TextView t2 = (TextView) view_layout.findViewById(R.id.time);
ImageView t3 = (ImageView) view_layout.findViewById(R.id.image);
/*
* Cursor c = sql.rawQuery("select * from use where ctitle =?", new
* String[] { lis.get(groupPosition)}); List<Myinfo> list = new
* ArrayList<Myinfo>();
*
* while (c.moveToNext()) { String s =
* c.getString(c.getColumnIndex("title")); list.add(new Myinfo(s));
*
* }
*/
//t1.setText(list.get(childPosition).getTitle());
// List<Myinfo> liss=new ArrayList<Myinfo>();
ShowA a = child_list.get(groupPosition).get(childPosition);
t1.setText(a.getDescript());
t2.setText(a.getTitle());
BitmapUtils bit = new BitmapUtils(co);
bit.display(t3, a.getPicurl());
// 设置字体颜色
if (text != null) {
String str = t2.getText().toString();
int id1 = str.indexOf(text);
if (id1 != -1) {
int end = id1 + text.length();
SpannableStringBuilder builder = new SpannableStringBuilder(str);
ForegroundColorSpan redSpan = new ForegroundColorSpan(Color.RED);
builder.setSpan(redSpan, id1, end,
Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
t2.setText(builder);
}
}
return view_layout;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
}
////数据库
package com.exaple.Exapa;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class MyHelp extends SQLiteOpenHelper{
public MyHelp(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table user(id integer,ctitle)");
db.execSQL("create table use(id integer,ctime,ctitle,descript,picurl,title)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}