2010-12-27 4 views
60

httpサーバからrssフィードをロードしているときにカスタムprogressdialogを表示しようとしていますが、ハード・サーチを行っても何も助けてくれませんでした。私が知っている唯一のことは、私はこのAsyncTaskに渡すパラメータについて混乱しています。 ここに私の活動です:progressDialog in AsyncTask

import java.util.ArrayList; 
import java.util.List; 

import com.cyberesa.info.BaseFeedParser; 
import com.cyberesa.info.Message; 
import com.cyberesa.info.MessageListAdapter; 
import com.cyberesa.info.R; 

import android.app.ListActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.TextView; 

public class Soirees extends ListActivity { 
    private List<Message> messages; 
    private TextView tvSorties; 
    private MyProgressDialog dialog; 
    @Override 
    public void onCreate(Bundle icicle) { 

     super.onCreate(icicle); 

     setContentView(R.layout.sorties); 

     tvSorties=(TextView)findViewById(R.id.TVTitle); 
     tvSorties.setText("Programme des soirées"); 

     loadFeed(); 

    } 

    private void loadFeed(){ 
     try{ 
      BaseFeedParser parser = new BaseFeedParser(); 
      messages = parser.parse(); 
      List<Message> titles = new ArrayList<Message>(messages.size()); 
      for (Message msg : messages){ 
       titles.add(msg); 
      } 
      MessageListAdapter adapter = new MessageListAdapter(this,titles); 
      this.setListAdapter(adapter); 
      adapter.notifyDataSetChanged(); 

     } catch (Throwable t){ 
      Log.e("ImageLoader",t.getMessage(),t); 
     } 
    } 

} 

はあなたが私はこれにAsyncTaskを追加助けてくださいことはできますか?

public class Soirees extends ListActivity { 
    private List<Message> messages; 
    private TextView tvSorties; 

    //private MyProgressDialog dialog; 
    @Override 
    public void onCreate(Bundle icicle) { 

     super.onCreate(icicle); 

     setContentView(R.layout.sorties); 

     tvSorties=(TextView)findViewById(R.id.TVTitle); 
     tvSorties.setText("Programme des soirées"); 

     new ProgressTask(Soirees.this).execute(); 


    } 


    private class ProgressTask extends AsyncTask<String, Void, Boolean> { 
     private ProgressDialog dialog; 
     List<Message> titles; 
     private ListActivity activity; 
     //private List<Message> messages; 
     public ProgressTask(ListActivity activity) { 
      this.activity = activity; 
      context = activity; 
      dialog = new ProgressDialog(context); 
     } 



     /** progress dialog to show user that the backup is processing. */ 

     /** application context. */ 
     private Context context; 

     protected void onPreExecute() { 
      this.dialog.setMessage("Progress start"); 
      this.dialog.show(); 
     } 

      @Override 
     protected void onPostExecute(final Boolean success) { 
       List<Message> titles = new ArrayList<Message>(messages.size()); 
       for (Message msg : messages){ 
        titles.add(msg); 
       } 
       MessageListAdapter adapter = new MessageListAdapter(activity, titles); 
       activity.setListAdapter(adapter); 
       adapter.notifyDataSetChanged(); 

       if (dialog.isShowing()) { 
       dialog.dismiss(); 
      } 

      if (success) { 
       Toast.makeText(context, "OK", Toast.LENGTH_LONG).show(); 
      } else { 
       Toast.makeText(context, "Error", Toast.LENGTH_LONG).show(); 
      } 
     } 

     protected Boolean doInBackground(final String... args) { 
      try{  
       BaseFeedParser parser = new BaseFeedParser(); 
       messages = parser.parse(); 


       return true; 
      } catch (Exception e){ 
       Log.e("tag", "error", e); 
       return false; 
      } 
      } 


    } 

} 

@Vladimir、THXあなたのコードは非常に有用だった:

は、固定のコードがあるのでonPostExecuteにビュー修飾子を移動することによって、固定 Houssem

答えて

41

、ありがとうございました。

+3

解決策としてok、upvoteまたはマーク=) –

+0

内部でハンドラを作成できませんLooper.prepare()を呼び出していないスレッドこのエラーが発生しています。これを解決する方法。 – Noby

+0

もう1つこれを追加する必要があると思います。 AsyncTaskにグローバルプライベートオブジェクトを作成する必要があり、アクティビティが破棄されたときにAsyncTaskをキャンセルする必要があります。あなたのケースでは、AsyncTaskが完了する前にユーザーが戻るボタンを押すと、アプリケーションがおそらくクラッシュします。 – tasomaniac

123
/** 
* this class performs all the work, shows dialog before the work and dismiss it after 
*/ 
public class ProgressTask extends AsyncTask<String, Void, Boolean> { 

    public ProgressTask(ListActivity activity) { 
     this.activity = activity; 
     dialog = new ProgressDialog(activity); 
    } 

    /** progress dialog to show user that the backup is processing. */ 
    private ProgressDialog dialog; 
    /** application context. */ 
    private ListActivity activity; 

