AsyncTask to get xml

160 views Asked by At

I want to use AsynTask to do custom list with xml from url. I am novice so I don´t know how to structure this properly. When the fragment start doesn´t display nothing. I am using Swipe views with fragments but I don´t know how correct the issue

public class UltimasFragment extends Fragment {

static final String URL = "http://myurl.com/songs";
// XML node keys
static final String KEY_SONG = "songs"; // parent node
static final String KEY_ID = "id_song";
static final String KEY_TITLE = "name";
static final String KEY_THUMB_URL = "picture";
static final String KEY_ARTIST = "duration";


View view;
ListView list;
LazyAdapter adapter;


private ProgressDialog dialog;
public ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {


        View view = inflater.inflate(R.layout.ultimas, container, false);

        new MiTarea().execute();

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

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

        adapter=new LazyAdapter(getActivity(), 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) {

                  String idrecibida =songsList.get(+position).get("id_song");
                  String nombrerecibido =songsList.get(+position).get("name");
                  String caratularecibida =songsList.get(+position).get("picture");
                  String nuestraoprecibida =songsList.get(+position).get("duration");


            }
        }); 


        return view;

}


private class MiTarea extends AsyncTask<String, String, String>{

    protected void onPreExecute() {
        dialog = new ProgressDialog(getActivity());
        dialog.setMessage("Actualizando...");
        dialog.setIndeterminate(false);
        dialog.setCancelable(true);
        dialog.show();
     }

     protected String doInBackground(String... args) {



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


        // 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_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
            map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));


            songsList.add(map);




        }
        return xml;


     }


     protected void onPostExecute(String bytes) {
         dialog.dismiss();
      // adding HashList to ArrayList




     }
}

}

2

There are 2 answers

5
bottus On

Here is how to impement async task

http://developer.android.com/reference/android/os/AsyncTask.html

The preExecute method allow you to perform operations before starting the job you want to do asyncronously.

the doInBackground method is here to do your async job.

the on postExecute method will receive what doInBackground returns if it returns something.

if you need to pass parameters to your class like a context, my advice is to make a constructor and passing parameters to it ;)

hope it helped.

regards.

1
greywolf82 On

The three parameters are: the parameter type of doInBackground, the parameter type of onProgressUpdate and the parameter type of onPostExecute.

See here the usage: http://developer.android.com/reference/android/os/AsyncTask.html

Remember to call setReteainInstance(true) in the fragment in this case, because if the fragment is remove you lost the reference to the AsyncTask.