0
我有一个AsyncTask连接到服务,并且有一个适配器在ListView中设置结果。在操作栏中,我想放置一个按钮来执行刷新操作,但问题是,当我单击此按钮并调用服务时,它会在列表视图中复制结果。在列表视图中做Asynctask的重复值
我曾尝试:
ListView myList=(ListView)findViewById(R.id.list);
myList.setAdapter(null);
if ((new Utils(this)).isConnected()){
new MyTask().execute();
}
我的AsyncTask代码:
private class MyTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MyActivity.this);
pDialog.setMessage("searching...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url+id, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONArray jsonObj = new JSONArray(jsonStr);
// looping through All Contacts
for (int i = 0; i < jsonObj.length(); i++) {
JSONObject c = jsonObj.getJSONObject(i);
String nick = c.getString("nick");
String minuto = c.getString("minuto");
String fecha = c.getString("fecha");
// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();
// adding each child node to HashMap key => value
contact.put("nick", nick);
contact.put("minuto", minuto);
contact.put("fecha", fecha);
// adding contact to contact list
contactList.add(contact);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
DetallePelicula.this, contactList,
R.layout.list_rowopiniones, new String[] { "nick", "minuto",
"fecha" }, new int[] { R.id.title,
R.id.minuto, R.id.fecha });
ListView myList=(ListView)findViewById(R.id.list);
myList.setAdapter(adapter);
}
}
有人能帮助我吗?谢谢
后的代码位你设置新的值到ListView请。 – Jave
尝试使用clear()方法删除数组中的所有数据 – Rogue
我已使用AsyncTask更新了我的问题 – Igor