0

I have made a normal class and an AsyncTask and I want to make this code:

private void addNewQuake (Quake _quake) {
    //Add the new quake to our list of earthquakes
    earthquakes.add(_quake);

    //Notify the array adapter of a change
    aa.notifyDataSetChanged();
}

So I can add it to AsyncTask? Or do I need to do in a other way?

Orhan Obut
  • 8,756
  • 5
  • 32
  • 42
  • what do you exactly want to achive? – Orhan Obut Jan 27 '14 at 20:30
  • I have made a app that shows new earthquakes. And addNewQuake wil add a new to the list. – user3238333 Jan 27 '14 at 20:42
  • If you want you can see all code here: http://stackoverflow.com/questions/21389779/android-os-networkonmainthreadexception-manifest/21389847?noredirect=1#comment32261426_21389847 I have added all code on refreshEarthquake to the Async and: ListView earthquakeListView; ArrayAdapter aa; ArrayList earthquakes = new ArrayList(); This is the first time I use Async – user3238333 Jan 27 '14 at 20:44

2 Answers2

0
@Override
protected void onCreate(Bundle savedInstanceState) {

   // your other code

   new DataLoadAsync().execute(); //start to get data
}


public class DataLoadAsync extends AsyncTask{

   private Quake quake;

   @Override
   protected void doInBackground(){
      // handle network data in here and put the result to quake in here.
      refreshEarthquakes();
   }

   @Override
   protected void onPostExecute(){
      // use the result
      addNewQuake(quake);
   }
}

syntax is not complete, I just wanted to show you, you need to put your network data load method in doInBackground to get data, then you can use it in onPostExecute()

Orhan Obut
  • 8,756
  • 5
  • 32
  • 42
0

Here is my new code:

public class Earthquake extends Activity {

ListView earthquakeListView;
ArrayAdapter<Quake> aa;

ArrayList<Quake> earthquakes = new ArrayList<Quake>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    earthquakeListView = (ListView) this
            .findViewById(R.id.earthquakeListView);

    int layoutID = android.R.layout.simple_list_item_1;
    aa = new ArrayAdapter<Quake>(this, layoutID, earthquakes);
    earthquakeListView.setAdapter(aa);

    new DataLoadAsync().execute();
}   

public class DataLoadAsync extends AsyncTask<Object, Object, Object>{

       private Quake quake;

       protected void doInBackground(){
          // handle network data in here and put the result to quake in here.
          refreshEarthquakes();
       }

       protected void onPostExecute(){
          // use the result
          addNewQuake(quake);
       }

    @Override
    protected Object doInBackground(Object... arg0) {
        // TODO Auto-generated method stub
        return null;
    }
    }

     void refreshEarthquakes() {
        //get the XML
        URL url;
        try {
            String quakeFeed = getString(R.string.quake_feed);
            url = new URL(quakeFeed);

            URLConnection connection;
            connection = url.openConnection();

            HttpURLConnection httpConnection = (HttpURLConnection)connection;
            int responseCode = httpConnection.getResponseCode();

            if (responseCode == HttpURLConnection.HTTP_OK) {
                InputStream in = httpConnection.getInputStream();

                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();

                //Parse the earthquake feed
                Document dom = db.parse(in);
                Element docEle = dom.getDocumentElement();

                //Clear the old earthquakes
                earthquakes.clear();

                //Get a list of each earthquake entry
                NodeList nl = docEle.getElementsByTagName("entry");
                if (nl != null && nl.getLength() > 0) {
                    for (int i = 0 ; i < nl.getLength(); i++) {
                        Element entry = (Element)nl.item(i);
                        Element title = (Element)entry.getElementsByTagName("title").item(0);
                        Element g = (Element)entry.getElementsByTagName("georss:point").item(0);
                        Element when = (Element)entry.getElementsByTagName("updated").item(0);
                        Element link = (Element)entry.getElementsByTagName("link").item(0);

                        String details = title.getFirstChild().getNodeValue();
                        String hostname = "http://earthquake.usgs.gov";
                        String linkString = hostname + link.getAttribute("href");

                        String point = g.getFirstChild().getNodeValue();
                        String dt = when.getFirstChild().getNodeValue();
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");
                        Date qdate = new GregorianCalendar(0,0,0).getTime();
                        try {
                            qdate = sdf.parse(dt);
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }

                        String[] location = point.split(" ");
                        Location l = new Location("dummyGPS");
                        l.setLatitude(Double.parseDouble(location[0]));
                        l.setLongitude(Double.parseDouble(location[1]));

                        String magnitudeString = details.split(" ")[1];
                        int end = magnitudeString.length()-1;
                        double magnitude = Double.parseDouble(magnitudeString.substring(0, end));

                        details = details.split(",")[1].trim();

                        Quake quake = new Quake(qdate, details, l, magnitude, linkString);
                    }
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
        finally {
        } 
    }

private void addNewQuake (Quake _quake) {
    //Add the new quake to our list of earthquakes
    earthquakes.add(_quake);

    //Notify the array adapter of a change
    aa.notifyDataSetChanged();
}
}