Android2.1--UI基本控件<1>--ListView
2012-08-02 10:20
375 查看
package com.yarin.android.Examples_04_04; import android.app.Activity; import android.database.Cursor; import android.graphics.Color; import android.os.Bundle; import android.provider.ContactsContract; import android.provider.ContactsContract.PhoneLookup; import android.view.View; import android.widget.AdapterView; import android.widget.LinearLayout; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.SimpleCursorAdapter; import android.widget.Toast; public class Activity01 extends Activity { LinearLayout m_LinearLayout; ListView m_ListView; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* 创建LinearLayout布局对象 */ m_LinearLayout = new LinearLayout(this); /* 设置布局LinearLayout的属性 */ m_LinearLayout.setOrientation(LinearLayout.VERTICAL); m_LinearLayout.setBackgroundColor(android.graphics.Color.BLACK); /* 创建ListView对象 */ m_ListView = new ListView(this); LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); m_ListView.setBackgroundColor(Color.BLACK); /* 添加m_ListView到m_LinearLayout布局 */ m_LinearLayout.addView(m_ListView, param); /* 设置显示m_LinearLayout布局 */ setContentView(m_LinearLayout); // 获取数据库Phones的Cursor 这个sdk 2.0 之后的版本对URI做了改动 Cursor cur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); startManagingCursor(cur); // ListAdapter是ListView和后台数据的桥梁 ListAdapter adapter = new SimpleCursorAdapter(this, // 定义List中每一行的显示模板 // 表示每一行包含两个数据项 android.R.layout.simple_list_item_2, // 数据库的Cursor对象 cur, // 从数据库的NAME和NUMBER两列中取数据 new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER }, // 与NAME和NUMBER对应的Views new int[] {android.R.id.text1, android.R.id.text2 }); // SimpleCursorAdapter类构造方法的第四个参数表示返回Cursor对象中的字段名,第五个参数表示要将该字段的值赋给那个组件。该组件在第二个参数中指定的布局文件中定义。 /* 将adapter添加到m_ListView中 */ m_ListView.setAdapter(adapter); /* 为m_ListView视图添加setOnItemSelectedListener监听 */ m_ListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // Parameters // parent-- The AdapterView where the selection happened // view --The view within the AdapterView that was clicked // position -- The position of the view in the adapter // id -- The row id of the item that is selected DisplayToast("滚动到第"+Integer.toString(arg2+1)+"项"); //public long getSelectedItemId () //返回值 arg0.getSelectedItemId (); //当前选中条目相应的 ID;无选中条目则返回 INVALID_ROW_ID。 } @Override public void onNothingSelected(AdapterView<?> arg0) { //没有选中 } }); /* 为m_ListView视图添加setOnItemClickListener监听 */ m_ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { //于对选中的项进行处理 // public abstract void onItemClick (AdapterView<?> parent, View view, int position, long id) // 当AdapterView中一元素被点击时,回调方法被调用。 // 如果需要访问与被选项相关的数据,执行程序可以调用getItemAtPosition(position)。 // 参数 // parent 发生点击动作的AdapterView。 // view 在AdapterView中被点击的视图(它是由adapter提供的一个视图) // position 视图在adapter中的位置。由0开始 // id 被点击元素的行id。 DisplayToast("选中了第"+Integer.toString(arg2+1)+"项"); } }); } /* 显示Toast */ public void DisplayToast(String str) { Toast.makeText(this, str, Toast.LENGTH_SHORT).show(); } }
摘自《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中
相关文章推荐
- Android2.1--UI基本控件<2>--Button
- Android2.1--UI基本控件<3>--初学者对Intent的理解
- Android基本控件之ListView(二)<ListView优化>
- Android基本控件之listView(三)<用ListView实现分页加载>
- Android基本控件之ListView(二)<ListView优化>
- android面试题目大全<第一部分>,android基本的UI控件和布局文件知识
- Android UI设计之<十二>自定义View,实现绚丽的字体大小控制控件FontSliderBar
- Android UI设计之<十>自定义ListView,实现QQ空间阻尼下拉刷新和渐变菜单栏效果
- <Android>控件Spinner的基本使用
- Android实战简易教程<五十一>(ListView实现子控件的动态显示和隐藏、checkbox全选和反选)
- Android UI控件----ExpandableListView的基本用法
- Android UI 优化 使用<include/>和 <merge />标签
- android之Animation<1>
- 从零开始学android:Android基本控件(下)—— 列表显示:ListView(GOOD)
- OpenCV 2 学习笔记(10): 算法的基本设计模式<1>:策略模式(strategy pattern)
- Android OPenCV 环境配置 <1>
- Android开发-UI控件:为ListView,GirdView,etc...添加系统自带的下拉刷新功能
- Android UI设计之<七>自定义Dialog,实现各种风格效果的对话框
- Android 控件<button>对齐方法
- Android自定义UI有哪些基本控件以及需求