我有一個RSS閱讀器在我的應用程序中工作。不過,我非常希望將線程從主I/O移開並使其成爲Async。實施異步任務
我真的被卡住了。
據我所知,這可以用幾條簡單的線條完成,但我對如何實現它感到困惑。
這是填充列表視圖的代碼: -
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);
}
}
不回答你的問題,但你可以在你的內部類使用CustomizedListView.this,所以你不需要thisReference。 –
你面臨的問題是什麼?在'AsyncTask'中獲取數據後實現'AsyncTask'或填充'ListView'? – Nevercom