[android]代码库
////////////////////////////MainActivity
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import bean.Bean;
import bean.Info;
import com.adapter.weixin.MyBaseAdapter;
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.xlistview.xlistview.XListView;
import com.xlistview.xlistview.XListView.IXListViewListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
public class MainActivity extends Activity implements IXListViewListener{
private XListView xListView;
private String path;
List<Info> list = new ArrayList<Info>();
private MyBaseAdapter myBaseAdapter;
private int tag=1;
Handler handler=new Handler(){
public void handleMessage(Message msg) {
//获取从网络获取的数据
String s=(String) msg.obj;
Gson gson=new Gson();
Bean bean = gson.fromJson(s, Bean.class);
int i=msg.arg1;
switch (i) {
case 1:
//清空之前的数据
list.clear();
list.addAll(bean.result.list);
myBaseAdapter.notifyDataSetChanged();
xListView.stopRefresh();
//刷新时间
SimpleDateFormat dateFormat=new SimpleDateFormat();
String format = dateFormat.format(new Date());
xListView.setRefreshTime(format);
break;
case 2:
list.addAll(bean.result.list);
myBaseAdapter.notifyDataSetChanged();
xListView.stopLoadMore();
break;
case 3:
list.addAll(bean.result.list);
myBaseAdapter=new MyBaseAdapter(MainActivity.this, list);
xListView.setAdapter(myBaseAdapter);
break;
default:
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
xListView=(XListView) findViewById(R.id.listview);
getData(3);
//开启加载更多
xListView.setPullLoadEnable(true);
//设置监听
xListView.setXListViewListener(this);
}
//下拉刷新
@Override
public void onRefresh() {
// TODO Auto-generated method stub
//联网得到新的数据
getData(1);
}
//上拉加载
@Override
public void onLoadMore() {
// TODO Auto-generated method stub
getData(2);
}
private void getData(final int i) {
//上拉刷新的联网请求
if(i==1){
path = "http://v.juhe.cn/weixin/query?key=b3edac6e41f3f3c84b855019e47b7ace&pno=" + 1;
}else if(i==2){
path = "http://v.juhe.cn/weixin/query?key=b3edac6e41f3f3c84b855019e47b7ace&pno=" +(tag+=1);
}//第一次进入时的界面请求的数据
else if(i==3){
path = "http://v.juhe.cn/weixin/query?key=b3edac6e41f3f3c84b855019e47b7ace&pno=" + 1;
}
HttpUtils httpUtils=new HttpUtils();
httpUtils.send(HttpMethod.GET,path,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
//把响应信息发送给hander
Message msg=Message.obtain();
String result = arg0.result;
msg.obj=result;
msg.arg1=i;
handler.sendMessage(msg);
}
});
}
}
//////////////////////////////////////////////////////////适配器
public class MyBaseAdapter extends BaseAdapter{
private Context context;
private List<Info> list;
public MyBaseAdapter(Context context, List<Info> 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 position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder v;
if(convertView==null){
convertView=View.inflate(context, R.layout.listview,null);
v=new ViewHolder();
v.name=(TextView) convertView.findViewById(R.id.listview_name);
v.title=(TextView) convertView.findViewById(R.id.listview_title);
convertView.setTag(v);
}else{
v=(ViewHolder) convertView.getTag();
}
v.title.setText(list.get(position).url);
v.name.setText(list.get(position).title);
return convertView;
}
class ViewHolder{
TextView title,name;
}
}
by: 发表于:2017-09-27 10:00:51 顶(0) | 踩(0) 回复
??
回复评论