Android Open Source - wxalert Message List






From Project

Back to project page wxalert.

License

The source code is released under:

Apache License

If you think the Android project wxalert 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 com.creatuity.wxalert;
//from   w w  w. jav a  2  s.c  o 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

com.creatuity.wxalert.Alert.java
com.creatuity.wxalert.AndroidSaxFeedParser.java
com.creatuity.wxalert.BaseFeedParser.java
com.creatuity.wxalert.DomFeedParser.java
com.creatuity.wxalert.FeedParserFactory.java
com.creatuity.wxalert.FeedParser.java
com.creatuity.wxalert.MessageList.java
com.creatuity.wxalert.Message.java
com.creatuity.wxalert.ParserType.java
com.creatuity.wxalert.RssHandler.java
com.creatuity.wxalert.SaxFeedParser.java
com.creatuity.wxalert.WeatherAlert.java
com.creatuity.wxalert.XmlPullFeedParser.java