您的位置:首页 > 其它

Huan1

2016-01-25 21:21 441 查看
package com.bwie.day20_1406a;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import com.bwie.helper.MyHelper;
import com.bwie.vo.News;
import com.bwie.vo.ShowNews;
import com.bwie.vo.SuperData;
import com.google.gson.Gson;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.exception.DbException;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
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.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

final String URL_JSON = "http://www.hengboit.com/json/json_search.php?wd=";
Context mcontext;
private ImageView ss;
private EditText editText;
ExpandableListView elv;
private List<News> list;
String ss_name;
SQLiteDatabase sdb;

Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
// 获得一级条目数据的集合
final List<String> List_ctitle = (List<String>) msg.obj;
// 创建二维集合
final List<ArrayList<ShowNews>> child_list = new ArrayList<ArrayList<ShowNews>>();

for (int i = 0; i < List_ctitle.size(); i++) {
// 创建二维集合里面的子集合
ArrayList<ShowNews> al = new ArrayList<ShowNews>();

sdb.execSQL("insert into first values(null,?)",new
String[]{List_ctitle.get(i)});
for (News n : list) {
if (n.getCtitle().equals(List_ctitle.get(i))) {

ShowNews sn = new ShowNews();
sn.setCtime(n.getCtime());
sn.setDescript(n.getDescript());
sn.setPicurl(n.getPicurl());
sn.setTitle(n.getTitle());

sdb.execSQL("insert into second values(null,?,?,?,?,?)",new
String[]{n.getCtitle(),n.getTitle(),n.getPicurl(),n.getDescript(),n.getCtime()});

al.add(sn);
}
}
child_list.add(al);
}
// 查询一级列表对应的数据表
final Cursor first_c = sdb.rawQuery("select * from first", null);
final List<String> first_list = new ArrayList<String>();
while (first_c.moveToNext()) {
first_list.add(first_c.getString(first_c
.getColumnIndex("ctitle")));
}

elv.setAdapter(new BaseExpandableListAdapter() {

public boolean isChildSelectable(int groupPosition,
int childPosition) {
// 设置儿子可以点击
return true;
}

public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}

public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// 用Android自带布局simple_list_item_1
View view = View.inflate(mcontext,
android.R.layout.simple_list_item_1, null);

TextView text = (TextView) view
.findViewById(android.R.id.text1);
// 设置一级标题
// text.setText(" "+List_ctitle.get(groupPosition));

text.setText(" " + first_list.get(groupPosition));
return view;
}

public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return 0;
}

public int getGroupCount() {
// // 返回一级列表是数据的个数
// return List_ctitle.size();
return first_c.getCount();
}

public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return null;
}

public int getChildrenCount(int groupPosition) {
// 返回二级列表是数据个数
// return child_list.get(groupPosition).size();

// 查询二级列表对应的数据表
final Cursor second_c = sdb.rawQuery(
"select * from second where ctitle = ?",
new String[] { first_list.get(groupPosition) });
return second_c.getCount();
}

