2011-04-13 50 views
0

我遇到一个问题,我有以下代码:如何实现ProgresDialog [Android]产品

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); 
    } 
} 
} 

这是实际的代码...我想这样做在AsyncTaskpopulateCurrent();,同时我想加载屏幕中显示...一直试图几个小时,但没有实际成功...我已经成功显示加载屏幕和文谷JSONArray,但无法更新列表视图...

感谢您的支持!

+0

我想你缺少'super.onCreate(savedInstanceState);' – 2011-04-13 08:26:23

+0

你期望什么行为?要显示父活动之前显示的对话框? – rajath 2011-04-13 08:31:30

+0

是的,我希望它显示在父活动之前... – 2011-04-13 08:46:14

回答

0

你可以等待活动的内容设置,直到你与进度对话框完成。

更新

这将在异步任务运行命令:

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(); 
    } 
} 

这很可能最好完全创建一个新列表并设置视图来查看该列表。

+0

嗨...我还没有设法做出任何进展,这就是为什么我更新问题部分中的代码来显示实际问题。 – 2011-04-13 12:10:50

+0

基于代码:在将项目添加到列表之后(因为您在填充列表之前创建适配器)之后,您需要注意列表视图适配器。也;你确定你没有在catch块中结束;你使用'adb logcat'来查看你得到的消息吗? – thoredge 2011-04-13 13:03:32

+0

不,没有问题不在这个代码中......这个工作很棒!我只是想在AsyncTask中加载并执行populateCurrent()..但不知道该怎么做... – 2011-04-13 13:15:56

1

预期的行为......

显示一个对话框UI线程的典型任务,但要等到完成onCreate方法,在UI线程不是随意执行对话框创建...

两种解决方案:在单独的线程中创建对话框或在单独的线程中执行长期任务。

这里的一些亮点: http://developer.android.com/guide/topics/ui/dialogs.html

+0

尝试它,但我总是收到'不能创建处理程序内部线程没有调用Looper.prepare()'...我将内容添加到ListView ... – 2011-04-13 10:07:14

+0

我已经添加了实际的代码,如果你能帮助,我会感激.. – 2011-04-13 12:11:20