如何以android的文本视图格式检索RSS feed?

时间:2013-07-25 02:19:53

标签: android eclipse rss textview

我可以知道如何以文本视图格式而不是列表视图格式检索RSS Feed吗?

我可以以列表视图格式检索它们但是现在我想将它们转换为文本视图格式,但我不知道该怎么做。

请提供建议。

由于

MainActivity.java

public class MainActivity 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.activity_main);

      try {
  URL rssUrl = new URL("http://app2.nea.gov.sg/data/rss/nea_psi.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) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 } catch (ParserConfigurationException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 } catch (SAXException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }

 if (myRssFeed!=null)
 {

  ArrayAdapter<RSSItem> adapter =
   new ArrayAdapter<RSSItem>(this,
     android.R.layout.simple_list_item_1,myRssFeed.getList());
  setListAdapter(adapter);

 }
  }

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
 // TODO Auto-generated method stub
 Intent intent = new Intent(this,ShowDetails.class);
 Bundle bundle = new Bundle();
 bundle.putString("keyTitle", myRssFeed.getItem(position).getTitle());
 bundle.putString("keyPsi", myRssFeed.getItem(position).getDescription());
 bundle.putString("keyLink", myRssFeed.getItem(position).getLink());
 bundle.putString("keyPubdate", myRssFeed.getItem(position).getPubdate());
 intent.putExtras(bundle);
      startActivity(intent);
}
}

RSSFeed.java

public class RSSFeed {
private String title = null;
private String description = null;
private String link = null;
private String pubdate = null;
private List<RSSItem> itemList;

RSSFeed(){
itemList = new Vector<RSSItem>(0);
}

void addItem(RSSItem item){
itemList.add(item);
}

RSSItem getItem(int location){
return itemList.get(location);
}

List<RSSItem> getList(){
return itemList;
}

void setTitle(String value)
{
title = value;
}
void setDescription(String value)
{
description = value;
}
void setLink(String value)
{
link = value;
}
void setPubdate(String value)
{
pubdate = value;
}

String getTitle()
{
return title;
}
String getDescription()
{
return description;
}
String getLink()
{
return link;
}
String getPubdate()
{
return pubdate;
}

}

RSSHandler.java

public class RSSHandler extends DefaultHandler {

final int state_unknown = 0;
final int state_title = 1;
final int state_description = 2;
final int state_link = 3;
final int state_pubdate = 4;
int currentState = state_unknown;

RSSFeed feed;
RSSItem item;

boolean itemFound = false;

RSSHandler(){
}

RSSFeed getFeed(){
return feed;
}

@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
feed = new RSSFeed();
item = new RSSItem();

}

@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
}

@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub

if (localName.equalsIgnoreCase("item")){
itemFound = true;
item = new RSSItem();
currentState = state_unknown;
}
else if (localName.equalsIgnoreCase("title")){
currentState = state_title;
}
else if (localName.equalsIgnoreCase("psi")){
currentState = state_description;
}
else if (localName.equalsIgnoreCase("link")){
currentState = state_link;
}
else if (localName.equalsIgnoreCase("pubdate")){
currentState = state_pubdate;
}
else{
currentState = state_unknown;
}

}

@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// TODO Auto-generated method stub
if (localName.equalsIgnoreCase("item")){
feed.addItem(item);
}
}

@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub

String strCharacters = new String(ch,start,length);

if (itemFound==true){
// "item" tag found, it's item's parameter
switch(currentState){
case state_title:
 item.setTitle(strCharacters);
 break;
case state_description:
 item.setDescription(strCharacters);
 break;
case state_link:
 item.setLink(strCharacters);
 break;
case state_pubdate:
 item.setPubdate(strCharacters);
 break;
default:
 break;
}
}
else{
// not "item" tag found, it's feed's parameter
switch(currentState){
case state_title:
 feed.setTitle(strCharacters);
 break;
case state_description:
 feed.setDescription(strCharacters);
 break;
case state_link:
 feed.setLink(strCharacters);
 break;
case state_pubdate:
 feed.setPubdate(strCharacters);
 break;
default:
 break;
}
}

currentState = state_unknown;
}


}

RSSItem.java

public class RSSItem {

private String title = null;
private String description = null;
private String link = null;
private String pubdate = null;

RSSItem(){
}

void setTitle(String value)
{
title = value;
}
void setDescription(String value)
{
description = value;
}
void setLink(String value)
{
link = value;
}
void setPubdate(String value)
{
pubdate = value;
}

String getTitle()
{
return title;
}
String getDescription()
{
return description;
}
String getLink()
{
return link;
}
String getPubdate()
{
return pubdate;
}

@Override
public String toString() {
// TODO Auto-generated method stub
return title;
}
}

ShowDetails.java

public class ShowDetails扩展了Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
 // TODO Auto-generated method stub
 super.onCreate(savedInstanceState);
 setContentView(R.layout.details);
 TextView detailsTitle = (TextView)findViewById(R.id.detailstitle);
 TextView detailsDescription = (TextView)findViewById(R.id.detailspsi);
 TextView detailsPubdate = (TextView)findViewById(R.id.detailspubdate);
 TextView detailsLink = (TextView)findViewById(R.id.detailslink);

 Bundle bundle = this.getIntent().getExtras();

      detailsTitle.setText(bundle.getString("keyTitle"));
      detailsDescription.setText(bundle.getString("keyPsi"));
      detailsPubdate.setText(bundle.getString("keyPubdate"));
      detailsLink.setText(bundle.getString("keyLink"));

}
}

activity_main.xml中

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <!-- Main ListView 
         Always give id value as list(@android:id/list)
    -->
    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

details.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >

<TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:id="@+id/detailstitle"
  android:textSize="20sp" />

<TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:id="@+id/detailspsi"
    android:textSize="18sp" />
<TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:autoLink="web"
  android:id="@+id/detailslink"
    android:textSize="10sp" />
<TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:id="@+id/detailspubdate"
    android:textSize="10sp" />
</LinearLayout>

rsslist.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rowtext"
android:layout_width="fill_parent"
android:layout_height="25px"
android:textSize="10sp" />

0 个答案:

没有答案