TShopping

 找回密碼
 註冊
搜索
查看: 1892|回復: 0
打印 上一主題 下一主題

[教學] Android ArrayAdapter 用法

[複製鏈接]
跳轉到指定樓層
1#
發表於 2016-6-30 21:30:48 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
 
Push to Facebook
許多mobile程式中,常常會看到排列整齊的選單,有可能是接收RSS或WebService之後的列表,或是提供使用者選擇所需項目,而這類方便的介面許多時候更是貫串整個APP的重要功臣,例如我們常常會打開的設定列表等等。

在Android當中,要做出列表般的效果,都會藉由android.widget.Adapter的相關子類別來實現,而在很多的應用當中,其中有一種"單純只想把一類項目列出來",這時可以利用ArrayAdapter達成。

整個事情要完成只有3個步驟:
1. 在layout裡面放一個ListView。

  1. <ListView    android:id="@+id/listview1"
  2. android:layout_width="fill_parent"
  3. android:layout_height="wrap_content"
  4. />
複製代碼


2. 將ListView實體化

  1. ListView Olalist = (ListView) this.findViewById(R.id.listview1);
複製代碼


3. 於要放置列表的事件中執行setAdapter。

String[] item = new String[] {"Ola的家", "魔獸世界","星海爭霸2","凱蘭迪亞傳奇","Ola Query簡介","蟲族秒滅心法","Ola MapGuide教學","Ola jQuery教學","Ola Android教學"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,item);
Olalist.setAdapter(adapter);

上方程式碼有兩個重點:
A. ArrayAdapte所接收之項目為Array,之所以前面說「"單純只想把一類項目列出來"」的原因也在這裡,ArrayAdapte的方法裡面並沒有提供多欄位的顯示方式。
B. 在第二個參數中使用"android.R.layout.simple_expandable_list_item_1",表示是用Android內建的配置。
執行結果:


單一項目是屬於ArrayAdapte的限制,但去看ArrayAdapter的說明,可以發現他也可以使用我們自己定義的配置檔;若是使用自己的配置檔(layout),那麼就必須去指定要擺放文字的textViewResourceId。
自訂的Layout:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="horizontal"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >

  7.    <ImageView
  8.        android:id="@+id/ImageView_icon"
  9.        android:layout_width="wrap_content"
  10.        android:layout_height="wrap_content"
  11.        android:layout_centerVertical="true"
  12.        android:layout_marginLeft="10px"
  13.        android:src="@drawable/main_new"
  14.    />

  15.    <CheckedTextView android:id="@+id/listTextView1"
  16.    xmlns:android="http://schemas.android.com/apk/res/android"
  17.    android:layout_width="wrap_content"
  18.    android:layout_height="wrap_content"
  19.    android:textColor="@drawable/green"
  20.    >
  21.    </CheckedTextView>
  22.    <CheckedTextView android:id="@+id/listTextView2"
  23.    xmlns:android="http://schemas.android.com/apk/res/android"
  24.    android:layout_width="wrap_content"
  25.    android:layout_height="wrap_content"
  26.    android:textColor="@drawable/green"
  27.    android:text="-----自訂"
  28.    >
  29.    </CheckedTextView>
  30. </LinearLayout>
複製代碼

也就是前面擺放一張固定的圖,在顯示的內容後面加上"-----自訂"文字。
綁定的程式碼:

  1. String[] item = new String[] {"Ola的家", "魔獸世界","星海爭霸2","凱蘭迪亞傳奇","Ola Query簡介","蟲族秒滅心法","Ola MapGuide教學","Ola jQuery教學","Ola Android教學"};
  2. ArrayAdapter adapter = new ArrayAdapter(this, R.layout.sample_list,R.id.listTextView1,item);
  3. Olalist.setAdapter(adapter);
複製代碼


執行結果:

如果看到上面的內容,想到的事情應該是:
A. 圖片可以依照項目不同嗎?
B. 第二個文字可以也直接用帶入的嗎?

答案是當然可以,但是使用ArrayAdapter可能不是最佳選擇,因為他一次綁定的數據為一個Array,對於其他內容的顯示可能就不是這麼方便,可是一切都還是可以藉由改寫的方式完成。

出處

 

臉書網友討論
*滑块验证:
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-4-26 17:46 , Processed in 0.066697 second(s), 25 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表