public class CuxiaoActivity extends AppCompatActivity {
private BaseAdapter adapter;
private ListView lv;
private Button cu;
private List<Cuxiaoli> list;
Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
adapter=new MyAdapter();
lv.setAdapter(adapter);
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cuxiao);
//接收listView控件
lv=(ListView)findViewById(R.id.culv);
//接收cu控件
cu=(Button)findViewById(R.id.cu);
cu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
new Thread(){
private String text;
public void run() {
try {
//创建URL
URL url=new URL("http://121.42.8.95:8090/ECServer_D/topic");
//创建连接HttpURLContention对象
HttpURLConnection connection=(HttpURLConnection) url.openConnection();
//执行
connection.connect();
//获得响应吗
int code=connection.getResponseCode();
//判断响应码正确是为200
if(code==200){
//输入流
InputStream inputStream=connection.getInputStream();
byte[] by=new byte[1024];
//输出流
ByteArrayOutputStream bos=new ByteArrayOutputStream();
int len=-1;
//
while((len=inputStream.read(by))!=-1){
bos.write(by, 0, len);
}
//关闭
inputStream.close();
//把值付给text
text=bos.toString();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Gson gson=new Gson();
Cuxiao cuxi=gson.fromJson(text,Cuxiao.class);
list=cuxi.getTopic();
handler.sendEmptyMessageAtTime(0,1000);
};
}.start();
}
//适配器
class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewholder;
if(convertView!=null){
viewholder=(ViewHolder) convertView.getTag();
}
else{
convertView= LayoutInflater.from(CuxiaoActivity.this).inflate(R.layout.cuxiaolv,null);
viewholder=new ViewHolder();
viewholder.name=(TextView)convertView.findViewById(R.id.cuname);
viewholder.image=(ImageView)convertView.findViewById(R.id.cuimage);
convertView.setTag(viewholder);
}
viewholder.name.setText(list.get(position).getName());
ImageLoader imageLoader=ImageLoader.getInstance();
imageLoader.displayImage(list.get(position).getPic(),viewholder.image);
return convertView;
}
class ViewHolder{
private TextView name;
private ImageView image;
}
}
private BaseAdapter adapter;
private ListView lv;
private Button cu;
private List<Cuxiaoli> list;
Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
adapter=new MyAdapter();
lv.setAdapter(adapter);
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cuxiao);
//接收listView控件
lv=(ListView)findViewById(R.id.culv);
//接收cu控件
cu=(Button)findViewById(R.id.cu);
cu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
new Thread(){
private String text;
public void run() {
try {
//创建URL
URL url=new URL("http://121.42.8.95:8090/ECServer_D/topic");
//创建连接HttpURLContention对象
HttpURLConnection connection=(HttpURLConnection) url.openConnection();
//执行
connection.connect();
//获得响应吗
int code=connection.getResponseCode();
//判断响应码正确是为200
if(code==200){
//输入流
InputStream inputStream=connection.getInputStream();
byte[] by=new byte[1024];
//输出流
ByteArrayOutputStream bos=new ByteArrayOutputStream();
int len=-1;
//
while((len=inputStream.read(by))!=-1){
bos.write(by, 0, len);
}
//关闭
inputStream.close();
//把值付给text
text=bos.toString();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Gson gson=new Gson();
Cuxiao cuxi=gson.fromJson(text,Cuxiao.class);
list=cuxi.getTopic();
handler.sendEmptyMessageAtTime(0,1000);
};
}.start();
}
//适配器
class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewholder;
if(convertView!=null){
viewholder=(ViewHolder) convertView.getTag();
}
else{
convertView= LayoutInflater.from(CuxiaoActivity.this).inflate(R.layout.cuxiaolv,null);
viewholder=new ViewHolder();
viewholder.name=(TextView)convertView.findViewById(R.id.cuname);
viewholder.image=(ImageView)convertView.findViewById(R.id.cuimage);
convertView.setTag(viewholder);
}
viewholder.name.setText(list.get(position).getName());
ImageLoader imageLoader=ImageLoader.getInstance();
imageLoader.displayImage(list.get(position).getPic(),viewholder.image);
return convertView;
}
class ViewHolder{
private TextView name;
private ImageView image;
}
}