Android Open Source - AndroidDev Message List






From Project

Back to project page AndroidDev.

License

The source code is released under:

GNU General Public License

If you think the Android project AndroidDev listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.developerworks.android;
//from   ww w.j  ava2s . co m
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlSerializer;

import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.util.Xml;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MessageList extends ListActivity {
  
  private List<Message> messages;
  
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        loadFeed(ParserType.ANDROID_SAX);
    }
    
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    menu.add(Menu.NONE, ParserType.ANDROID_SAX.ordinal(), 
        ParserType.ANDROID_SAX.ordinal(), R.string.android_sax);
    menu.add(Menu.NONE, ParserType.SAX.ordinal(), ParserType.SAX.ordinal(),
        R.string.sax);
    menu.add(Menu.NONE, ParserType.DOM.ordinal(), ParserType.DOM.ordinal(), 
        R.string.dom);
    menu.add(Menu.NONE, ParserType.XML_PULL.ordinal(), 
        ParserType.XML_PULL.ordinal(), R.string.pull);
    return true;
  }

  @SuppressWarnings("unchecked")
  @Override
  public boolean onMenuItemSelected(int featureId, MenuItem item) {
    super.onMenuItemSelected(featureId, item);
    ParserType type = ParserType.values()[item.getItemId()];
    ArrayAdapter<String> adapter =
      (ArrayAdapter<String>) this.getListAdapter();
    if (adapter.getCount() > 0){
      adapter.clear();
    }
    this.loadFeed(type);
    return true;
  }

  @Override
  protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Intent viewMessage = new Intent(Intent.ACTION_VIEW, 
        Uri.parse(messages.get(position).getLink().toExternalForm()));
    this.startActivity(viewMessage);
  }

  private void loadFeed(ParserType type){
      try{
        Log.i("AndroidNews", "ParserType="+type.name());
        FeedParser parser = FeedParserFactory.getParser(type);
        long start = System.currentTimeMillis();
        messages = parser.parse();
        long duration = System.currentTimeMillis() - start;
        Log.i("AndroidNews", "Parser duration=" + duration);
        String xml = writeXml();
        Log.i("AndroidNews", xml);
        List<String> titles = new ArrayList<String>(messages.size());
        for (Message msg : messages){
          titles.add(msg.getTitle());
        }
        ArrayAdapter<String> adapter = 
          new ArrayAdapter<String>(this, R.layout.row,titles);
        this.setListAdapter(adapter);
      } catch (Throwable t){
        Log.e("AndroidNews",t.getMessage(),t);
      }
    }
    
  private String writeXml(){
    XmlSerializer serializer = Xml.newSerializer();
    StringWriter writer = new StringWriter();
    try {
      serializer.setOutput(writer);
      serializer.startDocument("UTF-8", true);
      serializer.startTag("", "messages");
      serializer.attribute("", "number", String.valueOf(messages.size()));
      for (Message msg: messages){
        serializer.startTag("", "message");
        serializer.attribute("", "date", msg.getDate());
        serializer.startTag("", "title");
        serializer.text(msg.getTitle());
        serializer.endTag("", "title");
        serializer.startTag("", "url");
        serializer.text(msg.getLink().toExternalForm());
        serializer.endTag("", "url");
        serializer.startTag("", "body");
        serializer.text(msg.getDescription());
        serializer.endTag("", "body");
        serializer.endTag("", "message");
      }
      serializer.endTag("", "messages");
      serializer.endDocument();
      return writer.toString();
    } catch (Exception e) {
      throw new RuntimeException(e);
    } 
  }
}




Java Source Code List

nl.componentagro.feedparser.BaseFeedParser.java
nl.componentagro.feedparser.FeedParser.java
nl.componentagro.feedparser.ImageRef.java
nl.componentagro.feedparser.Message.java
nl.componentagro.feedparser.XmlPullFeedParser.java
nl.componentagro.kbreader.CAKennisbankLezerActivity.java
nl.rhoek.mygpspos.GPSPosActivity.java
nl.rhoek.mygpspos.GPSTracker.java
nl.rhoek.mygpspos.util.SystemUiHiderBase.java
nl.rhoek.mygpspos.util.SystemUiHiderHoneycomb.java
nl.rhoek.mygpspos.util.SystemUiHider.java
nl.ronaldhoek.helloandroid.HelloAndroid.java
org.developerworks.android.AndroidSaxFeedParser.java
org.developerworks.android.BaseFeedParser.java
org.developerworks.android.DomFeedParser.java
org.developerworks.android.FeedParserFactory.java
org.developerworks.android.FeedParser.java
org.developerworks.android.MessageList.java
org.developerworks.android.Message.java
org.developerworks.android.ParserType.java
org.developerworks.android.RssHandler.java
org.developerworks.android.SaxFeedParser.java
org.developerworks.android.XmlPullFeedParser.java