Skip to content

Instantly share code, notes, and snippets.

@IceskYsl
Created January 12, 2009 03:20
Show Gist options
  • Save IceskYsl/45866 to your computer and use it in GitHub Desktop.
Save IceskYsl/45866 to your computer and use it in GitHub Desktop.
package com.yobo.iceskysl;
import java.net.URL;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import com.yobo.iceskysl.album.AlbumFeed;
import com.yobo.iceskysl.album.AlbumItem;
import com.yobo.iceskysl.album.AlbumItemHandler;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class Albums extends Activity implements OnItemClickListener {
public final String ALL_ALBUMS = "http://1stlog.1sters.com/albums.xml";
// Identifiers for option menu items
private static final int ALBUM_SONGES = Menu.FIRST + 1;
private static final int ALBUM_INFOS = ALBUM_SONGES + 1;
private static final int ALBUM_BACK = ALBUM_INFOS + 1;
public final String tag = "Albums";
private AlbumFeed feed = null;
/** Called when the activity is first created. */
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.albums);
// go get our feed!
feed = getFeed(ALL_ALBUMS);
// display UI
UpdateDisplay();
}
private AlbumFeed getFeed(String urlToRssFeed) {
try {
// setup the url
URL url = new URL(urlToRssFeed);
// create the factory
SAXParserFactory factory = SAXParserFactory.newInstance();
// create a parser
SAXParser parser = factory.newSAXParser();
// create the reader (scanner)
XMLReader xmlreader = parser.getXMLReader();
// instantiate our handler
AlbumItemHandler albumHandler = new AlbumItemHandler();
// assign our handler
xmlreader.setContentHandler(albumHandler);
// get our data via the url class
InputSource is = new InputSource(url.openStream());
// perform the synchronous parse
xmlreader.parse(is);
// get the results - should be a fully populated RSSFeed instance,
// or null on error
return albumHandler.getFeed();
} catch (Exception ee) {
// if we have a problem, simply return null
return null;
}
}
// 初始化菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, ALBUM_SONGES, 0, R.string.album_songs)
.setIcon(R.drawable.ic_menu_notifications)
.setAlphabeticShortcut('S');
menu.add(0, ALBUM_INFOS, 0, R.string.album_infos).setIcon(
android.R.drawable.ic_menu_gallery).setAlphabeticShortcut('A');
menu.add(0, ALBUM_BACK, 0, R.string.menu_back).setIcon(
R.drawable.setting).setAlphabeticShortcut('I');
return true;
}
// 当一个菜单被选中的时候调用
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent = new Intent();
switch (item.getItemId()) {
case ALBUM_SONGES:
// intent.setClass(Album.this, YoboPlayer.class);
Intent itemintent = new Intent(this,PlayList.class);
Bundle album = new Bundle();
// album.putString("id", album_id);
// album.putString("name", album_name);
// album.putString("url", album_url);
// album.putString("pic", album_pic);
itemintent.putExtra("android.intent.extra.album", album);
startActivityForResult(itemintent, 0);
// startActivity(intent);
return true;
case ALBUM_INFOS:
intent.setClass(Albums.this, Album.class);
startActivity(intent);
return true;
case ALBUM_BACK:
finish();
break;
}
return true;
}
private void UpdateDisplay() {
TextView feedtitle = (TextView) findViewById(R.id.feedtitle);
ListView itemlist = (ListView) findViewById(R.id.itemlist);
if (feed == null) {
feedtitle.setText("No Album Available");
return;
}
ArrayAdapter<AlbumItem> adapter = new ArrayAdapter<AlbumItem>(this,
android.R.layout.simple_list_item_1, feed.getAllItems());
itemlist.setAdapter(adapter);
itemlist.setOnItemClickListener(this);
itemlist.setSelection(0);
}
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Log.i(tag, "item clicked! [" + feed.getItem(position).getName() + "]");
Intent itemintent = new Intent(this, Album.class);
Bundle album = new Bundle();
album.putString("id", feed.getItem(position).getId());
album.putString("name", feed.getItem(position).getName());
album.putString("introduction", feed.getItem(position).getIntro());
album.putString("url", feed.getItem(position).getUrl());
album.putString("pic", feed.getItem(position).getPic());
album.putString("publish_date", feed.getItem(position).getPubDate());
album.putString("publish_company", feed.getItem(position).getPubcomp());
itemintent.putExtra("android.intent.extra.album", album);
startActivityForResult(itemintent, 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment