標籤:android listview
列表視圖(ListView)是Android當中一個非常重要的組件,它以列表的形式展示具體內容,並且能夠根據資料的長度自適應顯示。
列表的顯示需要三個元素:
1.ListVeiw 用來展示列表的View。
2.適配器 用來把資料對應到ListView上的中介。
3.資料 具體的將被映射的字串,圖片,或者基本組件。
首先介紹“適配器”這個基礎概念。在列表中定義的資料都通過“適配器”來映射到ListView上,ListView中常用的適配器有兩種:
ArrayAdapter:最簡單的適配器,只能顯示一行文字;
SimpleAdapter:具有很好擴充性的適配器,可以顯示自訂內容。
用ArrayAdapter結合ListView進行顯示
運行效果
原始碼ActivityList1.java
package com.rainsong.listviewdemo;import java.util.List;import java.util.ArrayList;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.Toast;public class ActivityList1 extends Activity{ ListView listView; List<String> data; ArrayAdapter arrayAdapter; OnItemClickListener listener; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); listView = new ListView(this); data = new ArrayList<String>(); data.add("Item1"); data.add("Item2"); data.add("Item3"); arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, data); listView.setAdapter(arrayAdapter); listener = new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(ActivityList1.this, parent.getItemAtPosition(position).toString() + " clicked",Toast.LENGTH_SHORT).show(); } }; listView.setOnItemClickListener(listener); setContentView(listView); }}
API知識點
ArrayAdapter
public class
ArrayAdapter
extends BaseAdapter
implements Filterable
ArrayAdapter(Context context, int resource, List<T> objects)
Constructor
ListView
public class
ListView
extends AbsListView
ListView(Context context)
void
setAdapter(ListAdapter adapter)
Sets thedata behind this ListView.
void
setOnItemClickListener(AdapterView.OnItemClickListener listener)
Register acallback to be invoked when an item in this AdapterView has been clicked.
AdapterView.OnItemClickListener
public static interface
AdapterView.OnItemClickListener
abstract void
onItemClick(AdapterView<?>parent, View view, int position, long id)
Callbackmethod to be invoked when an item in this AdapterView has been clicked.
AdapterView
public abstract class
AdapterView
extends ViewGroup
Object
getItemAtPosition(int position)
Gets the data associated with the specified position in the list.
Toast
public class
Toast
extends Object
Constants
int LENGTH_LONG Showthe view or text notification for a long period of time.
int LENGTH_SHORT Showthe view or text notification for a short period of time.
static Toast
makeText(Context context, int resId, int duration)
Make a standard toast that just contains a text view with the text from a resource.
static Toast
makeText(Context context, CharSequence text, int duration)
Make a standard toast that just contains a text view.
void
show()
Show the view for the specified duration.
用SimpleAdapter結合ListView進行顯示
運行效果
原始碼ActivityList2.java
package com.rainsong.listviewdemo;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.Toast;public class ActivityList2 extends Activity{ private List<Map<String, Object>> data; private ListView listView; private SimpleAdapter adapter; OnItemClickListener listener; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); data = new ArrayList<Map<String, Object>>(); Map<String, Object> item; item = new HashMap<String, Object>(); item.put("姓名", "張三"); item.put("性別", "男"); data.add(item); item = new HashMap<String, Object>(); item.put("姓名", "李四"); item.put("性別", "男"); data.add(item); item = new HashMap<String, Object>(); item.put("姓名", "王五"); item.put("性別", "男"); data.add(item); item = new HashMap<String, Object>(); item.put("姓名", "小雪"); item.put("性別", "女"); data.add(item); item = new HashMap<String, Object>(); item.put("姓名", "小明"); item.put("性別", "男"); data.add(item); listView = new ListView(this); adapter = new SimpleAdapter(this, data, android.R.layout.simple_list_item_2, new String[] {"姓名", "性別"}, new int[] {android.R.id.text1, android.R.id.text2}); listView.setAdapter(adapter); setContentView(listView); listener = new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(ActivityList2.this, parent.getItemAtPosition(position).toString() + " clicked",Toast.LENGTH_SHORT).show(); } }; listView.setOnItemClickListener(listener); }}
API知識點
SimpleAdapter
public class
SimpleAdapter
extends BaseAdapter
implements Filterable
SimpleAdapter(Contextcontext, List<? extends Map<String, ?>> data, int resource,String[] from, int[] to)
Constructor
Android常用UI組件 - ListView