我已经预先解析了这个 RSS 源,现在我已经更新它以使用 AsyncTask 它现在显示一个空白屏幕而不是一个填充的列表视图代码:
public class CustomizedListView extends Activity {
static String URL= "http://scout.org/rss/feed/all";
static final String KEY_ITEM = "item";
static final String KEY_TITLE = "title";
static final String KEY_DESCRIPTION = "description";
static final String KEY_LINK = "link";
static final String KEY_PUB_DATE = "pubDate";
String PATH = Environment.getExternalStorageDirectory()
+ "/SCOUTS/";
ListView list;
LazyAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news);
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
URL= sharedPrefs.getString("news_feed", "-1");
ArrayList<HashMap<String, String>> libraryList = new ArrayList<HashMap<String, String>>();
list=(ListView)findViewById(R.id.listView1);
adapter=new LazyAdapter(CustomizedListView.this, libraryList);
list.setAdapter(adapter);
new MyAsyncTask() .execute();
}
class MyAsyncTask extends AsyncTask<String, Integer, ArrayList<HashMap<String, String>> > {
private ProgressDialog progressDialog = new ProgressDialog(CustomizedListView.this);
protected void onPreExecute() {
progressDialog.setMessage("Loading Latest WOSM News");
progressDialog.show();}
ArrayList<HashMap<String, String>> libraryList = new ArrayList<HashMap<String, String>>();
@Override
protected ArrayList<HashMap<String, String>> doInBackground(String... params) {
try {
new DefaultHttpClient().execute(new HttpGet(URL))
.getEntity().writeTo(
new FileOutputStream(new File(PATH,"news.xml")));
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
File dir = Environment.getExternalStorageDirectory();
File yourFile = new File(dir, "/SCOUTS/news.xml");
FileInputStream stream = null;
try {
stream = new FileInputStream(yourFile);
} catch (FileNotFoundException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
String jString = null;
try {
FileChannel fc = stream.getChannel();
MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
/* Instead of using default, pass in a decoder. */
jString = Charset.defaultCharset().decode(bb).toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
try {
stream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
final ArrayList<HashMap<String, String>> libraryList = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = jString;
Document doc = parser.getDomElement(xml);
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
for (int i = 0; i < nl.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
map.put(KEY_DESCRIPTION, parser.getValue(e, KEY_DESCRIPTION));
map.put(KEY_LINK, parser.getValue(e, KEY_LINK));
map.put(KEY_PUB_DATE, parser.getValue(e, KEY_PUB_DATE));
libraryList.add(map);
}
return libraryList;}
@Override
protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
list=(ListView)findViewById(R.id.listView1);
adapter=new LazyAdapter(CustomizedListView.this, libraryList);
list.setAdapter(adapter);
this.progressDialog.dismiss();
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
HashMap<String, String> map = libraryList.get(position);
Intent in = new Intent(CustomizedListView.this, org.scouts.android.news.Podcast.class);
in.putExtra(KEY_TITLE, map.get(KEY_TITLE));
in.putExtra(KEY_DESCRIPTION, map.get(KEY_DESCRIPTION));
in.putExtra(KEY_LINK, map.get(KEY_LINK));
in.putExtra(KEY_PUB_DATE, map.get(KEY_PUB_DATE));
startActivity(in);
}
});
;}}}
我不知道是什么原因造成的,但如果你能帮忙,请做