0

I'm have a frustratingly tough time figuring this out. I guess I'm just not understanding how to correctly assemble an AsyncTask. I have this method in my class but want it to run as an AsyncTask instead of just bogging everything down when it gets called. Can anyone help? Thanks in advance.

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

}
Brock Harris
  • 29
  • 1
  • 5

2 Answers2

0

Just stick your slow stuff in the doInBackground. Where you are filling in two arrays, its probably better to just make them private final, so the different aspects can see them.

 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();
HalR
  • 11,411
  • 5
  • 48
  • 80
0

You'll need to run getDateNames() in doInBackground(String... arg). Take a look at this tutorial. It also works with JSON and uses AsyncTasks.

beenjaminnn
  • 752
  • 1
  • 11
  • 23