0

我想在 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));

}

}
4

2 回答 2

1

你可以试试这段代码

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

    }

}

}
于 2012-10-11T13:36:38.340 回答
0

这是 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() 函数中停止它

于 2012-10-11T13:46:00.653 回答