Java 在android中使用listview分页

Java 在android中使用listview分页,java,android,listview,pagination,Java,Android,Listview,Pagination,我想在android中对listview使用分页。我一次从web服务加载10个值。但问题是,10个值的组一直在重复。我使用了一个名为ice_data的数组列表作为listview的适配器。代码: public class VideoActivity extends Activity { private ConnectionDetector cd; public HttpResponse video_respons; public String video_string_r

我想在android中对listview使用分页。我一次从web服务加载10个值。但问题是,10个值的组一直在重复。我使用了一个名为
ice_data
的数组列表作为listview的适配器。代码:

public class VideoActivity extends Activity {
    private ConnectionDetector cd;
    public HttpResponse video_respons;
    public String video_string_response1;
    public ArrayList<NameValuePair> nameValuePairs_Video;
    ArrayList<Ice_data> ice_list;
    String URL="http://footballultimate.com/icebucket/index.php/api/getVideo";
    String URL1="http://footballultimate.com/icebucket/index.php/api/getVideoByLimit";
    JSONObject jsonobj;
    JSONArray jsonarr;
    Ice_data iceobj;
    CustomIceAdapter ciadp;
    ListView list;
    int start = 1;
     int limit = 10;
     boolean loadingMore = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);
        ice_list=new ArrayList<Ice_data>();
        // GEt all Data for Video
        cd = new ConnectionDetector(VideoActivity.this);
        Config.isInternetPresent = cd.isConnectingToInternet();
        if (!Config.isInternetPresent) {

            AlertDialog.Builder builder = new AlertDialog.Builder(VideoActivity.this);
            // Shuld be fail icon
            builder.setIcon(R.drawable.ic_launcher);
            builder.setMessage("Connection Not Available !" + "\n"
                    + "Please enable your Internet Connection");
            builder.setTitle("INTERNET CONNECTION");
            builder.setPositiveButton("Ok",
                    new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    dialog.cancel();
                }
            });
            AlertDialog alert = builder.create();
            alert.show();
        } else {
            new GetVideos().execute();
        }
        // Get all Data for Video
         list= (ListView) findViewById(R.id.videoList);
        list.setOnScrollListener(new OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView arg0, int arg1) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onScroll(AbsListView arg0, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                int lastInScreen = firstVisibleItem + visibleItemCount;   
                   if((lastInScreen == totalItemCount) && !(loadingMore)){    

                       new GetVideos().execute();
                   }

            }
        });
    }


    class GetVideos extends AsyncTask<String, String, String> {
        private ProgressDialog pDialog;
        private HttpResponse vip_respons;

        protected void onPreExecute() {
            super.onPreExecute();

            pDialog = new ProgressDialog(VideoActivity.this);
            pDialog.setTitle("Processing...");
            pDialog.setMessage("Please wait...");
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... arg0) {
            loadingMore = true;
            //ice_list=new ArrayList<Ice_data>();
            // TODO Auto-generated method stub
            try {

                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(URL1);
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                 nameValuePairs.add(new BasicNameValuePair("start",String.valueOf(start)));
               nameValuePairs.add(new BasicNameValuePair("limit",String.valueOf(limit)));
               httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                video_respons = httpclient.execute(httppost);
                //video_string_response1 = getResponseBody(video_respons);
                video_string_response1=responsetostring.getResponseBody(video_respons);
                //Log.d("Store_Response", the_string_response1);

            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
            return null;
        }
        protected void onPostExecute(String video_string) {
            try{
                   if(pDialog.isShowing()){
                       pDialog.dismiss();
                   }  
            }
          catch(Exception e){
              e.printStackTrace();
          }
          finally
          {
              pDialog.dismiss();
          }
            if (video_string_response1!=null) {
                //displayjsonstring();
                geticevalues(video_string_response1);

            }
        }
    }

    public void geticevalues(String result)
    {

        try {
            jsonobj=new JSONObject(result);
            //ice_list=new ArrayList<Ice_data>();
            jsonarr=jsonobj.getJSONArray("video_data");
            for(int i=0;i<jsonarr.length();i++)
            {
                JSONObject jso=jsonarr.getJSONObject(i);
                iceobj=new Ice_data();
                iceobj.title=jso.getString("title");
                iceobj.image_URL=jso.getString("image");
                iceobj.video_URL=jso.getString("url");
                ice_list.add(iceobj);
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        ciadp=new CustomIceAdapter(VideoActivity.this,ice_list);
        loadingMore = false;
        ciadp.notifyDataSetChanged();
        list.setAdapter(ciadp);
        start+=10;
    }
公共类VideoActivity扩展活动{
专用连接检测器cd;
公共HttpResponse视频响应;
公共字符串视频字符串响应1;
公共ArrayList nameValuePairs\u视频;
ArrayList ice_列表;
字符串URL=”http://footballultimate.com/icebucket/index.php/api/getVideo";
字符串URL1=”http://footballultimate.com/icebucket/index.php/api/getVideoByLimit";
JSONObject jsonobj;
杰索纳里·杰索纳里;
Ice_数据iceobj;
定制适配器ciadp;
列表视图列表;
int start=1;
整数极限=10;
boolean loadingMore=false;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_视频);
ice_列表=新的ArrayList();
//获取所有视频数据
cd=新的连接检测器(VideoActivity.this);
Config.isInternetPresent=cd.isConnectingToInternet();
如果(!Config.isInternetPresent){
AlertDialog.Builder=新建AlertDialog.Builder(VideoActivity.this);
//应该是失败图标
builder.setIcon(R.drawable.ic_启动器);
builder.setMessage(“连接不可用!”+“\n”
+“请启用您的Internet连接”);
builder.setTitle(“互联网连接”);
builder.setPositiveButton(“确定”,
新建DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
//TODO自动生成的方法存根
dialog.cancel();
}
});
AlertDialog alert=builder.create();
alert.show();
}否则{
新建GetVideos().execute();
}
//获取所有视频数据
列表=(ListView)findViewById(R.id.videoList);
setOnScrollListener(新的OnScrollListener(){
@凌驾
CrollStateChanged上的公共无效(AbsListView arg0,int arg1){
//TODO自动生成的方法存根
}
@凌驾
public void onScroll(AbsListView arg0,int firstVisibleItem,int visibleItemCount,int totalItemCount){
int lastInScreen=firstVisibleItem+visibleItemCount;
如果((lastInScreen==totalItemCount)&&!(loadingMore)){
新建GetVideos().execute();
}
}
});
}
类GetVideos扩展异步任务{
私人对话;
私人HttpResponse vip_Response;
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=新建进度对话框(VideoActivity.this);
pDialog.setTitle(“处理…”);
setMessage(“请稍候…”);
pDialog.setCancelable(假);
pDialog.show();
}
@凌驾
受保护的字符串doInBackground(字符串…arg0){
loadingMore=true;
//ice_列表=新的ArrayList();
//TODO自动生成的方法存根
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(URL1);
List nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“开始”,String.valueOf(开始));
添加(新的BasicNameValuePair(“限制”,String.valueOf(限制));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
video_response=httpclient.execute(httppost);
//video\u string\u response1=getResponseBody(video\u response);
video\u string\u response1=responsetostring.getResponseBody(video\u response);
//Log.d(“存储\响应”,即\字符串\响应1);
}捕获(例外e){
//TODO:处理异常
e、 printStackTrace();
}
返回null;
}
受保护的void onPostExecute(字符串视频\u字符串){
试一试{
if(pDialog.isShowing()){
pDialog.disclose();
}  
}
捕获(例外e){
e、 printStackTrace();
}
最后
{
pDialog.disclose();
}
if(视频字符串响应1!=null){
//displayjsonstring();
GetIceValue(视频字符串响应1);
}
}
}
public void geticevalues(字符串结果)
{
试一试{
jsonobj=新的JSONObject(结果);
//ice_列表=新的ArrayList();
jsonarr=jsonobj.getJSONArray(“视频数据”);

对于(int i=0;我让你检查第二页的响应数据,我认为第二页的响应数据可能是相同的,也可能是其他方面的,你的代码看起来不错。但是当我第一次加载页面时,加载了两组相同的数据。这意味着对Web服务的请求在开始时会发生两次。我认为这是因为第一次加载时我们的列表是空列表,调用了setOnScrollListener(),新的GetVideos().execute();在else情况下先执行两次,然后执行setOnScrollListener(),所以我认为您调试了setOnScrollListener(),然后就得到了确切的想法。我尝试添加if(ice_data.size()!=0)在滚动侦听器中。第一组值不显示为重复值。但第二组值显示为重复值。如何修复它?在将ice_列表值提供给适配器之前,请尝试调试ice_列表值,然后您就可以确切了解ice_列表数据的情况。