Listview和数据绑定
Listview是android开发中常用的组件,它以列表的形式显示具体内容,并且可以根据数据的长度自适应显示。
以下是具体的例子:
Listview示例效果图
main_list_view.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" ><ListView android:id="@+id/main_list_view_listview" android:layout_width="fill_parent" android:layout_height="fill_parent" /></LinearLayout>
main_list_view_item.xml配置
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/RelativeLayout_Item"android:layout_width="fill_parent" android:layout_height="wrap_content"android:paddingBottom="5dip"><ImageView android:id="@+id/main_list_view_item_item_image"android:layout_centerHorizontal="true" android:layout_width="wrap_content"android:layout_height="wrap_content"/><TextView android:id="@+id/main_list_view_item_item_text"android:layout_centerHorizontal="true" android:layout_width="wrap_content"android:layout_height="wrap_content" android:textSize="20px"android:layout_gravity="center"/></LinearLayout>
Activity代码
//定义选项菜单 private String[] allOptionsMenuTexts = {“删除”、“保存”、“帮助”、“添加”、“详细”、“发送”、“电话”、“拍照”}; private int[] allOptionsMenuOrders = {5,2,6,1,4,3,78}; private int[] allOptionsMenuIds = {Menu.FIRST+1,Menu.FIRST+2,Menu.FIRST+3,Menu.FIRST+4,Menu.FIRST+5,Menu.FIRST+6,Menu.FIRST+7,Menu.FIRST+8}; private int[] allOptionsMenuIcons = { android.R.drawable.ic_menu_delete, android.R.drawable.ic_menu_edit, android.R.drawable.ic_menu_help, android.R.drawable.ic_menu_add, android.R.drawable.ic_menu_info_details, android.R.drawable.ic_menu_send, android.R.drawable.ic_menu_call, android.R.drawable.ic_menu_camera }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_list_view); //展示Listviewiew showListView(); } /** * 显示Listviewiew显示. */ public void showListView(){ final Context context = this; Integer[] itemImages = new Integer[allOptionsMenuIcons.length]; for(int i=0;i<itemImages.length;i++){ itemImages[i] = new Integer(allOptionsMenuIcons[i]); } ///绑定GridView组件内容 List<Object[]> itemValues = new ArrayList<Object[]>(); itemValues.add(allOptionsMenuTexts); itemValues.add(itemImages); ///定义适配器to参数 int[] to = {R.id.main_list_view_item_item_text,R.id.main_list_view_item_item_image}; int resource = R.layout.main_list_view_item; //生成适配器:使用自定义工具类 SimpleAdapterUtil.java生成适配器 SimpleAdapter simpleAdapter = SimpleAdapterUtil.create(context,itemValues,resource,to); ListView listView = (ListView)findViewById(R.id.main_list_view_listview); listView.setAdapter(simpleAdapter); listView.setOnItemClickListener(new OnItemClickListener(){@Overridepublic void onItemClick(AdapterView<?> parent, View view,int position, long id) {Toast.makeText(context, "["+allOptionsMenuTexts[position]+""被点击", Toast.LENGTH_SHORT).show();} }); }
注:Simpleadapterutil工具类的完整代码,请参阅本文《简单的Simpleadadapter工具类》-SimpleAdapterUtil 》。