用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - android代码库

实现Xlistview多条目加载

2017-02-28 作者: xjh举报

[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;
	}
}



网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...