2

I have an RSS reader working in my app. However I am keen to move the thread away from the main I/O and make it Async.

I am really stuck.

I understand that this can be done in a few simple lines but i'm getting really confused with how to implement it.

This is the code that populates the list view:-

package co.uk.androidreader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class CustomizedListView extends Activity {


Intent intent = getIntent();
// All static variables
static final String URL = "URL of RSS Feed";
// XML node keys
static final String KEY_ARTICLE = "article"; // parent node
//static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_DATE = "date";
static final String KEY_STRAPLINE = "strapline";
static final String KEY_DETAIL = "detail";
static final String KEY_THUMB_URL = "image";



ListView list;
LazyAdapter adapter;

CustomizedListView thisReference = this;

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


    ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(URL); // getting XML from URL
    Document doc = parser.getDomElement(xml); // getting DOM element

    NodeList nl = doc.getElementsByTagName(KEY_ARTICLE);
    // looping through all song nodes <song>
    for (int i = 0; i < nl.getLength(); i++) {
        // creating new HashMap
        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);
        // adding each child node to HashMap key => value
        //map.put(KEY_ID, parser.getValue(e, KEY_ID));
        map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
        map.put(KEY_DATE, parser.getValue(e, KEY_DATE));
        map.put(KEY_STRAPLINE, parser.getValue(e, KEY_STRAPLINE));
        map.put(KEY_DETAIL, parser.getValue(e, KEY_DETAIL));
        map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));


        // adding HashList to ArrayList
        songsList.add(map);
    }


    list=(ListView)findViewById(R.id.list);

    // Getting adapter by passing xml data ArrayList
    adapter=new LazyAdapter(this, songsList);        
    list.setAdapter(adapter);


    // Click event for single list row
    list.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Intent intent = new Intent(thisReference, DetailsActivity.class);

            intent.putExtra(KEY_TITLE, (String) ((Map)adapter.getItem(position)).get(KEY_TITLE));
            intent.putExtra(KEY_DATE, (String) ((Map)adapter.getItem(position)).get(KEY_DATE));
            intent.putExtra(KEY_DETAIL, (String) ((Map)adapter.getItem(position)).get(KEY_DETAIL));

            startActivity(intent);
        }
    });     
}

public void GoToHome(View v)
{
         Intent myIntent = new Intent(CustomizedListView.this, MainActivity.class);
         //myIntent.putExtra("communityName", (String)getIntent().getExtras().get(CustomizedListViewCommunity.KEY_NAME));
        startActivityForResult(myIntent, 0);

 }

public void GoToBack(View v)
{
         Intent myIntent = new Intent(CustomizedListView.this, MainActivity.class);
         //myIntent.putExtra("communityName", (String)getIntent().getExtras().get(CustomizedListViewCommunity.KEY_NAME));
        startActivityForResult(myIntent, 0);

 }

}

  • Not answering your question, but you can use CustomizedListView.this in your inner classes, so you don't need thisReference. – FD_ Jul 31 '13 at 12:35
  • Whats the problem you facing ? implementing `AsyncTask` or populating the `ListView` after fetching data in `AsyncTask` ? – Nevercom Jul 31 '13 at 13:52

3 Answers3

1

Here's a barbones implementation of the onCreate method...

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

     //define the task as an anonymous type (you could create a concrete inner class as well)
    AsyncTask<Void, Void, ArrayList<HashMap<String,String>>> task = new AsyncTask<Void, Void, ArrayList<HashMap<String,String>>>() {

        @Override
        protected ArrayList<HashMap<String, String>> doInBackground(
                Void... params) {
            ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

            XMLParser parser = new XMLParser();
            String xml = parser.getXmlFromUrl(URL); // getting XML from URL
            Document doc = parser.getDomElement(xml); // getting DOM element

            NodeList nl = doc.getElementsByTagName(KEY_ARTICLE);
            // looping through all song nodes <song>
            for (int i = 0; i < nl.getLength(); i++) {
                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();
                Element e = (Element) nl.item(i);
                // adding each child node to HashMap key => value
                //map.put(KEY_ID, parser.getValue(e, KEY_ID));
                map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
                map.put(KEY_DATE, parser.getValue(e, KEY_DATE));
                map.put(KEY_STRAPLINE, parser.getValue(e, KEY_STRAPLINE));
                map.put(KEY_DETAIL, parser.getValue(e, KEY_DETAIL));
                map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));


                // adding HashList to ArrayList
                songsList.add(map);
            }
            return songsList;
        }

        @Override
        protected void onPostExecute(
                ArrayList<HashMap<String, String>> result) {
            super.onPostExecute(result);

            ListView list=(ListView)CustomizedListView.this.findViewById(R.id.list);
            // Getting adapter by passing xml data ArrayList                
            list.setAdapter(new LazyAdapter(this, result));
        }

    };

            //execute the task
    task.execute((Void[])null);

    list=(ListView)findViewById(R.id.list);
    // Click event for single list row
    list.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Intent intent = new Intent(thisReference, DetailsActivity.class);

            intent.putExtra(KEY_TITLE, (String) ((Map)adapter.getItem(position)).get(KEY_TITLE));
            intent.putExtra(KEY_DATE, (String) ((Map)adapter.getItem(position)).get(KEY_DATE));
            intent.putExtra(KEY_DETAIL, (String) ((Map)adapter.getItem(position)).get(KEY_DETAIL));

            startActivity(intent);
        }
    });     
}
pdriegen
  • 2,019
  • 1
  • 13
  • 19
0

Hope this helps:

AsyncTask<String, Void, String> worker = new AsyncTask<String, Void, String>() {

    @Override
    protected String doInBackground(String... param) {
        // here goes the code you want off the main thread
        return "the result of this code that goes on onPostExecute";
    }

    @Override
    protected void onPostExecute(String str) {
        super.onPostExecute(str);

        // here goes the code that will run after you doInBackground has finished working
    }

};

worker.execute("any param you want");
Androiderson
  • 16,865
  • 6
  • 62
  • 72
0

Any network task will cannot be run on the mainthread, so we need an asynctask.

class RequestTask extends AsyncTask<String, String, String>{



    @Override
    protected String doInBackground(String... uri) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response;
        String responseString = null; // THIS IS WHERE THE INFO IS SAVED

        try {
            response = httpclient.execute(new HttpGet(uri[0]));
            StatusLine statusLine = response.getStatusLine();
            if(statusLine.getStatusCode() == HttpStatus.SC_OK){

                ByteArrayOutputStream out = new ByteArrayOutputStream();
                 response.getEntity().writeTo(out);
                 out.close();
                 responseString = out.toString("UTF-8");
            } 

            else{
                //Closes the connection.
                response.getEntity().getContent().close();
                throw new IOException(statusLine.getReasonPhrase());
            }
        } 

        catch (ClientProtocolException e) {
            e.printStackTrace();

            //Handle httprequest when answer is not expected
        } 

        catch (IOException e) { 
            e.printStackTrace();

            //Handle httprequest when answer is not expected
        }
        return responseString;
    }



    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result); 
        progressDialog.dismiss();

              //handle your result here and any catches



        }           









    }




}

then you use new RequestTask().execute("http://www.yourapilocation.com/whatever"); to call the method.

Hope this helps.

Paul
  • 478
  • 2
  • 16