progressDialog中的AsyncTask [英] progressDialog in AsyncTask
问题描述
我想显示自定义progressdialog在加载从HTTP服务器RSS源,我做了一个艰苦的搜索,但没有任何帮助我这样做,我唯一知道的是,该解决方案应该使用AsyncTask的,但我米混淆有关PARAMS传递给这个AsyncTask的。 这是我的活动:
I'm trying to display a custom progressdialog while loading rss feed from http server, I made a hard search, but nothing helped me to do this, the only thing i know is that the solution should use AsyncTask, but i'm confusing about the params to pass to this AsyncTask. Here's my activity :
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这样做呢?
Can you please help me add AsyncTask to this ?
感谢您, Houssem
Thank you, Houssem
推荐答案
通过移动视图修饰符onPostExecute因此固定code修正为:
Fixed by moving the view modifiers to onPostExecute so the fixed code is :
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你的code是非常有益的。
@Vladimir, thx your code was very helpful.
这篇关于progressDialog中的AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!