我就來為你介紹請怎麼使用列表(ListView)
新增專案我這邊就直接跳過了,不了解的可以參考其他篇文章
我們直接在main.xml的設計介面中拉進一個ListView
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
DemoListViewActivity.java
package jim.demo.listview;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class DemoListViewActivity extends Activity {
private ListView listcity;
private String catsName[]={"台北","桃園","新竹","苗栗","台中","彰化","雲林","嘉義","台南","高雄","屏東","台東","花蓮","宜蘭"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//指定該列表
listcity=(ListView)findViewById(R.id.listView1);
//這是簡單的快速寫法,直接設定資料橋接器ArrayAdapter到ListView內
listcity.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , catsName));
}
}
執行後畫面
後記:
我們可以看到,在這種需要和資料連結的控制項,都會有ArrayAdapter橋接器
橋接器是Android中做為資料和控制項連結之用。
ArrayAdapter
看到android.R.layout.simple_list_item_1,這個是內建的ListView設計樣式,你可以試著
改變為android.R.layout.simple_list_item_multiple_choice,你會發現ListView就會出現不
同之變化了,這是最基本的ListView,後續我們再來做點變形,感謝您的收看。



沒有留言:
張貼留言
您的寶貴建議是我前進的動力!