0
我怎樣才能線程方法readRss(),以便如果互聯網連接速度慢,它不會減慢應用程序的其餘部分?Android線程XML閱讀器方法
package com.noah.rss;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.view.LayoutInflater;
public class AndroidRssReader extends ListActivity {
private RSSFeed myRssFeed = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
readRss();
}
public class MyCustomAdapter extends ArrayAdapter<RSSItem> {
public MyCustomAdapter(Context context, int textViewResourceId, List<RSSItem> list) {
super(context, textViewResourceId, list);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// return super.getView(position, convertView, parent);
View row = convertView;
if (row == null) {
LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(R.layout.row, parent, false);
}
TextView listTitle = (TextView) row.findViewById(R.id.listtitle);
listTitle.setText(myRssFeed.getList().get(position).getTitle());
TextView listPubdate = (TextView) row.findViewById(R.id.listpubdate);
listPubdate.setText(myRssFeed.getList().get(position).getPubdate());
if (position % 2 == 0) {
listTitle.setBackgroundColor(0xff101010);
listPubdate.setBackgroundColor(0xff101010);
} else {
listTitle.setBackgroundColor(0xff080808);
listPubdate.setBackgroundColor(0xff080808);
}
return row;
}
private void readRss() {
try {
URL rssUrl = new URL("http://www.gov.hk/en/about/rss/govhkrss.data.xml");
SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
XMLReader myXMLReader = mySAXParser.getXMLReader();
RSSHandler myRSSHandler = new RSSHandler();
myXMLReader.setContentHandler(myRSSHandler);
InputSource myInputSource = new InputSource(rssUrl.openStream());
myXMLReader.parse(myInputSource);
myRssFeed = myRSSHandler.getFeed();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (myRssFeed != null) {
TextView feedTitle = (TextView) findViewById(R.id.feedtitle);
TextView feedDescribtion = (TextView) findViewById(R.id.feeddescribtion);
TextView feedPubdate = (TextView) findViewById(R.id.feedpubdate);
TextView feedLink = (TextView) findViewById(R.id.feedlink);
feedTitle.setText(myRssFeed.getTitle());
feedDescribtion.setText(myRssFeed.getDescription());
feedPubdate.setText(myRssFeed.getPubdate());
feedLink.setText(myRssFeed.getLink());
MyCustomAdapter adapter = new MyCustomAdapter(this, R.layout.row, myRssFeed.getList());
setListAdapter(adapter);
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Intent intent = new Intent(this, ShowDetails.class);
Bundle bundle = new Bundle();
bundle.putString("keyTitle", myRssFeed.getItem(position).getTitle());
bundle.putString("keyDescription", myRssFeed.getItem(position).getDescription());
bundle.putString("keyLink", myRssFeed.getItem(position).getLink());
bundle.putString("keyPubdate", myRssFeed.getItem(position).getPubdate());
intent.putExtras(bundle);
startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 0, 0, "Reload");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case (0):
Toast.makeText(this, "Reading News, Please wait.",
Toast.LENGTH_LONG).show();
readRss();
break;
default:
break;
}
return true;
}
}
爲什麼我得到一個錯誤,當我嘗試: \t保護的主題initBkgdThread =新主題(新的Runnable(){ \t \t公共無效的run(){ \t \t readRss(); \t \t \t \t} \t \t}); – Noah 2010-12-18 19:27:06
@ user,那麼,您的'readRss'函數包含修改UI組件(TextViews)的部分代碼,這是不允許的,UI組件只能從UI線程中修改....您可以使用'runOnUiThread'或一個'Handler'在UI線程....中運行'Runnables',但確保XML處理在非UI線程中完成.... – st0le 2010-12-19 04:09:47
感謝stOle,它的工作,我創造了一個Rannable:private Runnable mUpdateListTask =新可運行(){ \t \t公共無效的run(){ \t \t \t readRss(); \t \t \t displayRss(); \t \t \t} \t \t}; – Noah 2010-12-19 13:44:35