public View getChildView(final int groupPosition,
final int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
// 给二级列表显示加载子布局
View view_layout = View.inflate(mcontext,
R.layout.child_item, null);
// 找到子布局控件
ImageView child_image = (ImageView) view_layout
.findViewById(R.id.child_image);
TextView text1 = (TextView) view_layout
.findViewById(R.id.text1);
TextView text2 = (TextView) view_layout
.findViewById(R.id.text2);
TextView text3 = (TextView) view_layout
.findViewById(R.id.text3);
/*
* //获得当前显示的对象 ShowNews
* news=child_list.get(groupPosition).get(childPosition);
* //设置显示内容 text1.setText(news.getTitle());
* text2.setText(news.getDescript());
* text3.setText(news.getCtime()); //ImageLoader加载图片
* ImageLoader.getInstance().displayImage(news.getPicurl(),
* child_image);
*/
// 查询二级列表对应的数据表
final Cursor second_c = sdb.rawQuery(
"select * from second where ctitle = ?",
new String[] { first_list.get(groupPosition) });
List<ShowNews> second_list = new ArrayList<ShowNews>();
while (second_c.moveToNext()) {

String title = second_c.getString(second_c
.getColumnIndex("title"));
String picurl = second_c.getString(second_c
.getColumnIndex("picurl"));
String descript = second_c.getString(second_c
.getColumnIndex("descript"));
String ctime = second_c.getString(second_c
.getColumnIndex("ctime"));
second_list.add(new ShowNews(title, picurl, ctime,
descript));
}
ShowNews news = second_list.get(childPosition);
// 设置显示内容
text1.setText(news.getTitle());
text2.setText(news.getDescript());
text3.setText(news.getCtime());
// ImageLoader加载图片
ImageLoader.getInstance().displayImage(news.getPicurl(),
child_image);

if (ss_name != null && !ss_name.equals("")) {
// 得到标题
String str = news.getTitle();
// 获得关键字在标题中第一次出现的角标
int indexOf = str.indexOf(ss_name);

if (indexOf != -1) {

SpannableStringBuilder style = new SpannableStringBuilder(
str);

// 设置指定位置文字的颜色
style.setSpan(new ForegroundColorSpan(Color.RED),
indexOf, indexOf + ss_name.length(),
Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

text1.setText(style);
}
}

return view_layout;
}

public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return 0;
}

public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return null;
}
});

// 二级列表点击监听
elv.setOnChildClickListener(new OnChildClickListener() {

public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {

Intent intent = new Intent(mcontext, DetailsActivity.class);

ShowNews showNews = child_list.get(groupPosition).get(
childPosition);
// 跳转传值
intent.putExtra("title", showNews.getTitle());
intent.putExtra("picurl", showNews.getPicurl());
intent.putExtra("desc", showNews.getDescript());

startActivity(intent);
return true;
}
});
};
};

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mcontext = this;
MyHelper helper = new MyHelper(mcontext, "huang", null, 1);
sdb = helper.getReadableDatabase();
init();
}

private void init() {
ss = (ImageView) findViewById(R.id.ss);
editText = (EditText) findViewById(R.id.editText);
elv = (ExpandableListView) findViewById(R.id.elv);
// 默认显示web前端
new MyThread("web前端").start();
ss_name = "web前端";
// 搜索监听
ss.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// 获得输入框内容
ss_name = editText.getText().toString().trim();
// 开启线程
new MyThread(ss_name).start();
}
});
}

/*
* HttpClient方法 访问服务器,得到json串
*/
public String getJsonData_HttpClient(String name) {
String rs = "";
HttpGet get = new HttpGet(URL_JSON + name);
// 设置HttpParams参数
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 5 * 1000);
HttpConnectionParams.setSoTimeout(params, 5 * 1000);
// 创建HttpClient
HttpClient client = new DefaultHttpClient(params);

try {
HttpResponse resp = client.execute(get);
// 如果响应码为200,获得响应数据
if (resp.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = resp.getEntity();
rs = EntityUtils.toString(entity, "utf-8");
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return rs;
}

/*
* 解析Json串
*/
public List<News> getList(String str) {
Gson g = new Gson();
// 解析json串
SuperData superData = g.fromJson(str, SuperData.class);

// 得到数据集合
List<News> info = superData.getInfo();
return info;
}

class MyThread extends Thread {
String name;

// 构造方法
public MyThread(String name) {
super();
this.name = name;
}

public void run() {
// 访问服务器,得到json串
String jsonData = getJsonData_HttpClient(name);
list = getList(jsonData);
// 创建HashSet集合
Set<String> Set_ctitle = new HashSet<String>();

DbUtils db = DbUtils.create(mcontext, "1407a");

try {
db.createTableIfNotExist(News.class);

} catch (DbException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 遍历list集合,为HashSet赋值
for (News n : list) {
Set_ctitle.add(n.getCtitle());
try {
db.save(n);
} catch (DbException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 把Set集合转为ArrayList集合
List<String> List_ctitle = new ArrayList<String>(Set_ctitle);
handler.sendMessage(handler.obtainMessage(1, List_ctitle));
};
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: