我试图在应用程序对在线数据库进行查询时向用户显示加载图标。我试过使用AnimationDrawable(我放弃了,因为不需要自定义图标),ProgressDialog和ProgressBar。 ProgressBar看起来最合适,因为我不想要消息,只是一个旋转的图标。但我甚至无法让ProgressBar出现在屏幕上,无论我在哪里调用它。 我有ProgressDialog出现在屏幕上,但它只出现在服务器的响应后,如果我使用dismiss()或cancel()它甚至不出现。如何使用ProgressBar在等待来自服务器的httpResponse时显示微调器
我已经使用AsyncTasks或线程成功。
在应用程序中,有一个类JogarActivity.java试图显示选项列表。它接收一些参数,如用户ID,并调用UserFunctions:
public void onCreate(Bundled 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();
}
}
下面是userFunctions的getJogar功能: 公共JSONArray getJogar(字符串categoria,字符串usuarioId){ 列表PARAMS =新的ArrayList();
params.add(new BasicNameValuePair("categoria", categoria));
params.add(new BasicNameValuePair("idUsuario", usuarioId));
JSONArray json = jsonParser.getJSONFromUrl(perguntaURL, params);
return json;
}
JSONParser.java是使的HttpRequest类: 公共JSONArray getJSONFromUrl(字符串URL,列表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
只要JSONParser和userFunctions都没有活动,我不能在他们内部不使用ProgressDialogs(无法获取应用程序上下文)。所有服务器请求都是在JSONParser.java中完成的,这就是为什么我第一次尝试将ProgressDialog/ProgressBar放在那里。
,我已经用在JogarActivity(它显示ProgressDialog,但得到服务器的响应后,如果我用辞退,甚至不出现的话)
final ProgressDialog loader = new ProgressDialog(JogarActivity.this);
loader.show();
//...the if-else code i've pasted above
loader.dismiss();
即使该代码达到最接近使用runOnUiThread它不起作用!我没有选择...
感谢您的一切帮助。
试图实现的AsyncTask。检查真实的例子[这里](http://www.technotalkative.com/loading-remote-images/)并尝试以相同的方式实施。 –
为此使用一个'AsyncTask'。在'onPreExecute'中执行'ProgressDialog.show',并在'onPostExecute'或异常时关闭对话框。 – curioustechizen
我不能使用AsyncTask,因为它没有扩展Activity,因此我不能使用getApplicationContext()来实例化一个新的ProgressDialog/ProgressBar。 –