1. 程式人生 > >第2章 使用ListView顯示資訊列表

第2章 使用ListView顯示資訊列表

佈局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@+id/listView"
        android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>

ArrayAdapter:

public class MainActivity extends ActionBarActivity {

    private ArrayAdapter arrayAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView listView = (ListView)findViewById(R.id.listView);
        String[] array = new
String[]{"1","2","3"}; //1.新建一個數據介面卡 //引數:上下文,列表的佈局,資料來源 //2.介面卡載入資料來源 arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,array); //3.檢視載入介面卡 listView.setAdapter(arrayAdapter); } }

SimpleAdapter:

public class MainActivity
extends Activity implements OnItemClickListener, OnScrollListener {
private ListView listView; private SimpleAdapter simple_adapter; private List<Map<String, Object>> list; private int i=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 匹配佈局檔案中的ListView控制元件 listView = (ListView) findViewById(R.id.listView); // 設定ListView的元素被選中時的事件處理監聽器 listView.setOnItemClickListener(this); listView.setOnScrollListener(this); getData(); // 設定SimpleAdapter監聽器 /* * context:上下文 data:資料來源(List<? extends Map<String,?>> data)一個Map所組成的List集合 每個Map都會去對應ListView列表中的一行 每個Map(鍵-值對)中的鍵必須包含所有在from中所指定的鍵 resource:列表項的佈局檔案ID from:Map中的鍵名 to:繫結資料檢視中的ID,與from成對應關係 * */ simple_adapter = new SimpleAdapter(MainActivity.this, list, R.layout.list_item, new String[] { "image", "text" }, new int[] { R.id.image, R.id.text }); listView.setAdapter(simple_adapter); } // 載入SimpleAdapter資料集 private List<Map<String, Object>> getData() { list = new ArrayList<Map<String, Object>>(); Map<String, Object> map = new HashMap<String, Object>(); for(int i = 0;i < 20;i++){ map.put("text", "java"); map.put("image", R.drawable.ic_launcher); list.add(map); } Log.i("Main", list.size() + ""); return list; } // (5)事件處理監聽器方法 @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub // 獲取點選ListView item中的內容資訊 String text = listView.getItemAtPosition(position) + ""; // 彈出Toast資訊顯示點選位置和內容 Toast.makeText(MainActivity.this, "position=" + position + " content=" + text, 0).show(); } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { // TODO Auto-generated method stub switch (scrollState) { //是當用戶由於之前划動螢幕並擡起手指,螢幕產生慣性滑動時 case SCROLL_STATE_FLING: Log.d("Main", "用力劃了一下"); Map<String, Object> map = new HashMap<String, Object>(); map.put("text", "滾動新增 "+i++); map.put("image", R.drawable.ic_launcher); list.add(map); //動態更新檢視中所包含的資料 simple_adapter.notifyDataSetChanged(); break; //當用戶在以觸屏方式滾動螢幕並且手指仍然還在螢幕上 case SCROLL_STATE_TOUCH_SCROLL: Log.d("Main", "當用戶在以觸屏方式滾動螢幕並且手指仍然還在螢幕上"); break; //是當螢幕停止滾動時 case SCROLL_STATE_IDLE: Log.d("Main", "當螢幕停止滾動時"); break; } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { // TODO Auto-generated method stub } }