Android之ListActivity:布局與資料繫結

來源:互聯網
上載者:User

Android中的列表,當然也可以用ListView來完成所需要的功能,用法是一樣的。
廢話不說,來關鍵的。
LiveActivity本身繼承了關於List操作的眾多介面,我們可以方便的重寫這些操作中需要的方法來實現自己需要的功能。
如果要用ListActivity,則 Activity的Layout檔案中必須包括一個(只能一個)ListView,且ListView的id= "@id/android:list"。
如下代碼,一個標準的ListActivity Layout檔案:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp">

<ListView android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>

<TextView id="@id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF0000"
android:text="No data"/>
</LinearLayout>
請注意 ListView與TextView的id。前面說了,ListView的Id為固定不變的,為”@./idandroid:ost”,ListActivity會根據id自動尋找ListView引用;但如果當ListView中沒有值而又想提示一句話時,那麼用於指定顯示提示資訊的TextView的id 必須為”"@id/android:empty",提示的資訊可以通過android:text進行指定。
OK,關於如何布局說完了,那麼如何給List綁定值,並進行操作呢?
首先我們需要確實的是,ListView的布局也完成了,並通過調用setContentView(…)進行了綁定,但直到現在我們還沒有確定ListView中的第一行顯示的格式是什麼,是直接顯示文字還是要“圖文並茂”的顯示。
Android系統為我們提供了多種模板進行選擇(android.R.layout),如
Ø Simple_list_item_1 每項有一個TextView
Ø Simple_list_item_2 每項有兩個TextView
Ø Simple_list_item_checked 帶CheckView的項
Ø Simple_list_item_multiple_choise 每項有一個TextView並可以多選
Ø Simple_list_item_single_choice 每項有一個TextView,但只能進行單選。
 
但然,如果以上項目範本還無法滿足你的要求,那隻能自訂模板了(相當簡單,就是定義一個layout布局)。如果你做的asp.net的開發的話,是否對dataList控制項有印象呢。如果對DataList有印象,那麼理解ListView也就相當的簡單了。
自訂模板可以根據自己的需要定義成任意的格式,包括圖片、方案及其他可顯示的View,不用多說,自己定義就好了,關鍵是如果使用並進行模板的綁定。
如何要對ListView進行資料繫結,必須使用到一個介面:Adapter。
其中最經常與ListView進行配合使用的有ArrayAdapter、 CursorAdapter及SimpleAdapter等。
從名稱可以看出ArrayAdapter使用的是一個ArrayAdapter做為資料來源,SimpleCursorAdapter使用的是一個Cursor使用資料來源,都比較容易理解,那麼如何使用SimpleAdapter作為資料的適配器呢。Ok,從易到難。
ArrayAdapter:
 
String[] data = { "Item1", "Item2",
        "Item3", "Item4", "Item5" };
listView.setAdapter(new ArrayAdapter<String>(this,
     android.R.layout.simple_list_item_single_choice, data)); 
 
SimpleCursorAdapter:
//從資料庫中查詢Cursor
   cursor = adapter.getAllNotes();
   startManagingCursor(cursor);
 
   //設定要顯示的資料來源中的列名(需要包含在cursor中)
   String[] from = new String[] { DiaryDbAdapter.KEY_COLUMN_TITLE,
                DiaryDbAdapter.KEY_COLUMN_CREATEED };
 
   //顯示的View(自訂模板中的View)
   int[] to = new int[] { R.id.txtRowTitle, R.id.txtRowCreateed };
   //綁定
   SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
                R.layout.diaryrow, cursor, from, to);
   setListAdapter(notes);、
SimpleAdapter:
   SimpleAdapter將一個List做為資料來源,可以讓ListView進行更加個人化的顯示。而List中的第一項是個Map<String,?>(用到泛型),其中Map中的每項將與ListView中的每項進行一一對應綁定。Ok,看一下構造:
   SimpleAdapter(Context context,List<? Extends Map<String,?>> data,int resource,String [] form, int [] to);
² Context:當前上下文,一般把Activity.this傳遞進行。
² Data: 資料來源。
² Resource: 自訂的layout模板資源,可以用 R.layout.xxx擷取引用。
² Form: 定義ListView中的每一項資料索引,索引來自於Map<String,?>,即指定要顯示的內容。
² To:View數組,在ListView模板中的定義View,與Form中需要一一對應。
案例代碼:
      List<Hashtable<String, Object>> listContent
= new ArrayList<Hashtable<String, Object>>();
 
      for (int i = 0; i < deviceList.size(); i++) {
         Hashtable<String, Object> table
= new Hashtable<String, Object>();
         table.put("name", deviceList.get(i).Name);
         table.put("address", deviceList.get(i).Address);
         table.put("type", deviceList.get(i).Type + ""); 
 
         listContent.add(table);
      }
 
      adapter = new SimpleAdapter(HeartActivity.this,
listContent, R.layout.child, //自訂的layout
new String[] { "name", "address" },
new int[] {R.id.txtDeviceName, R.id.txtDeviceAddress });
 
      setListAdapter(adapter);
以上代碼使用了Hashtable做為一個Map,並添加到一個List<Hashtable<String, Object>>當中。
之後new一個SimpleAdapter,注意SimpleAdapter是如何產生的。
作者:by317966834

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.