0

我有一项活动,其中 RSS 提要从网站加载并显示在列表视图中。问题是将内容加载到列表视图中需要几秒钟。我想实现一个进度条来通知用户加载数据。

下面是显示RSS提要的代码..

public class RSS extends ListActivity {
    List<String> headlines;
    List<String> links;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rss);

     // Initializing instance variables
        headlines = new ArrayList<String>();
        links = new ArrayList<String>();

        try {
            URL url = new URL("http://feeds.pcworld.com/pcworld/latestnews");

            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(false);
            XmlPullParser xpp = factory.newPullParser();

                // We will get the XML from an input stream
            xpp.setInput(getInputStream(url), "UTF_8");

            boolean insideItem = false;

                // Returns the type of current event: START_TAG, END_TAG, etc..
            int eventType = xpp.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                if (eventType == XmlPullParser.START_TAG) {

                    if (xpp.getName().equalsIgnoreCase("item")) {
                        insideItem = true;
                    } else if (xpp.getName().equalsIgnoreCase("title")) {
                        if (insideItem)
                            headlines.add(xpp.nextText()); //extract the headline
                    } else if (xpp.getName().equalsIgnoreCase("link")) {
                        if (insideItem)
                            links.add(xpp.nextText()); //extract the link of article
                    }
                }else if(eventType==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")){
                    insideItem=false;
                }

                eventType = xpp.next(); //move to next element
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Binding data
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, headlines);

        setListAdapter(adapter);


    }
    private InputStream getInputStream(URL url) {
        // TODO Auto-generated method stub
           try {
               return url.openConnection().getInputStream();
           } catch (IOException e) {
               return null;
             }
    }
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        Uri uri = Uri.parse((String) links.get(position));
           Intent intent = new Intent(Intent.ACTION_VIEW, uri);
           startActivity(intent);
    }
}

那么有人可以在加载数据时帮助我实现一个简单的进度条吗?:)

4

2 回答 2

1

您应该为此使用异步任务

于 2012-10-01T16:55:43.290 回答
1

您应该使用 AsyncTask 来执行所有下载内容。然后在 AsyncTask 的 onProgessUpdate 方法中,将适配器刷新到 listview。

class LoadingTask extends AsyncTask<URL, Integer, Integer> {
    ProgressDialog mDialog;
    BaseAdapter mAdapter
    public LoadingTask(Context context, BaseAdapter adapter) {
        mDialog = new ProgressDialog(context);
        mAdapter = adapter;
        // Do your dialog stuff here
    }

    @Override
    public Integer doInBackground(URL... urls) {
        int result = 0; // number of loaded entities

        while (needsToDownload) {
            // Do your downloading here


            updateProgress(result / urls.length);
        }

        return result;
    }

    @Override
    public void onProgressUpdate(Integer... ints) {
        mDialog.setProgress(ints[0]);
        mAdapter.notifyDatasetChanged()
    }

    public void onPostExecute(Integer result) {
        mDialog.dismiss()
        Toast.makeToast(context, "Loaded " + result  + " urls...", 1).show();
    }
}
于 2012-10-01T16:55:56.037 回答