我正在尝试从http服务器加载rss feed时显示自定义progressdialog,我进行了一次硬搜索,但没有任何帮助我这样做,我唯一知道的是解决方案应该使用AsyncTask,但是我混淆了传递给这个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吗?
谢谢你, Houssem
答案 0 :(得分:126)
/**
* 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;
}
}
答案 1 :(得分:43)
通过将视图修饰符移动到onPostExecute来修复,因此固定代码为:
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,你的代码非常有帮助。
答案 2 :(得分: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();
}
}
答案 3 :(得分:3)
几天前,我找到了一个非常好的解决方案。阅读它here。用两个词来说,Mike创建了一个AsyncTaskManager来调解ProgressDialog和AsyncTask。使用此解决方案非常容易。您只需要在项目中包含几个接口和几个类,并在您的活动中编写一些简单的代码并从BaseTask嵌套新的AsyncTask。我也建议你阅读评论,因为有一些有用的提示。
答案 4 :(得分:0)
自问这个问题以来已经过了几年(因为有人发布了回复)。从那时起,根据Android's official documentation.,在API级别O中不推荐使用ProgressDialog。因此,您可以考虑使用an inline progress bar而不是ProgressDialog,正如文档作者所建议的那样。
答案 5 :(得分:0)
包括参数在内的许多开发人员在开始编写AsyncTask时都遇到了困难。最大的原因是我们试图记住AsyncTask中使用的参数。关键是不要记住。如果您可以看到任务真正需要执行的操作,那么编写带有正确签名的AsyncTask就是小菜一碟。
AsyncTask是在后台线程中运行的后台任务。它需要一个输入,执行进度并给出输出。
即
AsyncTask<Input,Progress,Output>
只需弄清楚您的输入,进度和输出是什么,您就可以开始使用
。例如
doInbackground()
与AsyncTask
参数如何变化?
doInBackground()
和onPostExecute()
,onProgressUpdate()
的方式 有关?
如何用代码编写?
DownloadTask extends AsyncTask<String,Integer,String>{
@Override
public void onPreExecute(){
}
@Override
public String doInbackGround(String... params)
{
// Download code
int downloadPerc = // calculate that
publish(downloadPerc);
return "Download Success";
}
@Override
public void onPostExecute(String result)
{
super.onPostExecute(result);
}
@Override
public void onProgressUpdate(Integer... params)
{
// show in spinner, access UI elements
}
}
您将如何在“活动”中运行此任务?
new DownLoadTask().execute("Paradise.mp3");
答案 6 :(得分:-2)
这个问题已经得到解答,这里的大多数答案都是正确的,但它们并没有解决配置更改的一个主要问题。如果您想以更好的方式编写异步任务,请查看本文https://androidresearch.wordpress.com/2013/05/10/dealing-with-asynctask-and-screen-orientation/。