gpt4 book ai didi

android - 如何清除ListView中的数据?异步任务

转载 作者:行者123 更新时间:2023-11-29 14:50:00 26 4
gpt4 key购买 nike

我在 fragment 中有以下 AsyncTask,我想清除 ListView 并在单击按钮时重新填充它。这是我尝试过的:

按钮点击:

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 ,但随后会将项目添加两次。因此, ListView 中已经存在的内容再次加上相同的项目。如果您再次单击该按钮,它会第三次添加项目!

我知道我一定遗漏了一些东西,但我在网上找到的只是使用 ArrayAdapter 而不是 ListAdapter。感谢您帮助解决这个问题!

最佳答案

在将 map 添加到它们之前,您需要清除 tripList.clear() 您的 tripList(arraylist) tripList.add(map) 否则它将添加到现有的旧值

关于android - 如何清除ListView中的数据?异步任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21473029/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com