2011-04-13 5 views
0

경험하고 구현하는 방법 :ProgresDialog은 [안드로이드] 내가 다음 코드 한 문제를

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 표시했지만 목록보기를 업데이트 할 수 없습니다 ...

Thanks for support!

+0

'super.onCreate (savedInstanceState);가 누락 된 것 같아요 –

+0

어떤 행동을 기대합니까? 부모 활동이 표시되기 전에 대화 상자가 표시됩니까? – rajath

+0

그래 부모 작업 전에 표시되고 싶습니다 ... –

답변

0

당신은 당신이 진행 대화 상자가 완료 될 때까지 활동의 내용을 설정하기 위해 기다릴 수 있습니다.

업데이트 : 그때는 아마 다시 GUI 스레드의 목록을 업데이트 할 수 있는지 확인해야합니다, 그러나

new AsyncTask<Void, Void, Void> { 
    protected Long doInBackground(Void... voids) { 
    populateCurrent(); 
    } 
}.execute() 

을 일부에서 :

이 비동기 작업에서 명령을 실행하는 것입니다 (목록을 어댑터에 제공 했으므로) 어댑터에 목록이 업데이트되었음을 ​​알립니다.

runOnUiThread(new Runnable() { 
    public void run() { 
    currentItems.add(i, newItem); 
    aa.notifyDataSetChanged(); 
    } 
} 

새 목록을 완전히 만들고보기를 설정하는 것이 가장 좋습니다.

+0

안녕하세요 ... 실제 문제를 보여주기 위해 질문 섹션의 코드를 업데이트 한 이유가 무엇이든 진행하지 못했습니다. –

+0

코드를 기반으로합니다. 목록에 항목을 추가 한 후에 목록보기 어댑터를 확인해야합니다 (목록을 채우기 전에 어댑터를 작성하기 때문에). 또한; catch 블록에서 끝나지 않을 것이라고 확신합니까? 'adb logcat'을 사용하여 어떤 메시지를 볼 수 있습니까? – thoredge

+0

아니요이 코드에는 문제가 없습니다 ...이 훌륭한 작업입니다! 나는 AsyncTask에서 Loading을 추가하고 populateCurrent()를 실행하려고한다. 그러나 그것을하는 방법을 모른다. –

1

예상되는 동작 ...

는 ... 대화 상자가 UI 스레드의 일반적인 작업입니다,하지만 당신은에서 onCreate 방법을 완료 할 때까지, UI 스레드가 대화 상자 생성을 실행할 자유가 아니다보기

두 가지 해결책 : 별도의 스레드에서 대화 상자를 만들거나 별도의 스레드에서 긴 작업을 실행하십시오. 여기

일부 하이라이트 : http://developer.android.com/guide/topics/ui/dialogs.html

+0

시도하고 있지만, 나는 항상 'Looper.prepare()'를 호출하지 않은 스레드 내부에서 처리기를 만들 수 없습니다. ... ListView에 내용을 추가하고 있습니다. ... –

+0

실제 코드를 추가했는데 감사 할 수 있다면 도와 드리겠습니다. –

관련 문제