0

我很难弄清楚这一点。我想我只是不明白如何正确组装 AsyncTask。我在我的课堂上有这个方法,但希望它作为 AsyncTask 运行,而不是在调用它时让所有东西都陷入困境。任何人都可以帮忙吗?提前致谢。

private void getDatesNames() {
JSONParser jParser = new JSONParser();

JSONObject json = jParser.getJSONFromUrl(url);

ArrayList<String> dates = new ArrayList<String>();
ArrayList<String> teams = new ArrayList<String>();


try {

    contacts = json.getJSONArray(TAG_CONTACTS);


    for(int i = 0; i < contacts.length(); i++){
        JSONObject c = contacts.getJSONObject(i);

        if ((c.getString(TAG_EMAIL1)).contains(league)) {

            // Storing each json item in variable
            String id = c.getString(TAG_ID);
            String email1 = c.getString(TAG_EMAIL1);
            String email2 = c.getString(TAG_EMAIL2);


            dates.add(id);
            teams.add(email1);
            teams.add(email2);
        }
    }

} catch (JSONException e) {
    e.printStackTrace();
}


LinkedHashSet hs1 = new LinkedHashSet();
LinkedHashSet hs2 = new LinkedHashSet();

hs1.addAll(dates);
hs2.addAll(teams);
dates.clear();
teams.clear();
dates.addAll(hs1);
teams.addAll(hs2);

    for (int i = 0; i < dates.size(); ++i) {
    adapter1.add(dates.get(i));
}

for (int i = 0; i < teams.size(); ++i)
{

    adapter2.add(teams.get(i));

}
4

2 回答 2

0

只需将慢速的东西放在 doInBackground 中即可。在您填写两个数组的地方,最好将它们设为私有最终,以便不同的方面可以看到它们。

 AsyncTask<Void, Void, Void> newTask =  new AsyncTask<Void, Void, Void>() {

        private final ArrayList<String> dates = new ArrayList<String>();
        private final ArrayList<String> teams = new ArrayList<String>();
        @Override
        protected void onPreExecute() {
            //You dont' really need anything here, so you could leave out onPreExecute()...
        }

        @Override
        protected ArrayList<String> doInBackground(Void... params) {
            JSONParser jParser = new JSONParser();

            JSONObject json = jParser.getJSONFromUrl(url);

            try {

                contacts = json.getJSONArray(TAG_CONTACTS);

                for(int i = 0; i < contacts.length(); i++){
                    JSONObject c = contacts.getJSONObject(i);

                    if ((c.getString(TAG_EMAIL1)).contains(league)) {

                        // Storing each json item in variable
                        String id = c.getString(TAG_ID);
                        String email1 = c.getString(TAG_EMAIL1);
                        String email2 = c.getString(TAG_EMAIL2);


                        dates.add(id);
                        teams.add(email1);
                        teams.add(email2);
                    }
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }


            LinkedHashSet hs1 = new LinkedHashSet();
            LinkedHashSet hs2 = new LinkedHashSet();

            hs1.addAll(dates);
            hs2.addAll(teams);
            dates.clear();
            teams.clear();
            dates.addAll(hs1);
            teams.addAll(hs2);

        }

        @Override
        protected void onPostExecute(ArrayList<String> fileList) {
            // dismiss dialog if necessary
            for (int i = 0; i < dates.size(); ++i) {
                adapter1.add(dates.get(i));
            }

            for (int i = 0; i < teams.size(); ++i)
            {

                adapter2.add(teams.get(i));

            }
        }

    };
    newTask.execute();
于 2013-08-28T22:24:26.687 回答
0

你需要跑getDateNames()进去doInBackground(String... arg)。看看这个教程。它也适用于 JSON 并使用 AsyncTasks。

于 2013-08-28T22:25:54.273 回答