如何从以下示例实现AsyncTask?

时间:2011-08-31 05:42:12

标签: android progress-bar android-asynctask

我尝试修复它,但是列表数据没有显示出来。而且,我的代码中还有一些红线。 onPostExecute它告知构造函数SimpleAdapter(EventsActivity.syncEvent, ArrayList<HashMap<String,String>>, int, String[], int[])未定义。

另一件事,我想做的是当我们刷新偶像时显示progress,就像我们刷新facebook中的通知一样,我们会看到刷新数据的动画,我们刷新的最后日期和时间。我怎样才能做到这一点?你有什么主意吗?我很擅长实施。谢谢你的建议。

public class EventsActivity extends ListActivity {

    EventDataSet sitesList = null;
    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

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

    }

    private class syncEvent extends AsyncTask<String, Integer, String> {

        @Override
        protected String doInBackground(String... arg0) {
            try {
                /** Handling XML */
                SAXParserFactory spf = SAXParserFactory.newInstance();
                SAXParser sp = spf.newSAXParser();
                XMLReader xr = sp.getXMLReader();

                /** Send URL to parse XML Tags */
                URL sourceUrl = new URL(
                        "http://xxxx.heroku.com/xxxxxxxxxxx.xml");

                /** Create handler to handle XML Tags ( extends DefaultHandler ) */
                EventXMLHandler myXMLHandler = new EventXMLHandler();
                xr.setContentHandler(myXMLHandler);
                xr.parse(new InputSource(sourceUrl.openStream()));

            } catch (Exception e) {

            }

            /** Get result from MyXMLHandler SitlesList Object */
            sitesList = EventXMLHandler.sitesList;

            /** Set the result text in textview and add it to layout */
            for (int i = 0; i < sitesList.getName().size(); i++) {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("name", "Name: " + sitesList.getName().get(i));
                map.put("createat", "Create-At: " + Convertor.getDateTimeDDMMYY(sitesList.getCreateat().get(i)));

                mylist.add(map);
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {

            ListAdapter adapter = new SimpleAdapter(this, mylist, R.layout.eventitem,
                    new String[]{"name", "createat"},
                    new int[]{R.id.item_title, R.id.item_subtitle});

            setListAdapter(adapter);
            //super.onPostExecute(result);
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            // TODO Auto-generated method stub
            super.onProgressUpdate(values);
        }
    }
}

2 个答案:

答案 0 :(得分:3)

问题在于this引用。当您在AsyncTask中时,this引用您的AsyncTask子类,而SimpleAdapter需要Context作为第一个参数。您应该使用对父类的显式引用替换调用:

new SimpleAdapter(EventsActivity.this, mylist , R.layout.eventitem, 
                new String[] { "name", "createat" }, 
                new int[] { R.id.item_title, R.id.item_subtitle });

答案 1 :(得分:1)

现在您可以通过此:: new syncEvent().execute();

来调用它

喜欢::

  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listplaceholder);
    new syncEvent().execute();
}