我在片段中有以下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。感謝任何幫助解決這個問題!
只需在doInbackground方法中清除您的ArrayList ... – InnocentKiller