我的应用程序中有一个 RSS 阅读器。但是,我热衷于将线程从主 I/O 移开并使其异步。
我真的被困住了。
我知道这可以通过几行简单的代码来完成,但我对如何实现它感到非常困惑。
这是填充列表视图的代码:-
package co.uk.androidreader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class CustomizedListView extends Activity {
Intent intent = getIntent();
// All static variables
static final String URL = "URL of RSS Feed";
// XML node keys
static final String KEY_ARTICLE = "article"; // parent node
//static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_DATE = "date";
static final String KEY_STRAPLINE = "strapline";
static final String KEY_DETAIL = "detail";
static final String KEY_THUMB_URL = "image";
ListView list;
LazyAdapter adapter;
CustomizedListView thisReference = this;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
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_ARTICLE);
// 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_DATE, parser.getValue(e, KEY_DATE));
map.put(KEY_STRAPLINE, parser.getValue(e, KEY_STRAPLINE));
map.put(KEY_DETAIL, parser.getValue(e, KEY_DETAIL));
map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
// adding HashList to ArrayList
songsList.add(map);
}
list=(ListView)findViewById(R.id.list);
// Getting adapter by passing xml data ArrayList
adapter=new LazyAdapter(this, 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) {
Intent intent = new Intent(thisReference, DetailsActivity.class);
intent.putExtra(KEY_TITLE, (String) ((Map)adapter.getItem(position)).get(KEY_TITLE));
intent.putExtra(KEY_DATE, (String) ((Map)adapter.getItem(position)).get(KEY_DATE));
intent.putExtra(KEY_DETAIL, (String) ((Map)adapter.getItem(position)).get(KEY_DETAIL));
startActivity(intent);
}
});
}
public void GoToHome(View v)
{
Intent myIntent = new Intent(CustomizedListView.this, MainActivity.class);
//myIntent.putExtra("communityName", (String)getIntent().getExtras().get(CustomizedListViewCommunity.KEY_NAME));
startActivityForResult(myIntent, 0);
}
public void GoToBack(View v)
{
Intent myIntent = new Intent(CustomizedListView.this, MainActivity.class);
//myIntent.putExtra("communityName", (String)getIntent().getExtras().get(CustomizedListViewCommunity.KEY_NAME));
startActivityForResult(myIntent, 0);
}
}