I’m trying to show a loading icon to the user when the application makes a query to online Database. I’ve tried using a AnimationDrawable (I gave up because there was no need of a custom icon), ProgressDialog and ProgressBar.
The ProgressBar seems most appropriate, since I don’t want a message, just a spinning icon. But I can not even make a ProgressBar appear on the screen, doesn’t matter where I call it.
I’ve got the ProgressDialog appearing in screen, but it only appears after the server’s response, and if I use dismiss() or cancel() it doesn’t even appear at all.
I’ve had any success using AsyncTasks or Threads.
In the app, there is a class JogarActivity.java that attemps to show a list of options. It receives some parameters like the user id, and calls UserFunctions:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.jogar_layout);
Intent in = getIntent();
String url = this.getString(R.string.urlSite);
ArrayList<HashMap<String, String>> respostaList = new ArrayList<HashMap<String, String>>();
String idt = in.getStringExtra(TAG_ID);
primeiraPergunta = in.getBooleanExtra(TAG_PRIMEIRAPERGUNTA, true);
TextView insertPergunta = (TextView) findViewById(R.id.insertPergunta);
ListView insertRespostas = (ListView) findViewById(R.id.listResposta);
SharedPreferences settings = getSharedPreferences("PREFS_LOGIN", MODE_PRIVATE);
Integer idUsuario = settings.getInt("idUsuario", 0);
String idUser = idUsuario.toString();
if (primeiraPergunta){
UserFunctions userFunction = new UserFunctions();
json = userFunction.getJogar(idt, idUser);
}else{
try {
json = new JSONArray(in.getStringExtra(TAG_JSON));
json = json.getJSONArray(2);
} catch (JSONException e) {
e.printStackTrace();
}
}
Below is the getJogar function in userFunctions:
public JSONArray getJogar(String categoria, String usuarioId){
List params = new ArrayList();
params.add(new BasicNameValuePair("categoria", categoria));
params.add(new BasicNameValuePair("idUsuario", usuarioId));
JSONArray json = jsonParser.getJSONFromUrl(perguntaURL, params);
return json;
}
JSONParser.java is the class that makes the httpRequest:
public JSONArray getJSONFromUrl(String url, List params) {
// Making HTTP request
try {
// defaultHttpClient
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
// json = EntityUtils.toString(httpEntity);
// HttpEntity httpEntity2 = httpEntity;
json = EntityUtils.toString(httpEntity);
// is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//then makes the JSON manipulation
As long as JSONParser and userFunctions aren’t activities, I couldn’t use ProgressDialogs inside them (Can’t get the application context). All server requests are made in JSONParser.java, that’s why i’ve first tried to put the ProgressDialog/ProgressBar there.
The closest that i’ve reached was using this code in JogarActivity (It shows the ProgressDialog, but after getting server’s response. And if I use dismiss, it doesn’t even appears)
final ProgressDialog loader = new ProgressDialog(JogarActivity.this);
loader.show();
//...the if-else code i've pasted above
loader.dismiss();
Even using runOnUiThread it doesn’t works! I’m getting without options…
Thanks for all help.
this worked: