1

Hello I am trying to create an ASyncTask that will parse data from an XML file in the background and then display that String array data in a ListView. I am not understanding what I am doing wrong so far, or how to return the String Array values back to my GUI. Here is the code for what I have so far with it if you need anything else let me know please. Thank you for looking and giving any suggestions or places to turn to to find out more.

package com.lvlup.kikurself.scotttest;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import java.io.IOException;
import org.xml.sax.SAXException;

//import com.lvlup.kikurself.scotttest.WikiParser.Cm;

public class scottPlayers extends ListActivity {

public class PlayerGet extends AsyncTask<Void, Void, Void>{


@Override
protected void onPostExecute(Void result){

WikiParser p = new WikiParser();
ArrayList<String> titles = new ArrayList<String>();

try {
    p.parseInto(new URL("http://scottlandminecraft.wikia.com/api.php?action=query&list=categorymembers&cmtitle=Category:Players&cmlimit=500&format=xml"), titles);

} catch (MalformedURLException e) {
} catch (IOException e) {
} catch (SAXException e) {}

    //String[] values = new String[50]; 
    //values = res;




    ArrayAdapter<String> adapter = new ArrayAdapter<String>(scottPlayers.this, R.layout.main, titles);

    setListAdapter(adapter);

    //final ListView playersList = getListView();


    /*playersList.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v, int position, long thisID)
    {
         Object o = (playersList.getItemAtPosition(position));
         String playerName_temp = (o.toString());

         Intent newIntent = new Intent(v.getContext(), playerDisp.class);
         newIntent.putExtra("tempN", playerName_temp);
         startActivity(newIntent);

    }
    });*/

}

 @Override
 protected void onPreExecute() {
  // TODO Auto-generated method stub
  Toast.makeText(scottPlayers.this,
    "onPreExecute \n: preload bitmap in AsyncTask",
    Toast.LENGTH_LONG).show();
 }


 @Override
 protected Void doInBackground(Void... params) {
  // TODO Auto-generated method stub

  return null;
 }


}


@Override
public void onCreate(Bundle icicle) {
 super.onCreate(icicle);

 setContentView(R.layout.main);

 new PlayerGet().execute();
}


    //get your data back again with: String fName = getIntent().getExtras().getInt("fname");




}

EDIT: I added new code after looking at other examples this is what I have come up with but now when I run this in the Emulator it just shows the background of my Main.xml the list doesn't populate, and if I import this to my phone running ICS it says that there was an error and force closes...I couldn't get this to happen in the emulator for some reason, and there were no errors in the LogCat for the emulator.

2 Answers 2

3
public class scottPlayers extends ListActivity {

    private ArrayAdapter<String> mAdapter;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        mAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, new ArrayList<String>());

        setListAdapter(mAdapter);

        new PlayerGet().execute();
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        Object o = l.getItemAtPosition(position);
        String playerName_temp = o.toString();

        Intent newIntent = new Intent(v.getContext(), playerDisp.class);
        newIntent.putExtra("tempN", playerName_temp);
        startActivity(newIntent);
    }


    private class PlayerGet extends AsyncTask<Void, Void, ArrayList<String>> {

        WikiParser p = new WikiParser();
        ArrayList<String> titles = new ArrayList<String>();

        @Override
        protected ArrayList<String> doInBackground(Void... urls) {
            try {
                p.parseInto(new URL("http://scottlandminecraft.wikia.com/api.php?action=query&list=categorymembers&cmtitle=Category:Players&cmlimit=500&format=xml"), titles);
            } catch (MalformedURLException e) {
            } catch (IOException e) {
            } catch (SAXException e) {}
            return titles;
        }

        @Override
        protected void onPostExecute(ArrayList<String> result) {
            for (String item : result) {
                mAdapter.add(item);
            }
        }
    }
}

Too much code to change, read AsyncTask docs. Especially execute() and onPostExecute().

Sign up to request clarification or add additional context in comments.

Comments

1

Please get into the habit of reading the documentation before you ask for help. Everything you need is here.

Specifically, you need to implement onPostExecute in your PlayerGet class. It will be called when the background task has finished and will return the ArrayList to you as an argument to the callback.

A couple of helpful tips also. Always follow the standard Java naming conventions. Your class name "scottPlayers" should start with an uppercase letter. Also try to avoid using Object unless absolutely necessary. Type casting and type checking will save you a world of pain later on.

2 Comments

Thank you for this, I am sorry I jumped right to here before reading more on the subject, I have a bit of ADD, and sometimes just feel so dumb with all of this but I want to learn everything I can. I updated my code based on what I have read and other examples that I had found but it's still givng me issues..if you have any input at all I'd be very happy to hear it, even if its the most negative thing you could ever say, I need input on what I am doing so I can change. Thank you Simon.
It's my belief that SO is for learners and for experienced programmers, driven by a community approach. As a learner, the only thing you need to do is to show that you have made some effort to solve a problem. After all, it's only fair when asking people to spend time helping. Therefore, come back with a new question with the problem your're having. Show your code and if you're getting an exception, the logcat output. If you do that, you'll find lots of people willing to help because helping learners is a great way to improve your rep. Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.