activity中
package com.bwie.test;
import java.util.ArrayList;
import java.util.List;
import com.bwei.adapter.MyAdapter;
import com.bwei.bean.News;
import com.bwei.bean.News.MyNews.News2;
import com.bwie.zidingyiview.XListView;
import com.bwie.zidingyiview.XListView.IXListViewListener;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.nostra13.universalimageloader.core.ImageLoader;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;
import android.widget.TextView;
public class NewsActivity extends Activity implements IXListViewListener{
private ImageView iv_qq;
private TextView tv_qq;
private int p=1;
private List<News2> list=new ArrayList<News2>();
private MyAdapter adapter;
private XListView xlv;
private Handler handler=new Handler();
private String path="http://api.sina.cn/sinago/list.json"
+ "?channel=hdpic_funny&adid=4ad30dabe134695c3b7c3"
+ "a65977d7e72&wm=b207&from=6042095012&chwm=12050_0"
+ "001&oldchwm=12050_0001&imei=867064013906290&uid=80"
+ "2909da86d9f5fc&p=";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.newsactivity_main);
//获取控件 并赋值
init();
//获取数据地方法
getdata(p);
}
private void getdata(int p) {
//获取网络的对象
HttpUtils utils=new HttpUtils();
utils.send(HttpMethod.GET, path+p, new RequestCallBack<String>() {
//访问失败时调用
@Override
public void onFailure(HttpException arg0, String arg1) {
// TODO Auto-generated method stub
}
//访问失成功调用
@Override
public void onSuccess(ResponseInfo<String> arg0) {
// TODO Auto-generated method stub
//获取字符串
String str=arg0.result;
//gson解析
Gson gson=new Gson();
News news=gson.fromJson(str, News.class);
//System.out.println(news.data.list.get(0).title);
//添加集合
list.addAll(news.data.list);
//调用添加适配器的方法
setAdapter(list);
}
});
}
public void setAdapter(List<News2> list){
//对适配器进行判断
if(adapter==null){
adapter=new MyAdapter(this, list);
xlv.setAdapter(adapter);
}else{
//更新适配器
adapter.update(list);
}
}
//获取控件
public void init(){
iv_qq = (ImageView) findViewById(R.id.iv_qq);
tv_qq = (TextView) findViewById(R.id.tv_qq);
xlv = (XListView) findViewById(R.id.xlv);
Intent intent=getIntent();
String name=intent.getStringExtra("qqname");
String image_url=intent.getStringExtra("image");
tv_qq.setText(name);
ImageLoader.getInstance().displayImage(image_url, iv_qq);
xlv.setPullLoadEnable(true);
xlv.setPullRefreshEnable(true);
xlv.setXListViewListener(this);
}
//下拉刷新
@Override
public void onRefresh() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
list.clear();
p++;
getdata(p);
load();
}
}, 2000);
}
//上滑加载
@Override
public void onLoadMore() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
p++;
getdata(p);
load();
}
}, 2000);
}
//停止刷新 加载的方法
public void load(){
xlv.stopLoadMore();
xlv.stopRefresh();
xlv.setRefreshTime("刚刚");
}
}
适配器类
package com.bwei.adapter;
import java.util.List;
import com.bwei.bean.News.MyNews.News2;
import com.bwie.test.R;
import com.nostra13.universalimageloader.core.ImageLoader;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
//创建适配器
public class MyAdapter extends BaseAdapter {
//定义属性
private Context context;
private List<News2> list;
//构造方法
public MyAdapter(Context context,List<News2> list) {
super();
this.context=context;
this.list=list;
}
//设置总条目个数
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
//加载
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
ViewHolder vh;
if(arg1==null){
vh=new ViewHolder();
//加载布局
arg1=View.inflate(context, R.layout.item, null);
//获取控件
vh.tv_title=(TextView) arg1.findViewById(R.id.tv_title);
vh.tv_info=(TextView) arg1.findViewById(R.id.tv_info);
vh.tv_laiyuan=(TextView) arg1.findViewById(R.id.tv_laiyuan);
vh.tv_num=(TextView) arg1.findViewById(R.id.tv_num);
vh.iv_image=(ImageView) arg1.findViewById(R.id.iv_image);
arg1.setTag(vh);
}else{
vh=(ViewHolder) arg1.getTag();
}
//为控件赋值
vh.tv_title.setText(list.get(arg0).title);
vh.tv_info.setText(list.get(arg0).intro);
vh.tv_laiyuan.setText(list.get(arg0).source);
vh.tv_num.setText(list.get(arg0).comment);
ImageLoader.getInstance().displayImage(list.get(arg0).pics.list.get(0).pic, vh.iv_image);
return arg1;
}
//定义优化类
class ViewHolder{
TextView tv_title;
TextView tv_laiyuan;
TextView tv_info;
TextView tv_num;
ImageView iv_image;
}
//更新的方法
public void update(List<News2> list){
this.list=list;
this.notifyDataSetChanged();
}
}