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.
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.