如何正确实施进度对话框?

时间:2012-02-18 13:21:17

标签: android eclipse

我正在为android开发一个rss阅读器,但我无法实现进度对话框。这是代码,

    public class RSSReader extends Activity implements OnItemClickListener{
private static final int ACTIVITY_DESCRIPTION = 0;
private static final String MY_RSS = "http://www.xyz.com/rss/rss.xml";
private RSSFeed feed = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Typeface tf = Typeface.createFromAsset(getAssets(),
            "font/SutonnyMJ.ttf");
    TextView tv = (TextView) findViewById(R.id.footer);
    tv.setTypeface(tf); 
    feed = getFeed(MY_RSS);
    UpdateDisplay();
}
public void refresh()
{
    feed = getFeed(MY_RSS);
    UpdateDisplay();
}
//Menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.refresh:
        refresh();
        return true;
   case R.id.about:
         showDialog(1);
        return true;
   case R.id.end:
        finish();
        return true;
   case R.id.tv:
       Intent intentabout = new Intent(this,WebTV.class);
        startActivity(intentabout);
        return true;
    default:
        return super.onOptionsItemSelected(item);
        //return true;
    }

}
//About Dialog
  protected Dialog onCreateDialog(int id) {
        final Dialog dialog = new Dialog(this);

        switch(id){
        case 1:
            dialog.setContentView(R.layout.about);
            dialog.setTitle("About");
            TextView text = (TextView) dialog.findViewById(R.id.text);
            ImageView image = (ImageView) dialog.findViewById(R.id.image);
            image.setImageResource(R.drawable.logobn24);
            Button button = (Button) dialog.findViewById(R.id.cancel);
            button.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                        dialog.cancel();
                    }
                });
            break;
        default:
            //dialog = null;
        }
        return dialog;
    }

public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    Intent i = new Intent(this, ShowDescription.class);
    RSSItem item = feed.getItem(position);

    i.putExtra("title", item.getTitle());
    i.putExtra("description", item.getDescription());
    i.putExtra("link", item.getLink());
    i.putExtra("pubdate", item.getPubdate());

    startActivityForResult(i, ACTIVITY_DESCRIPTION);
}

private void UpdateDisplay() {
    TextView feedTitle = (TextView) findViewById(R.id.feedtitle);
    Typeface tf = Typeface.createFromAsset(getAssets(),
            "font/SutonnyMJ.ttf");
    feedTitle.setTypeface(tf);
    TextView feedPubdate = (TextView) findViewById(R.id.feedpubdate);
    ListView itemList = (ListView) findViewById(R.id.itemlist);

    if (feed == null) {
        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.toast_layout,
                                       (ViewGroup) findViewById(R.id.toast_layout_root));

        ImageView image = (ImageView) layout.findViewById(R.id.image);
        image.setImageResource(R.drawable.alert);
        TextView text = (TextView) layout.findViewById(R.id.text);
        text.setTypeface(tf);
        text.setText("Avi Gm Gm cvIqv hvqwb, `qv K‡i B›Uvi‡b‡Ui jvBb wbwðZ Kiæb!");

        Toast toast = new Toast(getApplicationContext());
        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(layout);
        toast.show();
        //Toast.makeText(this,"No RSS Feed Available", Toast.LENGTH_LONG).show();
        return;
    }

    //feedTitle.setText(feed.getTitle());
    feedTitle.setText("msev` we‡bv`b mviv¶Y");
    feedPubdate.setText(feed.getPubdate());

    //ArrayAdapter<RSSItem> adapter = new ArrayAdapter<RSSItem>(this,
            //android.R.layout.simple_list_item_1, feed.getAllItems());
    ArrayAdapter<RSSItem> adapter = new OrderAdapter<RSSItem>(this,
            R.layout.rsslist,feed.getAllItems());   
    itemList.setAdapter(adapter);
    itemList.setSelection(0);
    itemList.setOnItemClickListener(this);
}

private RSSFeed getFeed(String urlToRssFeed) {
    try {
        URL url = new URL(urlToRssFeed);

        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser parser = factory.newSAXParser();

        XMLReader xmlReader = parser.getXMLReader();
        RSSHandler rssHandler = new RSSHandler();
        xmlReader.setContentHandler(rssHandler);

        InputSource is = new InputSource(url.openStream());
        xmlReader.parse(is);

        return rssHandler.getFeed();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

我想添加一个进程对话框,比如加载....以便耗时的方法UpdateDisplay()可以在后台运行。任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

您需要使用AsyncTask。我正在从here获取代码并进行一些修改:

public class ProgressTask extends AsyncTask<Void, Void, Boolean> {
    /** progress dialog to show user that the backup is processing. */
    private ProgressDialog dialog;
    /** application context. */
    private Activity activity;

    public ProgressTask(RSSReader activity) {
        this.activity = activity;
        dialog = new ProgressDialog(context);
    }



    protected void onPreExecute() {
        this.dialog.setMessage("Progress start");
        this.dialog.show();
    }

    @Override
    protected void onPostExecute(final Boolean success) {
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    protected Boolean doInBackground(final Void... args) {
         UpdateDisplay(); //here go the operations you want executed in the background 
    }
}

然后在您的活动中,您只需通过以下方式致电:AsyncTask task = new ProgressTask(this).execute();

答案 1 :(得分:0)

使用asynctask类

public class getRSS extends AsyncTask<Void, Void, Void> {
    ProgressDialog progressDialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        progressDialog = new ProgressDialog(actvity.this);
        progressDialog.setMessage("Please wait.....");
        progressDialog.setTitle("Loading...");
        progressDialog.setCancelable(false);
        progressDialog.show();

    }

    @Override
    protected Void doInBackground(Void... params) {

     // here code to call rss webservice 
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);

        //parse rss service here and also display here 
        if (progressDialog != null && progressDialog.isShowing())
            progressDialog.dismiss();
    }
}
相关问题