2014-08-27 4 views
0

J'essaye d'employer la pagination dans un projet d'androïde. Il y a 60 morceaux de données que je veux montrer 10 à la fois dans un listview.Mais le problème est que j'obtiens des doublons dans la liste que charges, soit les 10 premiers sont suivis par le même 10 fois:Travailler avec la pagination dans android

le code:

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; 
      // 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); 
     ciadp.notifyDataSetChanged(); 
     loadingMore = false; 
     list.setAdapter(ciadp); 
     start+=10; 
    } 

le début et la limite sont les valeurs qui montrent le début et le nombre d'éléments dans chaque request.I ont a également augmenté la valeur de démarrage en tant que start+=10. Je passe les valeurs de début et de limite au service Web dans la classe asynchrone.

Y at-il une façon plus élégante de faire ce qui précède? Ou pouvez-vous corriger le code ci-dessus.S'il vous plaît aider !!

+0

La valeur de départ et les données de retour sont-elles correctes lors de la demande de nouvelles données? – calvinfly

Répondre

0

initialisation de l'intérieur ice_list doInBackground éliminera les doublons

protégé Chaîne doInBackground (String ... arg0) { loadingMore = true;

 ice_list=new ArrayList<Ice_data>(); 

     // TODO Auto-generated method stub 
     try {...................... 
+0

Cela n'a pas résolu le problème .. Si vous dites nouveau mot-clé sur chaque appel du webservice alors ne serait pas effacé les anciennes données à l'intérieur et la liste commencerait comme un nouveau ... – user3852672

+0

Je ne suis pas sûr que c'est bon à faire cette ligne ciadp = new CustomIceAdapter (VideoActivity.this, ice_list); avant cette ligne voir ce tutoriel ça marche pour moi http://androidadapternotifiydatasetchanged.blogspot.in/ –

Questions connexes