天天看點

[Android] ListView (普通清單控件) 的基本使用方法

        在Android中,ListView控件用于以清單的形式顯示資料,采用MVC模式将前端顯示與後端資料進行分離。如下圖所示,ListView控件并不直接使用資料,而是間接通過Adapter對象。

是以,ListView控件的使用,一般需要進行兩步綁定:

        · 資料和Adapter的綁定:如下圖中(1)

        · Adapter和ListView的綁定:如下圖中(2)

[Android] ListView (普通清單控件) 的基本使用方法

        代碼中,ArrayAdapter的構造方法需要三個參數,本例中第一個參數使用目前Activity的對象執行個體,第二個參數指定布局檔案的資源ID,第三個參數指定清單項中的資料。

        使用ListView,可根據需要實作相關接口,常用的有OnItemClickListener(當清單項被點選時觸發)和OnItemSelectListener(當清單項被選中時觸發)。

代碼:

(1) MainActivity.java

package com.fergusworkroom.demo_listview;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity implements OnItemClickListener, OnItemSelectedListener{
	
	private static String[] data = new String[]{
		"大連",
		"沈陽",
		"北京",
		"哈爾濱",
		"杭州",
		"呼和浩特",
		"齊齊哈爾",
		"城市,是以非農業産業和非農業人口集聚形成的較大居民點(包括按國家行政建制設立的市、鎮)。一般而言,人口較稠密的地區稱為城市(city),一般包括了住宅區、工業區和商業區并且具備行政管轄功能。"
	};

	private TextView selectedItemInfo;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        ListView commonListView = (ListView)findViewById(R.id.commonListView);
        selectedItemInfo = (TextView)findViewById(R.id.selectedItemInfo);
        
        // (1) Adapter  <--> Data
        ArrayAdapter<String> aaData = new ArrayAdapter<String>(this,
        		android.R.layout.simple_list_item_1, data);
        
        // (2) ListView <--> Adapter
        commonListView.setAdapter(aaData);
        
        // (3) 設定相關響應函數
        commonListView.setOnItemClickListener(this);
        commonListView.setOnItemSelectedListener(this);
    }

	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		// TODO Auto-generated method stub
		selectedItemInfo.setText("Item selected: " + arg2 + ", " + arg3);
	}

	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		selectedItemInfo.setText("Nothing selected");
	}

	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		selectedItemInfo.setText("Item clicked: " + arg2 + ", " + arg3);
	}
}
           

(2) activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView 
        android:id="@+id/selectedItemInfo"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:text="所選資訊" />
    <ListView
        android:id="@+id/commonListView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
           

參考文獻:

[1] 李甯. Android開發權威指南[M].北京:人民郵電出版社.