這個學期學校開了安卓的課程,因為自己一直學習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里的應用程序設置,也是通過鍵值對得方式把數據保存到獨立存儲空間里,然后在別的頁面獲取它。下面是文章頁面的邏輯:
@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的思路是一樣的,所以只要掌握了一種移動開發方法,轉向其他平台只是語言問題,還有一些不一樣的特性,主要思路還是一摸一樣的。不過以后不打算從事安卓開發,這可能是第一次,也是最后一次開發安卓的應用。僅以此文紀念那些還未開始便結束的東西。
有需要的可以在這里下載項目代碼。
