Android2.1--UI基本控件<1>--ListView
1 package com.yarin.android.Examples_04_04; 2 3 import android.app.Activity; 4 import android.database.Cursor; 5 import android.graphics.Color; 6 import android.os.Bundle; 7 import android.provider.ContactsContract; 8 import android.provider.ContactsContract.PhoneLookup; 9 import android.view.View; 10 import android.widget.AdapterView; 11 import android.widget.LinearLayout; 12 import android.widget.ListAdapter; 13 import android.widget.ListView; 14 import android.widget.SimpleCursorAdapter; 15 import android.widget.Toast; 16 17 public class Activity01 extends Activity 18 { 19 LinearLayout m_LinearLayout; 20 ListView m_ListView; 21 22 /** Called when the activity is first created. */ 23 @Override 24 public void onCreate(Bundle savedInstanceState) 25 { 26 super.onCreate(savedInstanceState); 27 28 /* 创建LinearLayout布局对象 */ 29 m_LinearLayout = new LinearLayout(this); 30 /* 设置布局LinearLayout的属性 */ 31 m_LinearLayout.setOrientation(LinearLayout.VERTICAL); 32 m_LinearLayout.setBackgroundColor(android.graphics.Color.BLACK); 33 34 /* 创建ListView对象 */ 35 m_ListView = new ListView(this); 36 LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 37 m_ListView.setBackgroundColor(Color.BLACK); 38 39 /* 添加m_ListView到m_LinearLayout布局 */ 40 m_LinearLayout.addView(m_ListView, param); 41 42 /* 设置显示m_LinearLayout布局 */ 43 setContentView(m_LinearLayout); 44 45 // 获取数据库Phones的Cursor 这个sdk 2.0 之后的版本对URI做了改动 46 Cursor cur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); 47 startManagingCursor(cur); 48 // ListAdapter是ListView和后台数据的桥梁 49 ListAdapter adapter = new SimpleCursorAdapter(this, 50 // 定义List中每一行的显示模板 51 // 表示每一行包含两个数据项 52 android.R.layout.simple_list_item_2, 53 // 数据库的Cursor对象 54 cur, 55 // 从数据库的NAME和NUMBER两列中取数据 56 new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER }, 57 // 与NAME和NUMBER对应的Views 58 new int[] {android.R.id.text1, android.R.id.text2 }); 59 // SimpleCursorAdapter类构造方法的第四个参数表示返回Cursor对象中的字段名,第五个参数表示要将该字段的值赋给那个组件。该组件在第二个参数中指定的布局文件中定义。 60 /* 将adapter添加到m_ListView中 */ 61 m_ListView.setAdapter(adapter); 62 63 /* 为m_ListView视图添加setOnItemSelectedListener监听 */ 64 m_ListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 65 66 @Override 67 public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) 68 { 69 // Parameters 70 // parent-- The AdapterView where the selection happened 71 // view --The view within the AdapterView that was clicked 72 // position -- The position of the view in the adapter 73 // id -- The row id of the item that is selected 74 75 76 DisplayToast("滚动到第"+Integer.toString(arg2+1)+"项"); 77 //public long getSelectedItemId () 78 //返回值 arg0.getSelectedItemId (); 79 //当前选中条目相应的 ID;无选中条目则返回 INVALID_ROW_ID。 80 81 } 82 83 @Override 84 public void onNothingSelected(AdapterView<?> arg0) 85 { 86 //没有选中 87 } 88 }); 89 90 /* 为m_ListView视图添加setOnItemClickListener监听 */ 91 m_ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 92 93 @Override 94 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) 95 { 96 //于对选中的项进行处理 97 // public abstract void onItemClick (AdapterView<?> parent, View view, int position, long id) 98 // 当AdapterView中一元素被点击时,回调方法被调用。 99 // 如果需要访问与被选项相关的数据,执行程序可以调用getItemAtPosition(position)。 100 // 参数 101 // parent 发生点击动作的AdapterView。 102 // view 在AdapterView中被点击的视图(它是由adapter提供的一个视图) 103 // position 视图在adapter中的位置。由0开始 104 // id 被点击元素的行id。 105 106 DisplayToast("选中了第"+Integer.toString(arg2+1)+"项"); 107 } 108 109 }); 110 111 } 112 /* 显示Toast */ 113 public void DisplayToast(String str) 114 { 115 Toast.makeText(this, str, Toast.LENGTH_SHORT).show(); 116 } 117 118 }
摘自《android应用开发揭秘》Examples_04_04 sdk 2.0
sdk 2.0 以后对电话簿的URI做了调整
ContactsContract.CommonDataKinds.CONTENT_URI--->ContactsContract.CommonDataKinds.Phone.CONTENT_URI
PhoneLookip.DISPLAY_NAME--->ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
PhoneLookip.NUMBER--->ContactsContract.CommonDataKinds.Phone.NUMBER
ListAdapter 将数据与ListView关键起来
ListAdapter从corsor中获取数据,使用方法 ListView.setAdapter(ListAdapte)将数据显示到ListView中