我在片段中有以下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);
}
});
}
}
问题是单击按钮会清除列表视图,但会再添加两次项目。那么listview中已有的内容再加上相同的项目。如果再次单击该按钮,它会第三次添加项目!
我知道我必须遗漏一些东西,但我在网上找到的只是使用ArrayAdapter而不是ListAdapter。感谢您解决此问题的任何帮助!
答案 0 :(得分:2)
使适配器全局
ListAdapter adapter ;
将onClick更改为此
public void onClick(View view) {
new LoadAllProducts().execute();
adapter.notifyDataSetChanged();
}
另外,在添加项目之前,您需要检查tripList
其未附加列表。
答案 1 :(得分:2)
在将地图添加到tripList.clear()
之前,您需要清除tripList.add(map)
您的tripList(arraylist),否则它会添加到现有的旧值
答案 2 :(得分:1)
做adpater.clear(); 和tripList.clear();
答案 3 :(得分:1)
执行AsyncTask后,您可以清除您的arraylist
tripList.clear();
adapter.notifyDataSetChanged();
或者你也可以设置适配器null
lv.setAdapter(null);
adapter.notifyDataSetChanged();
答案 4 :(得分:0)
您必须全局声明适配器并使用其实例清除数据然后再次填充
答案 5 :(得分:0)
在适配器
中设置null后,您必须刷新ListviewbtnRefresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
lv.setAdapter(null);
adapter.notifyDataSetChanged();
new LoadAllProducts().execute();
}
});