這個學期學校開了安卓的課程,因為自己一直學習wp的開發,一直用的是.net和Silverlight這一套,也著實沒有太多時間投入安卓的方向去,因為想著畢業也不從事安卓的工作,所以也一直沒有怎麼研究。但是期末了,要交作品了,我想不如就做個RSS閱讀器交吧,因為在學習wp的時候覺得RSS閱讀器還是相對簡單的,安卓上應該也是用的一樣的思路。所以昨天晚上,在我們宿舍安卓大神的協助下(主要是解決一些wp和安卓不同的地方帶給我的疑問),我花了4個多小時做了這個應用。
總體思路是擷取IT之家的RSS源,儲存到一個集合裡,然後把資料繫結到ListView上,點擊ListView的其中一項,跳轉到另一個頁面,該頁面只有一個WebView,用來顯示點擊項帶過來的網址所對應的網頁——當然wp裡最簡單的RSS閱讀器也是這個思路。
首先定義一個文章的模型:
public class Item { private String title; private String description; private String date; private String link; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getLink() { return link; } public void setLink(String link) { this.link = link; } public HashMap<String,String> getMap(){ HashMap<String, String> map = new HashMap<String, String>(); map.put("title", title); map.put("description", description); map.put("date", date); map.put("link", link); return map; }}
最後一個方法getMap的作用是把當前對象的屬性以HashMap的形式返回。為什麼要這麼做呢?因為後面要用到。
擷取RSS源的實現方法:
public static List<Map<String, String>> readXML(InputStream inStream) { List<Map<String, String>> data = new ArrayList<Map<String, String>>(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder builder = factory.newDocumentBuilder(); Document dom = builder.parse(inStream); Element root = dom.getDocumentElement(); NodeList items = root.getElementsByTagName("item");// 尋找所有item節點 for (int i = 0; i < items.getLength(); i++) { Item item = new Item(); // 得到第一個item節點 Element itemNode = (Element) items.item(i); // 擷取item節點下的所有子節點 NodeList childsNodes = itemNode.getChildNodes(); for (int j = 0; j < childsNodes.getLength(); j++) { Node node = (Node) childsNodes.item(j); // 判斷是否為元素類型 if (node.getNodeType() == Node.ELEMENT_NODE) { Element childNode = (Element) node; if ("title".equals(childNode.getNodeName())) { item.setTitle(childNode.getFirstChild() .getNodeValue()); } else if ("link".equals(childNode.getNodeName())) { String computer = childNode.getFirstChild() .getNodeValue(); String phone = "http://wap.ithome.com/html" + computer.substring(computer .lastIndexOf("/")); Log.e("jpho", phone); item.setLink(phone); } else if ("description" .equals(childNode.getNodeName())) { item.setDescription(childNode.getFirstChild() .getNodeValue()); } else if ("pubDate".equals(childNode.getNodeName())) { item.setDate(childNode.getFirstChild() .getNodeValue()); } } } data.add(item.getMap()); } inStream.close(); } catch (Exception e) { e.printStackTrace(); } return data; }
這個方法把擷取到的RSS資料添加到一個List集合裡,裡面就用到了上面提到的getMap方法。這裡做了一個小小的處理,就是把擷取到的文章連結轉換成手機版的連結。畢竟要在手機上看,一來載入快,二來節省流量。
下面是邏輯:
String[] from = new String[] { "title", "date", "description", "link" }; int[] to = new int[] { R.id.title, R.id.date, R.id.description, R.id.link }; URL url; List<Map<String, String>> data = new ArrayList<Map<String, String>>(); try { url = new URL("http://www.ithome.com/rss/"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); InputStream isr = conn.getInputStream(); data = readXML(isr); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item, from, to); this.getListView().setAdapter(adapter); this.getListView().setOnItemClickListener(this);
這裡的資料繫結跟wp的寫法有點不同,這裡是通過適配器來把屬性綁定到相應控制項裡,在wp裡我們直接寫Bingding就可以了。getListView().setAdapter(adatper)跟wp裡的設定資料內容有點類似,不過這裡是設定資料配接器。
接下來是Item的點擊事件:
@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub ListView listView = (ListView) parent; HashMap<String, String> map = (HashMap<String, String>) listView .getItemAtPosition(position); String url = map.get("link"); Intent intent = new Intent(getApplicationContext(), ItemActivity.class); intent.putExtra("url", url); startActivity(intent); }
Intent有點像是wp裡的應用程式設定,也是通過索引值對得方式把資料儲存到隔離儲存區 (Isolated Storage)空間裡,然後在別的頁面擷取它。下面是文章頁面的邏輯:
@SuppressLint("SetJavaScriptEnabled")public class ItemActivity extends Activity { private WebView mWebView; @Override protected void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.activity_item); Intent intent = getIntent(); mWebView = (WebView) findViewById(R.id.webView); mWebView.getSettings().setJavaScriptEnabled(true); mWebView.loadUrl(intent.getStringExtra("url")); }}
要使WebView能載入js,就要添加這句話:@SuppressLint("SetJavaScriptEnabled"),還有這句:mWebView.getSettings().setJavaScriptEnabled(true),然後在onCreate方法裡擷取傳過來的連結,並用WebView顯示之。
這就是總體思路。畢竟是第一次寫安卓應用,寫得不好之處還望指正。不過真的跟wp的思路是一樣的,所以只要掌握了一種移動開發方法,轉向其他平台只是語言問題,還有一些不一樣的特性,主要思路還是一摸一樣的。不過以後不打算從事安卓開發,這可能是第一次,也是最後一次開發安卓的應用。僅以此文紀念那些還未開始便結束的東西。
有需要的可以在這裡下載項目代碼。