2014-05-18 3 views
0

J'ai une asyncTask qui se connecte à un service et avec un adaptateur définir le résultat dans un ListView. Dans la barre d'action, je veux mettre un bouton pour faire l'action d'actualisation, mais le problème est que lorsque je clique sur ce bouton et que j'appelle le service, il duplique les résultats dans la liste.Valeurs dupliquées faisant asynctask dans Listview

J'ai essayé:

ListView myList=(ListView)findViewById(R.id.list); 
      myList.setAdapter(null); 
      if ((new Utils(this)).isConnected()){ 
        new MyTask().execute(); 

       } 

Mon code AsyncTask:

private class MyTask extends AsyncTask<Void, Void, Void> { 

      @Override 
      protected void onPreExecute() { 
       super.onPreExecute(); 
       // Showing progress dialog 
       pDialog = new ProgressDialog(MyActivity.this); 
       pDialog.setMessage("searching..."); 
       pDialog.setCancelable(false); 
       pDialog.show(); 
      } 

      @Override 
      protected Void doInBackground(Void... arg0) { 
       // Creating service handler class instance 
       ServiceHandler sh = new ServiceHandler(); 

       // Making a request to url and getting response 
       String jsonStr = sh.makeServiceCall(url+id, ServiceHandler.GET); 

       Log.d("Response: ", "> " + jsonStr); 

       if (jsonStr != null) { 
        try { 
         JSONArray jsonObj = new JSONArray(jsonStr); 

         // looping through All Contacts 
         for (int i = 0; i < jsonObj.length(); i++) { 
          JSONObject c = jsonObj.getJSONObject(i); 

          String nick = c.getString("nick"); 
          String minuto = c.getString("minuto"); 
          String fecha = c.getString("fecha"); 

          // tmp hashmap for single contact 
          HashMap<String, String> contact = new HashMap<String, String>(); 

          // adding each child node to HashMap key => value 


          contact.put("nick", nick); 
          contact.put("minuto", minuto); 
          contact.put("fecha", fecha); 

          // adding contact to contact list 
          contactList.add(contact); 
         } 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } else { 
        Log.e("ServiceHandler", "Couldn't get any data from the url"); 
       } 

       return null; 
      } 

      @Override 
      protected void onPostExecute(Void result) { 
       super.onPostExecute(result); 
       // Dismiss the progress dialog 
       if (pDialog.isShowing()) 
        pDialog.dismiss(); 
       /** 
       * Updating parsed JSON data into ListView 
       * */ 
       ListAdapter adapter = new SimpleAdapter(
         DetallePelicula.this, contactList, 
         R.layout.list_rowopiniones, new String[] { "nick", "minuto", 
           "fecha" }, new int[] { R.id.title, 
           R.id.minuto, R.id.fecha }); 

       ListView myList=(ListView)findViewById(R.id.list); 


       myList.setAdapter(adapter); 



      } 
     } 

Quelqu'un peut me aider? merci

+0

Poster le bit de code où vous définissez les nouvelles valeurs à la ListView s'il vous plaît. – Jave

+1

Essayez de supprimer toutes les données de votre tableau avec la méthode clear() – Rogue

+0

J'ai mis à jour ma question avec mon AsyncTask – Igor

Répondre

0

Dans votre doInBackground, vous ajoutez les nouvelles données à une liste déjà existante. Cela signifie que, comme vous l'avez mentionné, les données seront dupliquées.

Il suffit d'ajouter contactList.clear() à votre méthode onPreExecute:

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

      contactList.clear(); // Add this line 

      // Showing progress dialog 
      pDialog = new ProgressDialog(MyActivity.this); 
      pDialog.setMessage("searching..."); 
      pDialog.setCancelable(false); 
      pDialog.show(); 
     } 
Questions connexes