我有一个 ListActivity
这将启动另一个活动
基于列表选择。这第二个活动
需要加载从互联网上的数据还是比较可观的,因此有当用户点击一个项目,当活动显示。
这是一个问题,因为我现在有没有办法来指示他们点击正在被处理(甚至只是改变选中的列表项的颜色就足够的用户,但我不能找到一个好办法来做到这一点)。理想情况下,我会能够显示一个不确定的 ProgressDialog
,而第二个活动
加载。
我已经尝试了一些不同的方法可以实现,但似乎没有任何工作按要求。
我已经试过如下:
检索序列数据(而不是全部,但某些部分)在的AsyncTask
第一活动
和将它作为一个额外的第二个。这并没有真正的工作还有一个 ProgressDialog
我在在preExecute创建()
不显示立即(似乎在 doInBackground()
因某种原因做了处理延迟。)
下面是code为:
的AsyncTask<字符串,太虚,字符串>读=新的AsyncTask<字符串,太虚,字符串>(){
对话的进展情况;
@覆盖
在preExecute保护无效(){
进度= ProgressDialog.show(SearchActivity.this,
加载数据,请稍候...);
super.on preExecute();
}
@覆盖
保护字符串doInBackground(字符串... PARAMS){
DatasetReader读卡器=新DatasetReader();
reader.setFundID(PARAMS [0]);
reader.addDatsets(FundProfile.datasets);
reader.populate();
返回reader.toString();
}
@覆盖
保护无效onPostExecute(字符串结果){
super.onPostExecute(结果);
progress.dismiss();
}
};
read.execute(selectedItem.getUniqueID());
尝试 {
行动=新的意图(SearchActivity.this,FundProfile.class);
action.putExtra(数据,read.get());
}赶上(例外前){
ex.printStackTrace();
}
在第二个活动的的onCreate()
方法(这并不在所有的工作):
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setProgressBarVisibility(真);
下面是的onCreate()
方法第二种方法:
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
this.setTitleColor(Color.WHITE);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setProgressBarVisibility(真正的);
尝试 {
的setContentView(R.layout.fund_profile);
//初始化一些数据
setProgressBarVisibility(假);
}赶上(例外前){
FundProfile.this.finish();
}
}
解决方案
如果你有长期的经营中,你不应该这样做他们的onCreate在任何情况下,因为这将冻结UI(是否显示活动)。用户界面通过的onCreate设置将不会出现,直到后的onCreate调用完成对用户界面将停止响应。
看来你就可以开始你的第二个活动,并显示进度条(或 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
),然后启动一个AsyncTask的,这将是负责更新你的UI一旦数据被检索到。
I have a ListActivity
which launches another Activity
based on the list selection. This second Activity
needs to load a fair bit of data from the internet and as such there is a noticeable delay between when the user clicks on an item and when the Activity
displays.
This is a problem because I currently have no way to indicate to the user that their click is being processed (even just changing the colour of the selected list item would be sufficient but I can't find a good way to do that). Ideally I'd be able to display an indeterminate ProgressDialog
while the second Activity
is loading.
I've tried a few different approaches for this but nothing seems to work as desired.
I've tried the following:
Retrieving the serializable data (not all of it but some part) in anAsyncTask
in the first Activity
and passing it as an extra to the second. This didn't really work well as a ProgressDialog
I created in onPreExecute()
didn't display immediately (it seems delayed by the processing done in doInBackground()
for some reason.)
Here is the code for that:
AsyncTask<String, Void, String> read = new AsyncTask<String, Void, String>() {
Dialog progress;
@Override
protected void onPreExecute() {
progress = ProgressDialog.show(SearchActivity.this,
"Loading data", "Please wait...");
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
DatasetReader reader = new DatasetReader();
reader.setFundID(params[0]);
reader.addDatsets(FundProfile.datasets);
reader.populate();
return reader.toString();
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
progress.dismiss();
}
};
read.execute(selectedItem.getUniqueID());
try {
action = new Intent(SearchActivity.this, FundProfile.class);
action.putExtra("data", read.get());
} catch(Exception ex) {
ex.printStackTrace();
}
In the second Activity's onCreate()
method (this does not work at all):
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setProgressBarVisibility(true);
Here is the onCreate()
method for the second approach:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setTitleColor(Color.WHITE);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setProgressBarVisibility(true);
try {
setContentView(R.layout.fund_profile);
// init some data
setProgressBarVisibility(false);
} catch(Exception ex) {
FundProfile.this.finish();
}
}
解决方案
If you have long operations you should not be doing them in onCreate in any case as this will freeze the UI (whether or not the activity is displayed). The UI set by onCreate will not appear and the UI will be unresponsive until after the onCreate call finishes.
It seems you can start your second activity and display a progress bar (or requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
), then start an ASyncTask which will be responsible for updating your UI once data has been retrieved.
上一篇:错误启动的第一个测试应用程序:Android的包装问题?第一个、应用程序、错误、测试
下一篇:升级到AppCompat v22.1.0和现在的onkeydown和OnKeyUp当菜单键是pressed不会触发升级到、菜单、现在、AppCompat