2014-05-18 74 views
0

我有一个AsyncTask连接到服务,并且有一个适配器在ListView中设置结果。在操作栏中,我想放置一个按钮来执行刷新操作,但问题是,当我单击此按钮并调用服务时,它会在列表视图中复制结果。在列表视图中做Asynctask的重复值

我曾尝试:

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

       } 

我的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); 



      } 
     } 

有人能帮助我吗?谢谢

+0

后的代码位你设置新的值到ListView请。 – Jave

+1

尝试使用clear()方法删除数组中的所有数据 – Rogue

+0

我已使用AsyncTask更新了我的问题 – Igor

回答

0

在你的doInBackground,你正在添加新的数据到一个已经存在的列表。这意味着,正如你所提到的,数据将会重复。

只需添加contactList.clear()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(); 
     }