ListView與GridView是Android開發(fā)中的常用控件,它們和Adapter配合使用能夠?qū)崿F(xiàn)很多界面效果。下面分別以實(shí)例說(shuō)明ListView、GridView的用法。
1.ListView的Android開發(fā)實(shí)例
ListView 是android開發(fā)中最常用的控件之一,一般構(gòu)成列表包括三個(gè)元素,ListView:用來(lái)展示列表的視圖、Adapter:數(shù)據(jù)與視圖連接的橋梁、Data:具體的數(shù)據(jù)包括字符串 、圖片或者控件。
適配器一般有以下幾種類型:
ArrayAdapter:Android中最簡(jiǎn)單的一種適配器,專門用于列表控件。只顯示一行數(shù)據(jù)。
SimpleAdapter:此適配器有最好的擴(kuò)充性,可以自定義出各種效果。經(jīng)常使用靜態(tài)數(shù)據(jù)填充列表。
CursorAdapter:通過(guò)游標(biāo)向列表提供數(shù)據(jù)。
ResourceCursorAdapter:這個(gè)適配器擴(kuò)展了CursorAdapter,知道如何從資源創(chuàng)建視圖。
SimpleCursorAdapter:這個(gè)適配器擴(kuò)展了ResourceCursorAdapter,從游標(biāo)中得列創(chuàng)建 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(); } /** * 獲取聯(lián)系人列表 */ private void getContacts() { mListView=(ListView) findViewById(R.id.contacts_list); listData = new ArrayList<Map<String, String>>(); //獲取數(shù)據(jù)庫(kù)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); // 處理多個(gè)號(hào)碼的情況 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(); // 建立一個(gè)適配器去查詢數(shù)據(jù) 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添加事件監(jiān)聽 mListView.setOnItemSelectedListener(new ListView.OnItemSelectedListener(){ @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(getApplicationContext(), "當(dāng)前所在行為:"+Long.toString(parent.getSelectedItemId()+1), 1).show(); } @Override public void onNothingSelected(AdapterView<?> parent) { // TODO Auto-generated method stub } }); } } 
2.GridView的Android開發(fā)實(shí)例
GridView 網(wǎng)格視圖,用于顯示多行多列。直接上示例:
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. */ //定義圖片整型數(shù)組 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); //實(shí)例化GridView GridView mGridView=(GridView) findViewById(R.id.gridview); // 生成動(dòng)態(tài)數(shù)組,并且傳入數(shù)據(jù) 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));// 按序號(hào)做ItemText lstImageItem.add(map); } //構(gòu)建一個(gè)適配器 SimpleAdapter simple = new SimpleAdapter(this, lstImageItem, R.layout.gridviewitem, new String[] { "ItemImage", "ItemText" }, new int[] {R.id.ItemImage, R.id.ItemText }); mGridView.setAdapter(simple); //添加選擇項(xiàng)監(jiān)聽事件 mGridView.setOnItemClickListener(new GridView.OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast toast=Toast.makeText(getApplicationContext(), "你選擇了"+(position+1)+"號(hào)圖片", 1); toast.setGravity(Gravity.BOTTOM, 0, 0); toast.show(); } }); } } 

看了這兩個(gè)Android開發(fā)實(shí)例,相信大家對(duì)ListView、GridView的使用有了一定掌握了。大家還可以使用它們和Adapter實(shí)現(xiàn)更多的功能試試。
以上就是對(duì)Android ListView 和GridView 的資料整理,后續(xù)繼續(xù)補(bǔ)充相關(guān)資料,謝謝大家對(duì)本站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選