ProgressDialog 没有显示在我的 ListActivity 的 onListItemClick 方法中
我有 ListActivity
和 BaseAdapter
。每次用户单击一个项目时,我都会清除适配器并重新加载新内容(这是某种树逻辑)。
有时,适配器在 onListItemClick()
中获取新的项目列表需要几秒钟的时间。我想在输入 onListItemClick()
时显示一些进度条并在完成时隐藏它。
为什么我的示例中没有显示进度对话框?我怀疑(正如我读到的)问题是因为“计算”没有在后台线程中运行?如果是这样,有人可以根据我的代码给我一个例子吗?
这是代码:
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
catalogAdapter.previousUrl = catalogAdapter.currentUrl;
RRmlMappingServicesCatalogItem item = catalogAdapter.getItem(position);
catalogAdapter.currentUrl = item.href;
//catalogAdapter.mappingServicesCatalog.loadFromRml(new RIoConnectionManagerImpl(this.getApplicationContext()), item.href, null, 120000, 500000);
loadCatalogFromRml(this.getApplicationContext(), item.href);
catalogAdapter.clearData();
if ((catalogAdapter.mappingServicesCatalog.getFlatItems()).length == 0)
{
catalogAdapter.mappingServiceContent = new RRmlMappingServiceContent ();
this.loadContentFromRml(this.getApplicationContext(), item.href);
//catalogAdapter.mappingServiceContent.loadFromRml(new RIoConnectionManagerImpl(this.getApplicationContext()),item.href, null, 120000, 500000);
settings = (GlobalSettings) getApplication();
settings.setMappingServiceContent(catalogAdapter.mappingServiceContent);
if (catalogAdapter.mappingServiceContent.authentication != null)
{
startActivity(new Intent(CatalogActivity.this,
LoginActivity.class));
}
else
{
startActivity(new Intent(CatalogActivity.this,
MapActivity.class));
}
}
else
{
for (RRmlMappingServicesCatalogItem s : catalogAdapter.mappingServicesCatalog.getFlatItems()) {
catalogAdapter.addItem(s);
}
setListAdapter(catalogAdapter);
}
}
public void loadContentFromRml(Context context, final String url)
{
final Context myContext = context;
final String myUrl = url;
progressDialog = ProgressDialog.show(CatalogActivity.this, "",
"Loading content...", true);
new Runnable()
{
public void run()
{
catalogAdapter.mappingServiceContent.loadFromRml(new RIoConnectionManagerImpl(myContext),myUrl, null, 120000, 500000);
progressDialog.dismiss();
}
}.run();
}
public void loadCatalogFromRml(Context context, String url)
{
final Context myContext = context;
final String myUrl = url;
progressDialog = ProgressDialog.show(CatalogActivity.this, "",
"Loading catalog...", true);
new Runnable()
{
public void run()
{
catalogAdapter.mappingServicesCatalog.loadFromRml(new RIoConnectionManagerImpl(myContext), myUrl, null, 120000, 500000);
progressDialog.dismiss();
}
}.run();
}
I have ListActivity
with BaseAdapter
. Each time an user clicks an item I clear the adapter and reload new content (it is some sort of tree logic).
Sometimes it takes several seconds that adapter gets new list of items when in onListItemClick()
. I would like to show some progress bar when entering onListItemClick()
and hide it when complete.
Why is the progress dialog not showing in my example? I suspect (as I read) the problem would be because the "calculation" is not running in a background thread? If so, can some one please give me an example based on my code?
Here is the code:
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
catalogAdapter.previousUrl = catalogAdapter.currentUrl;
RRmlMappingServicesCatalogItem item = catalogAdapter.getItem(position);
catalogAdapter.currentUrl = item.href;
//catalogAdapter.mappingServicesCatalog.loadFromRml(new RIoConnectionManagerImpl(this.getApplicationContext()), item.href, null, 120000, 500000);
loadCatalogFromRml(this.getApplicationContext(), item.href);
catalogAdapter.clearData();
if ((catalogAdapter.mappingServicesCatalog.getFlatItems()).length == 0)
{
catalogAdapter.mappingServiceContent = new RRmlMappingServiceContent ();
this.loadContentFromRml(this.getApplicationContext(), item.href);
//catalogAdapter.mappingServiceContent.loadFromRml(new RIoConnectionManagerImpl(this.getApplicationContext()),item.href, null, 120000, 500000);
settings = (GlobalSettings) getApplication();
settings.setMappingServiceContent(catalogAdapter.mappingServiceContent);
if (catalogAdapter.mappingServiceContent.authentication != null)
{
startActivity(new Intent(CatalogActivity.this,
LoginActivity.class));
}
else
{
startActivity(new Intent(CatalogActivity.this,
MapActivity.class));
}
}
else
{
for (RRmlMappingServicesCatalogItem s : catalogAdapter.mappingServicesCatalog.getFlatItems()) {
catalogAdapter.addItem(s);
}
setListAdapter(catalogAdapter);
}
}
public void loadContentFromRml(Context context, final String url)
{
final Context myContext = context;
final String myUrl = url;
progressDialog = ProgressDialog.show(CatalogActivity.this, "",
"Loading content...", true);
new Runnable()
{
public void run()
{
catalogAdapter.mappingServiceContent.loadFromRml(new RIoConnectionManagerImpl(myContext),myUrl, null, 120000, 500000);
progressDialog.dismiss();
}
}.run();
}
public void loadCatalogFromRml(Context context, String url)
{
final Context myContext = context;
final String myUrl = url;
progressDialog = ProgressDialog.show(CatalogActivity.this, "",
"Loading catalog...", true);
new Runnable()
{
public void run()
{
catalogAdapter.mappingServicesCatalog.loadFromRml(new RIoConnectionManagerImpl(myContext), myUrl, null, 120000, 500000);
progressDialog.dismiss();
}
}.run();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我怀疑您的代码中最繁忙的行是下一个:
为了让对话框显示您需要从调用
ProgressDialog.show(...)
的函数返回。这是因为此调用的实际处理是在消息循环中完成的(此循环首先调用您的函数)。因此,在您的代码中,您创建了一个对话框并将其标记为要显示,但从未真正给它机会通过在同一函数末尾调用.dismiss()
来显示自己。您应该使用
AsyncTask
。首先,您需要将 UI 处理代码与loadFromRml
功能分开。将所有耗时的事情放在AsyncTask
的doInBackground
函数中,所有UI都应该在onPreExecute
或onPostExecute
中。您可以在
onListItemClick
函数中显示进度对话框,并在AsyncTask.onPostExecute
函数中关闭它。或者,作为替代方案,在AsyncTask.onPreExecute
中显示进度对话框,并在AsyncTask.onPostExecute
中隐藏它。有很多关于
AsyncTask
的示例,其中之一位于 AsycnTask Android 文档。另一篇有用的文章可以在 Android 开发者资源中找到:Painless Threading。I suspect the busiest line of your code is next:
In order for dialog to show you need to return from function that called
ProgressDialog.show(...)
. That's because an actual processing of this call is done in message loop (this loop called your function in the first place). So in your code you're creating a dialog and marking it to be shown, but never really give it a chance to show itself by calling.dismiss()
at the end of the same function.You should use
AsyncTask
. First you need to separate your UI handling code from yourloadFromRml
functionality. Put all time consuming thing indoInBackground
function ofAsyncTask
, all UI should be either inonPreExecute
oronPostExecute
.You could show progress dialog in
onListItemClick
function and dismiss it inAsyncTask.onPostExecute
function. Or, as an alternative, show progress dialog inAsyncTask.onPreExecute
and hide it inAsyncTask.onPostExecute
.There are a lot of examples with
AsyncTask
, one of which is located in AsycnTask android docs. Another useful article can be found in android developer resources: Painless Threading.我在我的应用程序中也遇到了相同类型的情况,但我已经使用给定的代码解决了它。
我已经创建了这样的进度对话框,
当进度对话框关闭时调用打击函数
我希望它有帮助,
I have also face same type of condition in my application but i have resolved it using the given code the code it is.
I have create my progress dialog like this
when progress dialog dismiss the blow function is called
I hope it is help,
如果您发现它有用,请不要忘记将其标记为答案
最好的祝福,
〜Anup
If you find it useful please don't forget to mark it as an answer
Best Regards,
~Anup