ListView與GridView是Android開發中的常用控件,它們和Adapter配合使用能夠實現很多界面效果。下面分別以實例說明ListView、GridView的用法。
1.ListView的Android開發實例
ListView 是android開發中最常用的控件之一,一般構成列表包括三個元素,ListView:用來展示列表的視圖、Adapter:數據與視圖連接的橋梁、Data:具體的數據包括字符串 、圖片或者控件。
適配器一般有以下幾種類型:
ArrayAdapter:Android中最簡單的一種適配器,專門用於列表控件。只顯示一行數據。
SimpleAdapter:此適配器有最好的擴充性,可以自定義出各種效果。經常使用靜態數據填充列表。
CursorAdapter:通過游標向列表提供數據。
ResourceCursorAdapter:這個適配器擴展了CursorAdapter,知道如何從資源創建視圖。
SimpleCursorAdapter:這個適配器擴展了ResourceCursorAdapter,從游標中得列創建 TextView/ImageView視圖。下面獲取通訊錄的示例:
XML/HTML代碼
- <?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"
- android:background="@drawable/bg"
-
- >
- <ListView
- android:id="@+id/contacts_list"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- </ListView>
- </LinearLayout>
Java代碼
- package net.csdn.blog.androidtoast;
-
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Map;
-
- import android.app.Activity;
- import android.database.Cursor;
- import android.os.Bundle;
- import android.provider.ContactsContract;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.ListAdapter;
- import android.widget.ListView;
- import android.widget.SimpleAdapter;
- import android.widget.Toast;
-
- public class MainActivity extends Activity {
-
- ListView mListView;
- ArrayList<Map<String, String>> listData;
-
- static final String NAME = "name";
- static final String NUMBER = "number";
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- getContacts();
- }
-
- /**
- * 獲取聯系人列表
- */
- private void getContacts() {
- mListView=(ListView) findViewById(R.id.contacts_list);
- listData = new ArrayList<Map<String, String>>();
- //獲取數據庫Cursor
- Cursor cur=getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
- startManagingCursor(cur);
- while (cur.moveToNext()) {
- Map<String, String> mp = new HashMap<String, String>();
- long id = cur.getLong(cur.getColumnIndex("_id"));
- Cursor pcur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
- null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + Long.toString(id), null, null);
-
- // 處理多個號碼的情況
- String phoneNumbers = "";
- while (pcur.moveToNext()) {
- String strPhoneNumber = pcur.getString(pcur.getColumnIndex(
- ContactsContract.CommonDataKinds.Phone.NUMBER));
- phoneNumbers += strPhoneNumber + ":";
- }
- phoneNumbers += "\n";
- pcur.close();
-
- String name = cur.getString(cur.getColumnIndex("display_name"));
- mp.put(NAME, name);
- mp.put(NUMBER, phoneNumbers);
- listData.add(mp);
- }
- cur.close();
-
- // 建立一個適配器去查詢數據
- ListAdapter adapter = new SimpleAdapter(this, listData, android.R.layout.simple_list_item_2,
- new String[]{NAME, NUMBER},
- new int[] {android.R.id.text1, android.R.id.text2});
- mListView.setAdapter(adapter);
- //為listView添加事件監聽
- mListView.setOnItemSelectedListener(new ListView.OnItemSelectedListener(){
- @Override
- public void onItemSelected(AdapterView<?> parent, View view,
- int position, long id) {
- Toast.makeText(getApplicationContext(), "當前所在行為:"+Long.toString(parent.getSelectedItemId()+1), 1).show();
- }
-
- @Override
- public void onNothingSelected(AdapterView<?> parent) {
- // TODO Auto-generated method stub
-
- }
-
- });
- }
- }
2.GridView的Android開發實例
GridView 網格視圖,用於顯示多行多列。直接上示例:
XML/HTML代碼
- <?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"
- android:background="@drawable/bg"
- >
- <GridView
- android:id="@+id/gridview"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:numColumns="3"/>
-
- </LinearLayout>
XML/HTML代碼
- <?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" android:scrollbars="vertical">
- <ImageView
- android:layout_height="100dip"
- android:id="@+id/ItemImage"
- android:layout_width="80dip"
- android:src="@drawable/png1"
- android:layout_gravity="center_horizontal"/>
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:id="@+id/ItemText" />
- </LinearLayout>
Java代碼
- package net.csdn.blog.androidtoast;
-
- import java.util.ArrayList;
- import java.util.HashMap;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.Gravity;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.GridView;
- import android.widget.SimpleAdapter;
- import android.widget.Toast;
-
- public class MainActivity extends Activity {
- /** Called when the activity is first created. */
- //定義圖片整型數組
- private int[] mImages={
- R.drawable.png1,
- R.drawable.png2,
- R.drawable.png3,
- R.drawable.png4,
- R.drawable.png5,
- R.drawable.png6,
- R.drawable.png7,
- R.drawable.png8,
- R.drawable.png9
-
- };
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- //實例化GridView
- GridView mGridView=(GridView) findViewById(R.id.gridview);
- // 生成動態數組,並且傳入數據
- ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();
-
- for (int i = 0; i < 9; i++) {
- HashMap<String, Object> map = new HashMap<String, Object>();
- map.put("ItemImage", mImages[i]);// 添加圖像資源的ID
- map.put("ItemText", "NO." + String.valueOf(i+1));// 按序號做ItemText
- lstImageItem.add(map);
- }
- //構建一個適配器
- SimpleAdapter simple = new SimpleAdapter(this, lstImageItem, R.layout.gridviewitem,
- new String[] { "ItemImage", "ItemText" }, new int[] {R.id.ItemImage, R.id.ItemText });
- mGridView.setAdapter(simple);
- //添加選擇項監聽事件
- mGridView.setOnItemClickListener(new GridView.OnItemClickListener(){
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- Toast toast=Toast.makeText(getApplicationContext(), "你選擇了"+(position+1)+"號圖片", 1);
- toast.setGravity(Gravity.BOTTOM, 0, 0);
- toast.show();
- }
-
- });
-
- }
- }
看了這兩個Android開發實例,相信大家對ListView、GridView的使用有了一定掌握了。大家還可以使用它們和Adapter實現更多的功能試試。