gpt4 book ai didi

android - 如何使用AsyncTask显示ProgressBar和加载Rss

转载 作者:太空宇宙 更新时间:2023-11-03 10:29:27 25 4
gpt4 key购买 nike

我正在使用 AsyncTask 在加载 Rss 新闻时显示进度条。因为这是我第一次使用 asyncTask 我不知道我的方法是否正确,所以你能告诉我它对你来说是否合适?它正在工作,但我只想保持安静!谢谢

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;
}



}

loadFeed();

private void loadFeed(){
try{
BaseFeedParser parser = new BaseFeedParser();
messages = parser.parse();

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


finish();
}
}

displayRss();

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);

}

最佳答案

为了更新进度条,您需要调用 publishProgress()功能。您没有在代码中的任何地方这样做。如果我是你,我会将 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();
}
}

您还需要实现您尚未实现的 onProgressUpdate() 方法。这是您实际更新进度条值的地方。

     protected void onProgressUpdate(Integer... progress) {
dialog.setProgress(progress[0]);
}

关于android - 如何使用AsyncTask显示ProgressBar和加载Rss,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8297947/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com