您好我正在尝试创建一个ASyncTask,它将在后台解析XML文件中的数据,然后在ListView中显示该String数组数据。到目前为止,我不明白我做错了什么,或者如何将String Array值返回给我的GUI。这是我到目前为止所用的代码,如果你需要别的东西,请告诉我。感谢您查看并提供任何建议或地点以寻求更多信息。
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");
}
编辑:我在查看其他示例之后添加了新代码这是我提出的,但是现在当我在模拟器中运行它时,它只显示我的Main.xml的背景,列表没有填充,如果我将其导入到运行ICS的手机中,它表示存在错误并且强制关闭...由于某种原因我无法在模拟器中发生这种情况,并且模拟器的LogCat中没有错误。
答案 0 :(得分: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);
}
}
}
}
要更改的代码太多,请阅读AsyncTask docs。特别是execute()和onPostExecute()。
答案 1 :(得分:1)
在您寻求帮助之前,请养成阅读文档的习惯。您需要的一切都是here。
具体来说,您需要在PlayerGet类中实现onPostExecute。它将在后台任务完成时调用,并将ArrayList作为回调参数返回给您。
还有一些有用的提示。始终遵循标准Java命名约定。您的班级名称“scottPlayers”应以大写字母开头。除非绝对必要,否则尽量避免使用Object。类型转换和类型检查将在以后为您节省一个痛苦的世界。