您好我是android新手。有一个arrayList和一个ListView。我使用AsyncTask类从MYSQL DB调用数据库。此AsyncTask类设置mArrayList(这是arrayList)。当我从另一个活动返回时更新列表视图,我使用了onResume()。这是一部分。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mold_breakage_history);
brokenMoldListView = findViewById(R.id.brokenMoldListView);
mArrayList = new ArrayList<>();
GetData task = new GetData();
task.execute("http://www.cafe24.com/aaa.php");
}
@Override
protected void onResume() {
super.onResume();
mArrayList = new ArrayList<>();
GetData task = new GetData();
task.execute("http://www.cafe24.com/aaa.php");
在onResume()中,我初始化了mArrayList并再次调用AsyncTask来更新ListView。问题是,当首次执行此活动时,ListView被复制。但是,当我从本活动的下一页回来时,问题就消失了。我希望在首次执行活动时不存在此问题。请帮忙。 这是AsyncTask类的代码。
@SuppressLint("StaticFieldLeak")
private class GetData extends AsyncTask<String, Void, String> {
ProgressDialog progressDialog;
String errorString = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(MoldBreakageHistoryActivity.this,
"Please Wait", null, true, true);
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
progressDialog.dismiss();
Log.d(TAG, "response - " + result);
mJsonString = result;
showResult();
}
@Override
protected String doInBackground(String... params) {
String serverURL = params[0];
try {
URL url = new URL(serverURL);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setReadTimeout(5000);
httpURLConnection.setConnectTimeout(5000);
httpURLConnection.connect();
int responseStatusCode = httpURLConnection.getResponseCode();
Log.d(TAG, "response code - " + responseStatusCode);
InputStream inputStream;
if (responseStatusCode == HttpURLConnection.HTTP_OK) {
inputStream = httpURLConnection.getInputStream();
} else {
inputStream = httpURLConnection.getErrorStream();
}
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
bufferedReader.close();
return sb.toString().trim();
} catch (Exception e) {
Log.d(TAG, "InsertData: Error ", e);
errorString = e.toString();
return null;
}
}
}
private void showResult() {
try {
JSONObject jsonObject = new JSONObject(mJsonString);
JSONArray jsonArray = jsonObject.getJSONArray(TAG_JSON);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject item = jsonArray.getJSONObject(i);
String brokenDate = item.getString(TAG_BROKEN_DATE);
String moldCode = item.getString(TAG_MOLD_CODE);
String finalHitting = item.getString(TAG_FINAL_HITTING_TIMES);
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put(TAG_BROKEN_DATE, brokenDate);
hashMap.put(TAG_MOLD_CODE, moldCode);
hashMap.put(TAG_FINAL_HITTING_TIMES, finalHitting);
mArrayList.add(hashMap);
}
ListAdapter adapter = new SimpleAdapter(
MoldBreakageHistoryActivity.this, mArrayList, R.layout.list_item_broken_mold,
new String[]{TAG_BROKEN_DATE, TAG_MOLD_CODE, TAG_FINAL_HITTING_TIMES},
new int[]{R.id.brokenDateListItem, R.id.brokenMoldListItem, R.id.finalHittingTimesListItem}
);
brokenMoldListView.setAdapter(adapter);
} catch (JSONException e) {
Log.d(TAG, "showResult : ", e);
}
}
答案 0 :(得分:3)
删除它:
mArrayList = new ArrayList<>();
GetData task = new GetData();
task.execute("http://www.cafe24.com/aaa.php");
来自onCreate
。在onResume
之后执行onCreate
并且您的异步任务执行了两次,这就是为什么它首先被复制的原因。当你点击后面时只执行onResume,那么问题就会发生。
答案 1 :(得分:0)
请删除
mArrayList = new ArrayList<>();
GetData task = new GetData();
task.execute("http://www.cafe24.com/aaa.php");
来自你的onCreate方法。
但是,如果您需要在onCreate和onResume中的其他场景中执行som场景,则可以在执行新任务之前使用mArrayList.clear();
。