ListView學習筆記之一

listview的顯示需要三個元素:


1.ListVeiw 用來展示列表的View。

2.適配器 用來把數據映射到ListView上的中介。

3.數據    具體的將被映射的字符串,圖片,或者基本組件。

根據列表的適配器類型,列表分爲三種,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter

一\ ArrayAdapter

java.lang.Object
   ↳ android.widget.BaseAdapter
    ↳ android.widget.ArrayAdapter<T>
extends BaseAdapter
implements Filterable

類簡介
默認的的ResourceID爲TextView,若要用其他視圖組建,重寫getView(int, View, ViewGroup),返回所需要的視圖

舉例
public ArrayAdapter(Context context, int textViewResourceId, List<T> objects)
Parameters
context The current context.
textViewResourceId The resource ID for a layout file containing a TextView to use when instantiating views.
objects The objects to represent in the ListView.

public class MyListView extends Activity {
 
    private ListView listView;
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
         
        listView = new ListView(this);       
        //android.R.layout.simple_list_item_1是系統定義好的佈局文件只顯示一行文字
        listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,getData()));
        setContentView(listView);
    }

    private List<String> getData(){
         
        List<String> data = new ArrayList<String>();
        data.add("測試數據1");
        data.add("測試數據2");
        data.add("測試數據3");
        data.add("測試數據4");         
        return data;
    }
}


二、CursorAdapter
通過遊標獲得數據後,在listview中顯示結果。
Cursor cursor = getContentResolver().query(People.CONTENT_URI, null, null, null, null);先獲得一個指向系統通訊錄數據庫的Cursor對象獲得數據來源。
 startManagingCursor(cursor);我們將獲得的Cursor對象交由Activity管理,這樣Cursor的生命週期和Activity便能夠自動同步。
 SimpleCursorAdapter 構造函數前面3個參數和ArrayAdapter是一樣的,最後兩個參數:一個包含數據庫的列的String型數組,一個包含佈局文件中對應組件id的int型數組。其作用是自動的將String型數組所表示的每一列數據映射到佈局文件對應id的組件上。下面的代碼,將NAME列的數據一次映射到佈局文件的id爲text1的組件上。


注意:需要在AndroidManifest.xml中如權限:<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>

SimpleCursorAdapter
extends ResourceCursorAdapter
java.lang.Object
   ↳ android.widget.BaseAdapter
    ↳ android.widget.CursorAdapter
    ↳android.widget.ResourceCursorAdapter
    ↳android.widget.SimpleCursorAdapter

函數簡介
public SimpleCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to)
Parameters
context    The context where the ListView associated with this SimpleListItemFactory is running
layout    resource identifier of a layout file that defines the views for this list item. The layout file should include at least those named views defined in "to"
c The database cursor. Can be null if the cursor is not available yet.
from A list of column names representing the data to bind to the UI. Can be null if the cursor is not available yet.
to The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter. Can be null if the cursor is not available yet.

舉例

public class MainActivity extends Activity {
 
    private ListView listView;
@Override
    public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
listView = new ListView(this);

Cursor cursor = getContentResolver().query(People.CONTENT_URI, null, null, null, null);
startManagingCursor(cursor);

ListAdapter listAdapter = new SimpleCursorAdapter(this,  android.R.layout.simple_expandable_list_item_1, cursor,
new String[] { People.NAME }, new int[] { android.R.id.text1 });
listView.setAdapter(listAdapter);
setContentView(listView);
    }
*CursorAdapter 常與數據庫查詢配合使用,獲得的數據結果顯示在listview中。

如果將simplecursoradapter改成這樣:

ListAdapter listAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_expandable_list_item_2, cursor,
new String[] { People.NAME, People.NAME }, new int[] { android.R.id.text1,android.R.id.text2 });

則會顯示android.R.layout.simple_expandable_list_item_2對應格式的list。



整理來自: http://blog.csdn.net/x359981514/article/details/8120731

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章