繁体   English   中英

ProgressDialog Android AsyncTAsk

[英]ProgressDialog Android AsyncTAsk

我想在Detail类在后台旋转时按列表视图中的任何项目时显示一个ProgressDialog,我知道我必须使用asyncTask,但是我不知道如何,请问有什么可以帮助我的吗?

这是我去显示ProgressDialog的片段

public class FeedPlayerIndexFragment extends SherlockFragment{     
String url="http://www.toto.com/index.php?format=feed&type=rss";
ArrayList<Article> feeds ;
AndroidSaxFeedParser feedParser= new AndroidSaxFeedParser(url);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    feeds=feedParser.parse();
    View view=inflater.inflate(R.layout.main, container, false);
    CustomItemAdapter_News lfa = new   CustomItemAdapter_News(getActivity().getApplicationContext(), feeds);
    ((ListView)view.findViewById(R.id.listFeed)).setAdapter(lfa);

    ((ListView)view.findViewById(R.id.listFeed)).setOnItemClickListener(new  OnItemClickListener() {
        public void onItemClick(AdapterView<?> arg0, View v, int  position,long id) {
            Bundle b = new Bundle();
            b.putInt("position", position);
            b.putString("url", url);
            Intent intent=new   Intent(getActivity().getApplicationContext(), DetailActivity.class);
            intent.putExtras(b);
            startActivity(intent);
        }
    });
    return view;
}
} 

这是我的详细信息活动,应该打开背景

public class DetailActivity extends Activity{
String descriptionFinale = null,imageFinale = null,url;
int position;
TextView description_,titre,date;
ImageView image_;
ArrayList<Article> feeds ;

WebView contentWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.detail);
    Bundle b = getIntent().getExtras();
    position=b.getInt("position");
    url=b.getString("url");
    AndroidSaxFeedParser feedParser= new AndroidSaxFeedParser(url);
    feeds=feedParser.parse();


    date=(TextView) findViewById(R.id.date);
    contentWebView= (WebView)findViewById(R.id.contentWebView);
    titre=(TextView) findViewById(R.id.titre);

    date.setText(feeds.get(position).getTitle());
    GetDescriptionWebView.getDescriptionImage(position, feeds,contentWebView);
    titre.setText("Postulé le: "+GetDateFormat.getDate(position, feeds));

}

}

您可以尝试此代码

public class DetailActivity extends Activity
{
    String descriptionFinale = null,imageFinale = null,url;
    int position;
TextView description_,titre,date;
ImageView image_;
ArrayList<Article> feeds ;

WebView contentWebView;


ProgressDialog dialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.detail);

    feeds=new Arraylist<Article>;
    contentWebView= (WebView)findViewById(R.id.contentWebView);
    date=(TextView) findViewById(R.id.date);
    titre=(TextView) findViewById(R.id.titre);
    Bundle b = getIntent().getExtras();
    position=b.getInt("position");
    url=b.getString("url");
    AndroidSaxFeedParser feedParser= new AndroidSaxFeedParser(url);


    new MyFetchTask().execute();



}

public class MyFetchTask extends AsyncTask<Object, Object, Object>
{

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();

        dialog=ProgressDialog.show(DetailActivity.this, "", "Loading...");

    }


    @Override
    protected Object doInBackground(Object... params) {
        // TODO Auto-generated method stub

        feeds=feedParser.parse();

        return null;
    }

    @Override
    protected void onPostExecute(Object result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);

        dialod.dismiss();
        date.setText(feeds.get(position).getTitle());
        GetDescriptionWebView.getDescriptionImage(position, feeds,contentWebView);
        titre.setText("Postulé le: "+GetDateFormat.getDate(position, feeds));

    }

}

}

这是asynctask的示例,希望对您有所帮助

  class NewCouponsTask extends AsyncTask<String, Void, ArrayList<NewCoupons>>

 {
NewCouponsScreen parentActivity;

public NewCouponsTask(NewCouponsScreen parent) {
    // TODO Auto-generated constructor stub
    parentActivity = parent;
}

@Override
protected void onPreExecute() {
    // TODO Auto-generated method stub
    super.onPreExecute();
}
@Override
protected ArrayList<NewCoupons> doInBackground(String... params) {

    URL url=null;
    ArrayList<NewCoupons> couponslist=null;
    try {
        url = new URL(ApplicationLinks.newCouponsLink);

     InputSource is = new InputSource(url.openStream());
     is.setEncoding("UTF-8");
    SaxFeedParser saxParser = new SaxFeedParser(ApplicationLinks.newCouponsLink);
    Log.e("Error",ApplicationLinks.newCouponsLink);
    couponslist=saxParser.parse();  
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return couponslist;
}

@Override
protected void onPostExecute(ArrayList<NewCoupons> result) {
    // TODO Auto-generated method stub
    parentActivity.fetchNewCoupons(result);
        }

}

您可以在onPreExecute()函数中加载进度条,并在onPostExecute()函数中将其停止

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM