package com.example.extab_text;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import com.example.helper.MyHelper;
import com.example.vo.Info;
import com.example.vo.ShowInfo;
import com.example.vo.SuperJsonPerse;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;
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.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
private List<Info> list;
public static final String URL = "http://www.hengboit.com/json/json_search.php?wd";
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
// 获得一级列表条目的集合
final List<String> list_ctitle = (List<String>) msg.obj;
// 创建二维集合
final List<ArrayList<ShowInfo>> chile_list = new ArrayList<ArrayList<ShowInfo>>();
for (int i = 0; i < list_ctitle.size(); i++) {
// 创建二维集合里的子集合
ArrayList<ShowInfo> al = new ArrayList<ShowInfo>();
database.execSQL("insert into user values(null,?)",
new String[] { list_ctitle.get(i) });
for (Info n : list) {
if (n.getCtitle().equals(list_ctitle.get(i))) {
// 创建实体类对象
ShowInfo si = new ShowInfo();
si.setCtime(n.getCtime());
si.setTitle(n.getTitle());
si.setDescript(n.getDescript());
si.setPicurl(n.getPicurl());
database.execSQL(
"insert into user1 values(null,?,?,?,?,?)",
new String[] { n.getCtitle(), n.getTitle(),
n.getPicurl(), n.getDescript(),
n.getCtime() });
al.add(si);
}
chile_list.add(al);
}
for (int j = 0; j < list_ctitle.size(); j++) {
}
elv.setAdapter(new BaseExpandableListAdapter() {
public boolean isChildSelectable(int groupPosition,
int childPosition) {
// TODO Auto-generated method stub
return true;
}
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
public View getGroupView(int groupPosition,
boolean isExpanded, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
View view = View.inflate(MainActivity.this,
android.R.layout.simple_list_item_1, null);
TextView textView = (TextView) view
.findViewById(android.R.id.text1);
textView.setText(list_ctitle.get(groupPosition));
return view;
}
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return 0;
}
public int getGroupCount() {
// TODO Auto-generated method stub
return list_ctitle.size();
}
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return 0;
}
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
//
return chile_list.get(groupPosition).size();
}
public View getChildView(int groupPosition,
int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
// 给二级列表显示添加子布局
View view = View.inflate(MainActivity.this,
R.layout.item, null);
ImageView imageView = (ImageView) view
.findViewById(R.id.image);
TextView textView1 = (TextView) view
.findViewById(R.id.textView1);
TextView textView2 = (TextView) view
.findViewById(R.id.textView2);
TextView textView3 = (TextView) view
.findViewById(R.id.textView3);
// 获得当前显示的对象
ShowInfo infos = chile_list.get(groupPosition).get(
childPosition);
// 设置显示内容
textView1.setText(infos.getTitle());
textView2.setText(infos.getDescript());
textView3.setText(infos.getCtime());
// 使用Imageloader加载图片
ImageLoader.getInstance().displayImage(
infos.getPicurl(), imageView);
return view;
}
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
});
}
elv.setOnChildClickListener(new OnChildClickListener() {
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,
DetailsActivity.class);
ShowInfo showinfo = chile_list.get(groupPosition).get(
childPosition);
intent.putExtra("title", showinfo.getTitle());
intent.putExtra("picurl", showinfo.getPicurl());
intent.putExtra("desc", showinfo.getDescript());
startActivity(intent);
return false;
}
});
}
};
private ExpandableListView elv;
private SQLiteDatabase database;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private List<Info> perseJson(String str) {
// TODO Auto-generated method stub
Gson g = new Gson();
SuperJsonPerse sjp = g.fromJson(str, SuperJsonPerse.class);
ArrayList<Info> list_ss = sjp.getInfo();
return list_ss;
};
private void init() {
// TODO Auto-generated method stub
EditText editText = (EditText) findViewById(R.id.ed_name);
Button button = (Button) findViewById(R.id.b1);
MyHelper helper = new MyHelper(MainActivity.this, "ccccccccccc", null,
1);
database = helper.getWritableDatabase();
elv = (ExpandableListView) findViewById(R.id.elv);
new Thread() {
public void run() {
String data = getJsonFromServer();
list = perseJson(data);
// 创建集合,过滤
Set<String> set_ctitle = new HashSet<String>();
// 遍历集合,为hashset赋值
for (Info n : list) {
set_ctitle.add(n.getCtitle());
}
// 把set集合转化为Arraylist集合
List<String> list_ctitle = new ArrayList<String>(set_ctitle);
handler.sendMessage(handler.obtainMessage(1, list_ctitle));
};
}.start();
}
private String getJsonFromServer() {
StringBuffer sb = new StringBuffer();
try {
URL url = new URL(URL);
HttpURLConnection urlcon = (HttpURLConnection) url.openConnection();
urlcon.setConnectTimeout(5000);
urlcon.setReadTimeout(5000);
if (urlcon.getResponseCode() == 200) {
BufferedReader br = new BufferedReader(new InputStreamReader(
urlcon.getInputStream(), "utf-8"));
String str = "";
while ((str = br.readLine()) != null) {
sb.append(str);
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sb.toString();
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
转载于:https://www.cnblogs.com/6660qq/p/5158746.html