我有一个有两个AsyncTask的Activity。在我的操作栏中,我有两个项目,当我点击调用其中一个AsyncTask执行eventosList.clear();
不将新的AsyncTask结果附加到列表中。我不知道我正在做什么问题,因为GetPrograma第一次工作正常,GetProgramaDos工作正常,但是如果我再调用另一个调用GetPrograma的按钮工作不好,因为processDialog没有消失而且清除了工作。这是我的代码,有人可以帮帮我吗?谢谢。
public class MyActivity extends ActionBarActivity {
private ProgressDialog pDialog;
View v;
// contacts JSONArray
String results;
LazyAdapterClasificacion adapter;
String jsonStr;
JSONArray eventos = null;
// Hashmap for ListView
ArrayList<HashMap<String, String>> eventosList;
static final String TAG_ID= "id";
static final String TAG_NAME = "name";
static final String TAG_TF= "tf";
static final String TAG_TC= "tc";
static final String TAG_COEF= "coef";
String idioma;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lista_clasificacion);
ActionBar actionBar = getSupportActionBar(); // || getActionBar();
actionBar.setIcon(getResources().getDrawable(R.drawable.navbar_logo));
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#071607")));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
actionBar.setTitle("My Title");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
eventosList = new ArrayList<HashMap<String, String>>();
// Calling async task to get json
if ((new UtilsDos(MyActivity.this).isConnected())){
new GetPrograma().execute();
}
}
/**
* Async task class to get json by making HTTP call
* */
@SuppressLint("NewApi")
private class GetPrograma extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
eventosList.clear();
pDialog = new ProgressDialog(MyActivity.this);
pDialog.setMessage("Por favor, espere...");
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
jsonStr = sh.makeServiceCall("myurl", ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
eventos = jsonObj.getJSONArray("results");
// looping through All Contacts
for (int i = 0; i < eventos.length(); i++) {
JSONObject c = eventos.getJSONObject(i);
String id = c.getString(TAG_ID);
String pilotari = c.getString(TAG_NAME);
String tf = c.getString(TAG_TF);
String tc = c.getString(TAG_TC);
String coef = c.getString(TAG_COEF);
// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();
// adding each child node to HashMap key => value
contact.put("id", id);
contact.put("name", name);
contact.put("tf", tf);
contact.put("tc", tc);
contact.put("coef", coef);
// adding contact to contact list
eventosList.add(contact);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
@SuppressLint("NewApi")
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
adapter=new LazyAdapterClasificacion(MyActivity.this, eventosList, "fonts/Roboto-Thin.ttf");
ListView milista = (ListView) findViewById(R.id.lista);
milista.setAdapter(adapter);
pDialog.dismiss();
}
}
/**
* Async task class to get json by making HTTP call
* */
@SuppressLint("NewApi")
private class GetProgramaDos extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MyActivity.this);
pDialog.setMessage("Por favor, espere...");
eventosList.clear();
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
jsonStr = sh.makeServiceCall("myurldos", ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
eventos = jsonObj.getJSONArray("results");
// looping through All Contacts
for (int i = 0; i < eventos.length(); i++) {
JSONObject c = eventos.getJSONObject(i);
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String tf = c.getString(TAG_TF);
String tc = c.getString(TAG_TC);
String coef = c.getString(TAG_COEF);
// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();
// adding each child node to HashMap key => value
contact.put("id", id);
contact.put("name", name);
contact.put("tf", tf);
contact.put("tc", tc);
contact.put("coef", coef);
// adding contact to contact list
eventosList.add(contact);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
@SuppressLint("NewApi")
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
adapter=new LazyAdapterClasificacion(MyActivity.this, eventosList, "fonts/Roboto-Thin.ttf");
ListView milista = (ListView) findViewById(R.id.lista);
milista.setAdapter(adapter);
pDialog.dismiss();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_clasificaciones_grupo, menu);
return true;
}
/* *
* Called when invalidateOptionsMenu() is triggered
*/
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id.grupo1).setVisible(true);
menu.findItem(R.id.grupo2).setVisible(true);
return super.onPrepareOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
this.finish();
return true;
case R.id.grupo1:
if ((new UtilsDos(MyActivity.this).isConnected())){
new GetPrograma().execute();
}
case R.id.grupo2:
if ((new UtilsDos(MyActivity.this).isConnected())){
new GetProgramaDos().execute();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}