Google Code Prettify - 輕量級的語法上色工具

顯示具有 Android 標籤的文章。 顯示所有文章
顯示具有 Android 標籤的文章。 顯示所有文章

星期二, 8月 04, 2015

Context 的使用 (Activity Context, Application Context, getBaseContext.....)

In a regular Android application, you usually have two kinds of Context, Activity and Application.

1.Activity Context
this.getContext()= Activity.this 屬於Activity,Activity摧毀他就摧毀

  • View.getContext(): Returns the context the view is currently running in. Usually the currently active Activity.


2.Application Context
 getApplicationContext()
生命週期是整個app應用,app摧毀它才摧毀,
但是使用的時候要注意,和activity生命週期掛鉤的任務不要使用該context,比如網絡訪問,防止內存洩露

  • Activity.getApplicationContext(): Returns the context for the entire application (the process all the Activities are running inside of). Use this instead of the current Activity context if you need a context tied to the lifecycle of the entire application, not just the current Activity.


3.Don't use getBaseContext(), just use the Context you have.
getBaseContext()一般情况下不推荐使用这种方法


  • ContextWrapper.getBaseContext(): If you need access to a Context from within another context, you use a ContextWrapper. The Context referred to from inside that ContextWrapper is accessed via getBaseContext().

4.getActivity():在fragment和Notification中會使用到
Notification http://nkeegamedev.blogspot.tw/2010/06/android-notification.html
fragment http://developer.android.com/reference/android/app/Fragment.html

5.getParent() 

6.this








星期一, 8月 03, 2015

Android Adapter

ArrayAdapter:將一組array連繫到ListView 支持泛型操作,只能展示一行字。
SimpleAdapter:適用於自訂ListView各種外觀的應用場合。可加圖。
BaseAdapter:抽象類別。適用於需要自訂ListView外觀等靈活應用的場合。可加按鈕。
SimpleCursorAdapter:回傳Cursor。資料庫或聯絡人等ContentProvider的資料查詢的結果連繫到ListView中。

android.R.layout.simple_list_item_1:一行text
android.R.layout.simple_list_item_2:一行text較大,一行text較小。
android.R.layout.simple_list_item_single_choice:單選
android.R.layout.simple_list_item_multiple_choice:多選按鈕
android.R.layout.simple_list_item_checked:勾選盒