    protected void onPreExecute() { 
     this.dialog.setMessage("Progress start"); 
     this.dialog.show(); 
    } 

     @Override 
    protected void onPostExecute(final Boolean success) { 
     if (dialog.isShowing()) { 
      dialog.dismiss(); 
     } 


     MessageListAdapter adapter = new MessageListAdapter(activity, titles); 
     setListAdapter(adapter); 
     adapter.notifyDataSetChanged(); 


     if (success) { 
      Toast.makeText(context, "OK", Toast.LENGTH_LONG).show(); 
     } else { 
      Toast.makeText(context, "Error", Toast.LENGTH_LONG).show(); 
     } 
    } 

    protected Boolean doInBackground(final String... args) { 
     try{  
      BaseFeedParser parser = new BaseFeedParser(); 
      messages = parser.parse(); 
      List<Message> titles = new ArrayList<Message>(messages.size()); 
      for (Message msg : messages){ 
       titles.add(msg); 
      } 
      activity.setMessages(titles); 
      return true; 
     } catch (Exception e) 
      Log.e("tag", "error", e); 
      return false; 
     } 
    } 
} 

public class Soirees extends ListActivity { 
    private List<Message> messages; 
    private TextView tvSorties; 
    private MyProgressDialog dialog; 
    @Override 
    public void onCreate(Bundle icicle) { 

     super.onCreate(icicle); 

     setContentView(R.layout.sorties); 

     tvSorties=(TextView)findViewById(R.id.TVTitle); 
     tvSorties.setText("Programme des soirées"); 

     // just call here the task 
     AsyncTask task = new ProgressTask(this).execute(); 
    } 

    public void setMessages(List<Message> msgs) { 
     messages = msgs; 
    } 

} 
+0

ありがとう、私はこれを試している、と私は尋ねたように、どのparams私はここで渡す? MessageListAdapterアダプタ= new MessageListAdapter(this、titles)という行がエラー – Houssem

+0

をスローするため、ブール値doInBackground(final String ... args)が保護されます。あなたはリストアダプタを設定する必要があります。 –

+0

申し訳ありませんVlad、私は完全に混乱しています:S(初心者の場合) – Houssem

3

数日前、私はこの問題の非常に素晴らしい解決策を見つけました。それについてはhereを読んでください。 2つの言葉で、MikeはProgressDialogとAsyncTaskを仲介するAsyncTaskManagerを作成しました。このソリューションを使用するのはとても簡単です。あなたのプロジェクトにいくつかのインターフェイスといくつかのクラスを含め、あなたのアクティビティに単純なコードを書いて、新しいAsyncTaskをBaseTaskからネストしてください。また、いくつかの役に立つヒントがあるので、コメントを読むことをアドバイスします。

8

AsyncTaskは非常に役に立ちます!

class QueryBibleDetail extends AsyncTask<Integer, Integer, String>{ 
     private Activity activity; 
     private ProgressDialog dialog; 
     private Context context; 

     public QueryBibleDetail(Activity activity){ 
      this.activity = activity; 
      this.context = activity; 
      this.dialog = new ProgressDialog(activity); 
      this.dialog.setTitle("查询经文"); 
      this.dialog.setMessage("正在查询:"+tome+chapterID+":"+sectionFromID+"-"+sectionToID); 
      if(!this.dialog.isShowing()){ 
       this.dialog.show(); 
      } 
     } 

     @Override 
     protected String doInBackground(Integer... params) { 
      Log.d(TAG,"经文doInBackground"); 
      publishProgress(params[0]); 

      if(sectionFromID > sectionToID){ 
       return ""; 
      } 

      String queryBible = "action=query_bible&article="+chapterID+"&id="+tomeID+"&verse_start="+sectionFromID+"&verse_stop="+sectionToID+""; 
      try{ 
       String bible = (Json.getRequest(HOST+queryBible)).trim(); 
       bible = android.text.Html.fromHtml(bible).toString(); 
       return bible; 
      }catch(Exception e){ 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(String bible){ 
      Log.d(TAG,"经文onPostExecute"); 
      TextView bibleBox = (TextView) findViewById(R.id.bibleBox); 
      bibleBox.setText(bible); 
      this.dialog.dismiss(); 
     } 
    } 
0

この質問が尋ねられてから(そして誰かが回答を投稿して以来)数年が経ちました。それ以来、ProgressDialogは、APIレベルOでは、Android's official documentation.に従って廃止されました。ドキュメント作成者の提案によれば、ProgressDialogの代わりにan inline progress barを使用することを検討することがあります。

-1

この質問はすでに回答済みであり、ここでの回答のほとんどは正しいものの、設定の変更に関する大きな問題は解決しません。より良い方法で非同期タスクを作成したい場合は、この記事https://androidresearch.wordpress.com/2013/05/10/dealing-with-asynctask-and-screen-orientation/をご覧ください。

+0

@これまでに投票しました。この答えが間違っていることを教えてください。それは私が読んだ中級開発者と共有したいと思う良い記事の一つです。 – Prakash

関連する問題