1

我想将 ClassAsyncTask 用于一个单独的类,我正在尝试但没有成功。我需要有关如何执行此操作的明确信息或如何执行此操作的想法。提前致谢。

public class XMLParser extends Activity {

    String targetURL = "http://www.androidpeople.com/wp-content/uploads/2010/06/example.xml";
    TextView name[], website[], category[];
    LinearLayout linearLayout;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /* layout to display the view */
        linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(1);

        /* Set the ContentView to layout for display */
        this.setContentView(linearLayout);

        ClassAsyncTask asyncTask = new GBAsyncTask();
        asyncTask.execute(targetURL);

    }

    //
    public class ClassAsyncTask extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... urls) {

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

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

            } catch (Exception e) {
                System.out.println("XML Pasing Excpetion = " + e);
            }
            return null;
        }

        /* Return-value from doInBackground */
        protected void onPostExecute(String result) {

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

            /* Assign TextView array length by arrayList size */
            name = new TextView[sitesList.getName().size()];
            website = new TextView[sitesList.getName().size()];
            category = new TextView[sitesList.getName().size()];

            int h = sitesList.getName().size();
            /* Set the result text in TextView and add it to layout */
            for (int i = 0; i < h; i++) {
                name[i] = new TextView(getApplicationContext());
                name[i].setText("Name = " + sitesList.getName().get(i));
                website[i] = new TextView(getApplicationContext());
                website[i]
                        .setText("Website = " + sitesList.getWebsite().get(i));
                category[i] = new TextView(getApplicationContext());
                category[i].setText("Website Category = "
                        + sitesList.getCategory().get(i));
                linearLayout.addView(name[i]);
                linearLayout.addView(website[i]);
                linearLayout.addView(category[i]);
            }
        }
    }
}

谢谢。

4

2 回答 2

1

您可以ClassAsyncTask使用 . 从另一个类访问XMLParser.ClassAsyncTask

如果要使子类独立于父类(以扩大其使用范围),则应确保删除对父类的类变量的任何引用。在你的情况下,这是targetURL. 您可以通过创建一个存储目标 URL 的类构造函数来做到这一点。

于 2012-09-04T09:23:33.553 回答
0

You can separate it and hand over the current context and an interface implementation for the callback method in the constructor, e.g.:

public class JSONLoader extends AsyncTask<String, Void, String> {
    private AsyncTaskCompleteListener<String> callback;
    private Context context;
    ..

    public JSONLoader(Context context, AsyncTaskCompleteListener<String> cb) {
        this.context = context;
        this.callback = cb;
    }

    ..
    // callback method implemented by calling activity:
    protected void onPostExecute(String result) {
        // call callback method
        callback.onTaskComplete(result);
    }
}

The task is started from an activity this way (note that it doesn't need to extend FragmentActivity. It can extend Activity or any of its subclasses as well):

public class MainActivity extends FragmentActivity 
    implements AsyncTaskCompleteListener<String> {

    public void onCreate(Bundle savedInstanceState) {
        ..
        JSONLoader jsonLoader = new JSONLoader(this, this);
        jsonLoader.execute(getString(R.string.datasource_url_list));
    }

    // from AsyncTaskCompleteListener interface
    public void onTaskComplete(String result) {
        // your callback method implementation
    }
    ..
}

And this is the interface that can be used by your activities to define different callback methods:

public interface AsyncTaskCompleteListener<T> {
    public void onTaskComplete(T result);

}
于 2012-09-04T09:42:32.797 回答