2015年6月27日 星期六

[Android] ListView - 基本用法

ListView是一種相當常用的Layout,可以用來顯示許多的資訊,

這裡會介紹ListView的基本用法。

首先你可以在你的Activity裡面加入一個ListView的元件。


程式碼如下:

listView = (ListView) findViewById(R.id.listView);
//找到ListView
String[] values = new String[]{
"Apple",
"Banana",
"Cat",
"Dog"
};
//丟入你要顯示的文字
ListAdapter adapter = new ArrayAdapter<>(this , android.R.layout.simple_list_item_1 ,values);
//使用ListAdapter來顯示你輸入的文字
listView.setAdapter(adapter);
//將ListAdapter設定至ListView裡面
view raw gistfile1.java hosted with ❤ by GitHub

中間可能沒有什麼太大的問題,只是在New Adapter的時候,丟了三個參數進去可能有些疑惑。

第一個是丟Context,第三個是丟字串陣列,看起來都還好,

但第二個丟了一個 android.R.layout.simple_list_item_1 是什麼意思呢?

事實上這是Android內建的Layout,專門用於ListView的item的,它有提供許多種樣式,

我們實際輸入看看會有哪些樣式可以選擇。

simple_list_item_1
基礎中的基礎,只有一行文字顯示。














接下來的幾個,因為可以讓使用者選擇要不要勾選,要多設定一個屬性。

詳細的程式碼如下。

String[] values = new String[]{
"Apple",
"Banana",
"Cat",
"Dog"
};
//丟入你要顯示的文字
ListAdapter adapter = new ArrayAdapter<>(this , android.R.layout.simple_list_item_checked ,values);
//使用ListAdapter來顯示你輸入的文字
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
//設定選擇的模式
listView.setAdapter(adapter);
//將ListAdapter設定至ListView裡面
view raw gistfile1.java hosted with ❤ by GitHub

setChoiceMode

可以設定許多種類型,可以自己試試看要哪種。


/**
* Normal list that does not indicate choices
*/
public static final int CHOICE_MODE_NONE = 0;
/**
* The list allows up to one choice
*/
public static final int CHOICE_MODE_SINGLE = 1;
/**
* The list allows multiple choices
*/
public static final int CHOICE_MODE_MULTIPLE = 2;
/**
* The list allows multiple choices in a modal selection mode
*/
public static final int CHOICE_MODE_MULTIPLE_MODAL = 3;
view raw gistfile1.java hosted with ❤ by GitHub

而只要把Layout丟入的值改變,會有以下幾種不同的選單。

simple_list_item_checked
CheckBox的樣式,可以打勾之類的。















simple_list_item_multiple_choice
多選選單。














simple_list_item_single_choice
單選選單。















simple_list_item_2
有兩行文字,一大一小,這時候你必須修改程式碼才能使用,因為ArrayAdapter沒辦法傳入多個值,因此要改使用SimpleAdapter來填充ListView裡面的屬性。

而他要丟的參數比較多一點,詳細程式碼可以參考下面。

listView = (ListView) findViewById(R.id.listView);
List<HashMap<String , String>> list = new ArrayList<>();
//使用List存入HashMap,用來顯示ListView上面的文字。
String[] title = new String[]{"Apple" , "Banana" , "Cat" , "Dog"};
String[] text = new String[]{"蘋果" , "香蕉" , "貓" , "狗"};
for(int i = 0 ; i < title.length ; i++){
HashMap<String , String> hashMap = new HashMap<>();
hashMap.put("title" , title[i]);
hashMap.put("text" , text[i]);
//把title , text存入HashMap之中
list.add(hashMap);
//把HashMap存入list之中
}
ListAdapter listAdapter = new SimpleAdapter(
this,
list,
android.R.layout.simple_list_item_2 ,
new String[]{"title" , "text"} ,
new int[]{android.R.id.text1 , android.R.id.text2});
// 5個參數 : context , List , layout , key1 & key2 , text1 & text2
listView.setAdapter(listAdapter);
view raw gistfile1.java hosted with ❤ by GitHub

要丟五個參數,Context、List、Layout、Key1&Key2、Text1&Text2

前幾個應該還算好理解,List就丟入一個包有HashMap的List就可以,

Key1 、 Key2 則是那個HashMap的Key值

Text1、Text2是這個Layout裡面的文字ID



























最後會有的效果會是這個樣子。



















以上是介紹ListView最基本的用法,只是純粹顯示資料而已,

那當使用者點選或者是想知道他勾了那些選單,以及客製化ListView會在之後的章節介紹。

沒有留言:

張貼留言