ç°¡åãªRSSãªã¼ãã¼ãä½ã£ã¦ã¿ã
第4回 簡単なRSSリーダーを作ってみる
第5回 RSSリーダーの要、パース機能を知る
第6回 詳細画面を付けて、簡易RSSリーダーの完成
ã¨ããè¨äºããã£ã¦ãã³ã¼ããã¾ã¨ãã¦ã¿ã¦åããã¦ã¿ãããã¨ã
XMLã®åä¿¡ããã¼ã¹ã表示ã®æµãã¯ã¯ã©ã¦ãã¢ããªã§ã¯å¿
ãå¿
è¦ã ã¨æãã®ã§ã
ãã¡ã¤ã«æ§æ
rssreader |-- AndroidManifest.xml |-- bin |-- build.properties |-- build.xml |-- default.properties |-- libs |-- local.properties |-- res | |-- layout | | |-- main.xml | | `-- item_row.xml | `-- values | `-- strings.xml `-- src `-- com `-- sample `-- rssreader |-- RssReader.java (*RssReaderActivity.java) |-- RssListAdapter.java |-- RssParserTask.java |-- Item.java `-- ItemDetailActivity.java
AndroidManifest.xml
ã¢ã¯ãã£ããã£ã¯2ã¤ã許å¯ãããã¼ããã·ã§ã³ã¯ã¤ã³ã¿ã¼ãããæ¥ç¶ã®ã¿ã
ã¢ã¤ã³ã³ã¯ç»åããªãã®ã§æå®ããªãã
SDKã®ãã¼ã¸ã§ã³ã¯1.6以ä¸ã¨ããã
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.sample.rssreader" android:versionCode="1" android:versionName="1.0"> <application android:label="@string/app_name"> <activity android:name=".RssReader" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".ItemDetailActivity" /> </application> <uses-sdk android:minSdkVersion="4" /> <uses-permission android:name="android.permission.INTERNET" /> </manifest>
res/layout/main.xml
ã¡ã¤ã³ã¢ã¯ãã£ããã£ã®ãªã¹ããã¥ã¼ã®ã¬ã¤ã¢ã¦ãã
ããããã®ãªã¹ãã²ã¨ã¤ã²ã¨ã¤ã¯ãitem_row.xmlã§ã
<?xml version="1.0" encoding="utf-8"?> <!-- mainã¢ã¯ãã£ããã£ç¨ --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <!-- ãªã¹ããã¥ã¼ --> <ListView android:id="@android:id/list" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <!-- ãªã¹ãã空ã®ã¨ãã«è¡¨ç¤º --> <TextView android:id="@android:id/empty" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
res/layout/item_row.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/item_container" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> <TextView android:id="@+id/item_title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#ffffff" android:textSize="18sp" android:lines="1" /> <TextView android:id="@+id/item_descr" android:layout_width="fill_parent" android:layout_height="wrap_content" android:lines="2" android:ellipsize="end" /> </LinearLayout>
src/com/sample/rssreader/RssReader.java
package com.sample.rssreader; import android.os.Bundle; import android.app.Activity; import android.app.ListActivity; import android.content.Intent; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.widget.ListView; import java.util.ArrayList; public class RssReader extends ListActivity { private static final String RSS_FEED_URL = "http://itpro.nikkeibp.co.jp/rss/ITpro.rdf"; public static final int MENU_ITEM_RELOAD = Menu.FIRST; private RssListAdapter mAdapter; private ArrayList<Item> mItems; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Itemãªãã¸ã§ã¯ããä¿æããããã®ãªã¹ããçæããã¢ããã¿ã«è¿½å ãã mItems = new ArrayList<Item>(); mAdapter = new RssListAdapter(this, mItems); // ã¿ã¹ã¯ãèµ·åãã RssParserTask task = new RssParserTask(this, mAdapter); task.execute(RSS_FEED_URL); } // ãªã¹ãã®é ç®ãé¸æããæã®å¦ç @Override protected void onListItemClick(ListView l, View v, int position, long id) { Item item = mItems.get(position); Intent intent = new Intent(this, ItemDetailActivity.class); intent.putExtra("TITLE", item.getTitle()); intent.putExtra("DESCRIPTION", item.getDescription()); startActivity(intent); } // MENUãã¿ã³ãæ¼ããã¨ãã®å¦ç @Override public boolean onCreateOptionsMenu(Menu menu) { boolean result = super.onCreateOptionsMenu(menu); // ããã©ã«ãã§ã¯ã¢ã¤ãã ã追å ããé çªéãã«è¡¨ç¤ºãã menu.add(0, MENU_ITEM_RELOAD, 0, "æ´æ°"); return result; } // MENUã®é ç®ãæ¼ããã¨ãã®å¦ç @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { // æ´æ° case MENU_ITEM_RELOAD: // ã¢ããã¿ãåæåããã¿ã¹ã¯ãèµ·åãã mItems = new ArrayList(); mAdapter = new RssListAdapter(this, mItems); // ã¿ã¹ã¯ã¯ãã®é½åº¦çæãã RssParserTask task = new RssParserTask(this, mAdapter); task.execute(RSS_FEED_URL); return true; } return super.onOptionsItemSelected(item); } }
src/com/sample/rssreader/RssListAdapter.java
package com.sample.rssreader; import android.view.View; import android.view.ViewGroup; import android.content.Context; import android.widget.TextView; import android.widget.ArrayAdapter; import android.view.LayoutInflater; import java.util.List; public class RssListAdapter extends ArrayAdapter<Item> { private LayoutInflater mInflater; private TextView mTitle; private TextView mDescr; public RssListAdapter(Context context, List<Item> objects) { super(context, 0, objects); mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } // 1è¡ãã¨ã®ãã¥ã¼ãçæãã @Override public View getView(int position, View convertView, ViewGroup parent) { View view = convertView; if (convertView == null) { view = mInflater.inflate(R.layout.item_row, null); } // ç¾å¨åç §ãã¦ãããªã¹ãã®ä½ç½®ããItemãåå¾ãã Item item = this.getItem(position); if (item != null) { // Itemããå¿ è¦ãªãã¼ã¿ãåãåºããããããTextViewã«ã»ãããã String title = item.getTitle().toString(); mTitle = (TextView) view.findViewById(R.id.item_title); mTitle.setText(title); String descr = item.getDescription().toString(); mDescr = (TextView) view.findViewById(R.id.item_descr); mDescr.setText(descr); } return view; } }
src/com/sample/rssreader/RssParserTask.java
package com.sample.rssreader; import android.os.AsyncTask; import android.app.ProgressDialog; import android.util.Xml; import java.io.InputStream; import java.io.IOException; import java.net.URL; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; public class RssParserTask extends AsyncTask<String, Integer, RssListAdapter> { private RssReader mActivity; private RssListAdapter mAdapter; private ProgressDialog mProgressDialog; // ã³ã³ã¹ãã©ã¯ã¿ public RssParserTask(RssReader activity, RssListAdapter adapter) { mActivity = activity; mAdapter = adapter; } // ã¿ã¹ã¯ãå®è¡ããç´å¾ã«ã³ã¼ã«ããã @Override protected void onPreExecute() { // ããã°ã¬ã¹ãã¼ã表示ãã mProgressDialog = new ProgressDialog(mActivity); mProgressDialog.setMessage("Now Loading..."); mProgressDialog.show(); } // ããã¯ã°ã©ã¦ã³ãã«ãããå¦çãæ ããã¿ã¹ã¯å®è¡æã«æ¸¡ãããå¤ãå¼æ°ã¨ãã @Override protected RssListAdapter doInBackground(String... params) { RssListAdapter result = null; try { // HTTPçµç±ã§ã¢ã¯ã»ã¹ããInputStreamãåå¾ãã URL url = new URL(params[0]); InputStream is = url.openConnection().getInputStream(); result = parseXml(is); } catch (Exception e) { e.printStackTrace(); } // ããã§è¿ããå¤ã¯ãonPostExecuteã¡ã½ããã®å¼æ°ã¨ãã¦æ¸¡ããã return result; } // ã¡ã¤ã³ã¹ã¬ããä¸ã§å®è¡ããã @Override protected void onPostExecute(RssListAdapter result) { mProgressDialog.dismiss(); mActivity.setListAdapter(result); } // XMLããã¼ã¹ãã public RssListAdapter parseXml(InputStream is) throws IOException, XmlPullParserException { XmlPullParser parser = Xml.newPullParser(); try { parser.setInput(is, null); int eventType = parser.getEventType(); Item currentItem = null; while (eventType != XmlPullParser.END_DOCUMENT) { String tag = null; switch (eventType) { case XmlPullParser.START_TAG: tag = parser.getName(); if (tag.equals("item")) { currentItem = new Item(); } else if (currentItem != null) { if (tag.equals("title")) { currentItem.setTitle(parser.nextText()); } else if (tag.equals("description")) { currentItem.setDescription(parser.nextText()); } } break; case XmlPullParser.END_TAG: tag = parser.getName(); if (tag.equals("item")) { mAdapter.add(currentItem); } break; } eventType = parser.next(); } } catch (Exception e) { e.printStackTrace(); } return mAdapter; } }
src/com/sample/rssreader/Item.java
package com.sample.rssreader; public class Item { // è¨äºã®ã¿ã¤ãã« // è¨äºã®æ¬æ private CharSequence mTitle; private CharSequence mDescription; public Item() { mTitle = ""; mDescription = ""; } public CharSequence getDescription() { return mDescription; } public void setDescription(CharSequence description) { mDescription = description; } public CharSequence getTitle() { return mTitle; } public void setTitle(CharSequence title) { mTitle = title; } }
src/com/sample/rssreader/ItemDetailActivity.java
package com.sample.rssreader; import android.os.Bundle; import android.app.Activity; import android.widget.TextView; import android.widget.ArrayAdapter; import android.content.Intent; public class ItemDetailActivity extends Activity { private TextView mTitle; private TextView mDescr; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.item_detail); Intent intent = getIntent(); String title = intent.getStringExtra("TITLE"); mTitle = (TextView) findViewById(R.id.item_detail_title); mTitle.setText(title); String descr = intent.getStringExtra("DESCRIPTION"); mDescr = (TextView) findViewById(R.id.item_detail_descr); mDescr.setText(descr); } }