Android 如何使用AsyncTask显示ProgressBar和加载Rss
我使用AsyncTask在加载Rss新闻时显示进度条。因为这是我第一次使用asyncTask,我不知道我的方法是否正确,所以你能告诉我它对你来说是否合适吗?它可以工作,但我只想被使用!谢谢Android 如何使用AsyncTask显示ProgressBar和加载Rss,android,rss,progress-bar,android-asynctask,Android,Rss,Progress Bar,Android Asynctask,我使用AsyncTask在加载Rss新闻时显示进度条。因为这是我第一次使用asyncTask,我不知道我的方法是否正确,所以你能告诉我它对你来说是否合适吗?它可以工作,但我只想被使用!谢谢 public class BackgroundAsyncTask_nea extends AsyncTask<Void, Void, Void> { private ProgressDialog dialog; int myProgress;
public class BackgroundAsyncTask_nea extends
AsyncTask<Void, Void, Void> {
private ProgressDialog dialog;
int myProgress;
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
displayRss();
dialog.dismiss();
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
dialog = ProgressDialog.show(nea.this, "", "Loading. Please wait...", true);
myProgress = 0;
}
@Override
protected void onProgressUpdate(Void... values) {
// TODO Auto-generated method stub
//super.onProgressUpdate(values);
}
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
loadFeed();
return null;
}
}
displayRss()强>
private void loadFeed(){
try{
BaseFeedParser parser = new BaseFeedParser();
messages = parser.parse();
}
catch (Throwable t){
Log.e("OSFP.News",t.getMessage(),t);
finish();
}
}
private void displayRss(){
ArrayList<HashMap<String, String>> List_nea = new ArrayList<HashMap<String, String>>(messages.size());
for (Message msg : messages){
des.add(msg.getDescription());// keimeno
text.add(msg.getTitle());// titlos
url.add(msg.getLink());// link
imgl.add(msg.getImgLink());
HashMap<String, String> map = new HashMap<String, String>();
map.put("name", msg.getTitle());
map.put("date", msg.getDate());
List_nea.add(map);
ListAdapter mSchedule = new SimpleAdapter(this, List_nea, R.layout.row,
new String[] {"name", "date"}, new int[] {R.id.TextView01, R.id.TextView02});
this.setListAdapter(mSchedule);
}
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(400);
set.addAnimation(animation);
animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, -1.0f,Animation.RELATIVE_TO_SELF, 0.0f
);
animation.setDuration(400);
set.addAnimation(animation);
LayoutAnimationController controller = new LayoutAnimationController(set, 0.5f);
ListView listViewn = getListView();
listViewn.setLayoutAnimation(controller);
}
private void displayRss(){
ArrayList_nea=新的ArrayList(messages.size());
用于(消息消息:消息){
des.add(msg.getDescription());//keimeno
text.add(msg.getTitle());//titlos
url.add(msg.getLink());//link
add(msg.getImgLink());
HashMap=newHashMap();
put(“name”,msg.getTitle());
map.put(“date”,msg.getDate());
列表添加(地图);
ListAdapter msSchedule=new SimpleAdapter(此,列表,R.layout.row,
新字符串[]{“name”,“date”},新int[]{R.id.TextView01,R.id.TextView02});
此.setListAdapter(msSchedule);
}
AnimationSet set=新的AnimationSet(真);
动画=新AlphaAnimation(0.0f,1.0f);
动画。设置持续时间(400);
set.addAnimation(动画);
动画=新的TranslateAnimation(
Animation.RELATIVE_TO_SELF,0.0f,Animation.RELATIVE_TO_SELF,0.0f,
Animation.RELATIVE_TO_SELF,-1.0f,Animation.RELATIVE_TO_SELF,0.0f
);
动画。设置持续时间(400);
set.addAnimation(动画);
LayoutImationController=新的LayoutImationController(设置,0.5f);
ListView listViewn=getListView();
listViewn.SetLayoutImation(控制器);
}
要更新进度条,需要调用函数。您在代码中的任何地方都没有这样做。如果我是您,我会将AsyncTask的引用向下传递到loadFeed函数中。大概是这样的:
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
loadFeed(this);
return null;
}
private void loadFeed(AsyncTask<Void, Void, Void> asyncTask){
// make calls to publishProgress in here.
try{
BaseFeedParser parser = new BaseFeedParser();
messages = parser.parse();
}
catch (Throwable t){
Log.e("OSFP.News",t.getMessage(),t);
finish();
}
}
对。
这是正确的工作流,在UI线程上执行之前显示ProgressDialog(也可以在此类之外执行,但这种方式似乎更好),在doInBackground方法上执行工作,如果需要,使用publishProgress()更新ProgressDialog,然后关闭ProgressDialog并在PostExecute上显示结果(),在您的UI线程中。在loadFeed解析器中,您可以使用AsyncTask的引用和方法
publishProgress
将进度更新发送到onProgressUpdate
,在那里您可以更新进度条。更新了我的答案。至于在没有internet连接时应用程序崩溃的问题,请使用toastn loadfeed,这些是不同的问题。为他们提出新的问题。但在你这样做之前,先尝试做一些研究(例如谷歌)。可能重复
protected void onProgressUpdate(Integer... progress) {
dialog.setProgress(progress[0]);
}