问题描述
|
我遇到了以下代码问题:
public void onCreate(Bundle savedInstanceState) {
MyDialog = ProgressDialog.show(this,\"Nalagam kanale\",\"Prosimo počakaj ... \",true);
MyDialog.show();
... }
实际上应该从哪个对话框开始...但是问题是,在加载所有内容时都会显示对话框...
我该如何解决?
实际代码
package com.TVSpored;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
public class Currently extends Activity{
static final int PROGRESS_DIALOG = 0;
private ArrayList<CurrentlyItem> currentItems;
private CurrentAdapter aa;
private ListView currentListView;
private JSONArray CurrentShows;
private Communicator CommunicatorEPG;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.layout_currently);
CommunicatorEPG = new Communicator();
currentItems = new ArrayList<CurrentlyItem>();
if(currentItems == null)
int resID = R.layout.current_item;
aa = new CurrentAdapter(this,resID,currentItems);
currentListView = (ListView)findViewById(R.id.currentListView);
try {
currentListView.setAdapter(aa);
} catch (Exception e) {
Log.d(\" * Napaka\",e.toString());
}
try {
populateCurrent();
} catch (JSONException e) {
// Todo Auto-generated catch block
e.printstacktrace();
}
}
public void populateCurrent() throws JSONException
{
CurrentShows = CommunicatorEPG.getCurrentShows(0);
for (int i = 0; i < CurrentShows.length(); i++)
{
JSONObject jsonObject = CurrentShows.getJSONObject(i);
String start = jsonObject.getString(\"1\");
Integer duration = jsonObject.getInt(\"2\");
String title = jsonObject.getString(\"3\");
String epg_channel = jsonObject.getString(\"4\");
String channel_name = jsonObject.getString(\"5\");
CurrentlyItem newItem = new CurrentlyItem(1,2,3,4,5);
currentItems.add(i,newItem);
}
}
}
这是实际的代码...我想在AsyncTask
中执行populateCurrent();
,同时我想显示一个加载屏幕...已经尝试了几个小时,但没有实际成功...我已经成功显示了加载屏幕,并且温槽JSONArray
,但无法更新列表视图...
感谢你的支持!
解决方法
您可以等待设置活动的内容,直到完成进度对话框。
更新:
这将在async-task中运行您的命令:
new AsyncTask<Void,Void,Void> {
protected Long doInBackground(Void... voids) {
populateCurrent();
}
}.execute()
但是,那么您可能必须确保再次在GUI线程中更新列表,并以某种方式告知适配器列表已更新(因为您已将该列表提供给适配器):
runOnUiThread(new Runnable() {
public void run() {
currentItems.add(i,newItem);
aa.notifyDataSetChanged();
}
}
最好是完全创建一个新列表并设置视图以查看该列表。
,预期的行为...
显示对话框是UI线程的典型任务,但是在完成onCreate方法之前,UI线程无法自由执行对话框的创建...
两种解决方案:在单独的线程中创建对话框或在单独的线程中执行您的长任务。
这里有一些亮点:
http://developer.android.com/guide/topics/ui/dialogs.html