2014-01-31 67 views
1

我在片段中有以下AsyncTask,並希望清除列表視圖並重新填充單擊按鈕時。這是我曾嘗試:如何清除ListView中的數據? AsyncTask

按鈕點擊:

btnRefresh.setOnClickListener(new View.OnClickListener() { 
    @Override 
public void onClick(View view) { 
    lv.setAdapter(null); 
     new LoadAllProducts().execute(); 
} 
}); 

這裏是我的AsyncTask:

class LoadAllProducts extends AsyncTask<String, String, String> { 

    //ListView lv = (ListView) rootView.findViewById(R.id.list); 
    /** 
    * Before starting background thread Show Progress Dialog 
    * */ 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 

     lv.setAdapter(null); 
     pDialog = new ProgressDialog(getActivity()); 
     pDialog.setMessage("Loading your trips. Please wait..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(false); 
     pDialog.show(); 
    } 

    /** 
    * getting All products from url 
    * */ 
    protected String doInBackground(String... args) { 
     // Building Parameters 
     List<NameValuePair> params = new ArrayList<NameValuePair>(); 
     // getting JSON string from URL 
     JSONObject json = jParser.makeHttpRequest(url_all_trips, "GET", params); 

     // Check your log cat for JSON response 
     Log.d("All Trips: ", json.toString()); 

     try { 
      // Checking for SUCCESS TAG 
      int success = json.getInt(TAG_SUCCESS); 

      if (success == 1) { 
       // products found 
       // Getting Array of Products 
       trips = json.getJSONArray(TAG_TRIPS); 

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

        // Storing each json item in variable 
        String tripid = c.getString(TAG_TRIPID); 
        String tripname = c.getString(TAG_TRIPNAME); 
        String userId = c.getString("uid"); 

        // creating new HashMap 
        DatabaseHandler_Helpers db = new DatabaseHandler_Helpers(getActivity()); 
        HashMap<String, String> map = new HashMap<String, String>(); 

        if (userId.equals(db.getUserDetails().get("uid"))) { 
         // adding each child node to HashMap key => value 
         map.put(TAG_TRIPID, tripid); 
         map.put(TAG_TRIPNAME, tripname); 

         // adding HashList to ArrayList 
         tripList.add(map); 
        } //else { 
         //map.put(TAG_TRIPID, ""); 
         //map.put(TAG_TRIPNAME, "You have no tracked trips."); 

         //tripList.add(map); 
        //} 
       } 
      } else { 
       // no products found 
       // Launch Add New product Activity 
       Intent i = new Intent(getActivity(), 
         NewTrip_Activity.class); 
       // Closing all previous activities 
       i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       startActivity(i); 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

     return null; 
    } 

    /** 
    * After completing background task Dismiss the progress dialog 
    * **/ 
    protected void onPostExecute(String file_url) { 
     // dismiss the dialog after getting all products 
     pDialog.dismiss(); 

     // updating UI from Background Thread 
     ((Activity) getActivity()).runOnUiThread(new Runnable() { 
      public void run() { 
       /** 
       * Updating parsed JSON data into ListView 
       * */ 


       ListAdapter adapter = new SimpleAdapter(
         getActivity(), tripList, 
         R.layout.list_item, new String[] { TAG_TRIPID, 
           TAG_TRIPNAME, "TEST"}, 
         new int[] { R.id.pid, R.id.name, R.id.mileage }); 
       // updating listview 
       ((ListView) lv.findViewById(R.id.list)).setAdapter(adapter); 
      } 
     }); 
    } 
} 

問題是,點擊按鈕清除列表視圖,但然後將兩次的項目背部。那麼,列表視圖中已經有了什麼,再加上相同的項目。如果再次點擊按鈕,它會第三次添加項目!

我知道我必須缺少一些東西,但所有我在網上找到的是使用ArrayAdapter而不是ListAdapter。感謝任何幫助解決這個問題!

+0

只需在doInbackground方法中清除您的ArrayList ... – InnocentKiller

回答

2

你需要清除tripList.clear()您tripList(數組列表)前的地圖添加到他們tripList.add(map)否則將添加到現有的舊值

+0

工程就像一個魅力!謝謝! – user2573690

+0

我很高興我能幫上忙 – Nambi

2

製作適配器全球

ListAdapter adapter ; 

和更改的onClick這個

public void onClick(View view) { 
       new LoadAllProducts().execute(); 
       adapter.notifyDataSetChanged(); 
} 

你也需要檢查tripList其不追加添加項目弄清楚之前列表。

+0

我沒有notifyDataSetC在適配器下的hanged()方法中,只有notify()。現在嘗試一個。 – user2573690

0

你必須在全球範圍內聲明的適配器和使用它的實例來清除數據,然後再填一遍

1

do adpater.clear(); 和tripList.clear();

0

你有適配器

btnRefresh.setOnClickListener(new View.OnClickListener() { 
@Override 
public void onClick(View view) { 
    lv.setAdapter(null); 
    adapter.notifyDataSetChanged(); 
    new LoadAllProducts().execute(); 
} 
}); 
1

設置爲null您的AsyncTask執行可以清除您的ArrayList

tripList.clear(); 
adapter.notifyDataSetChanged(); 

,或者你可以設置適配器空也

lv.setAdapter(null); 
adapter.notifyDataSetChanged(); 
後後referesh列表視圖