Android的UI组件之ListView(三)
来源:互联网 发布:新华社新媒体数据库 编辑:程序博客网 时间:2024/05/20 10:13
还是接着上篇。
三.通过继承BaseAdapter来ListView的每个Item中显示一个组合控件
其实第三种方法和上篇中的应该归到一类,只不过方法不同,姑且算上一种吧。这篇文章参考的是http://blog.csdn.net/hellogv/的一篇文章。
UI界面XMl代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" ><ListView android:id="@+id/myList" android:layout_width="wrap_content" android:layout_height="wrap_content" > </ListView></LinearLayout>list.xml代码如下:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="fill_parent" /><TextView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toRightOf="@id/image" /><TextView android:id="@+id/textView2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toRightOf="@id/image" android:layout_below="@id/textView1" /></RelativeLayout>Activity代码如下:
package com.example.testlistview;import android.os.Bundle;public class TestListView extends Activity {ListView listView;String [] titles={"标题1","标题2","标题3","标题4"};String [] texts={"文本内容1","文本内容2","文本内容3","文本内容4"};int [] Ids={R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test_list_view); listView=(ListView)findViewById(R.id.myList); listView.setAdapter(new ListViewAdapter(titles, texts, Ids)); } public class ListViewAdapter extends BaseAdapter{View [] itemViews;public ListViewAdapter(String[] stitles,String[] stexts,int[] sIds ){itemViews=new View[stitles.length];for(int i=0;i<stitles.length;i++){itemViews[i] = makeItemView(stitles[i], stexts[i], sIds[i]); }}private View makeItemView(String strtitle,String strtext,int resid ){LayoutInflater inflater=(LayoutInflater)TestListView.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);// 使用View的对象itemView与R.layout.item关联 View itemView=inflater.inflate(R.layout.list, null);// 通过findViewById()方法实例R.layout.item内各组件 TextView title=(TextView)itemView.findViewById(R.id.textView1);title.setText(strtitle);TextView text=(TextView)itemView.findViewById(R.id.textView2);text.setText(strtext);ImageView image=(ImageView)itemView.findViewById(R.id.image);image.setImageResource(resid);return itemView;} public int getCount() {// TODO Auto-generated method stubreturn itemViews.length;}public Object getItem(int arg0) {// TODO Auto-generated method stubreturn itemViews[arg0];}public long getItemId(int arg0) {// TODO Auto-generated method stubreturn arg0;}public View getView(int arg0, View arg1, ViewGroup arg2) {// TODO Auto-generated method stub if (arg1 == null) return itemViews[arg0]; return arg1;} } }运行结果如下:
详细的解释可参考文章:http://blog.csdn.net/hellogv/article/details/4548659
这里要说明的一点自己在编写好后,调试时老是出错,经过将近1个小时的排查才发现是Activity代码中36、39、42中是写成了TextView title=(TextView)findViewById(R.id.textView1);而应该写成
TextView title=(TextView)itemView.findViewById(R.id.textView1)
大家注意一下这个细节。
- Android的UI组件之ListView(三)
- Android UI 之ListView组件
- Android的UI组件之ListView(一)
- Android的UI组件之ListView(二)
- Android常用UI组件 - ListView
- Android UI组件--ListView(1)
- Android常见UI组件之ListView(一)
- Android UI 之 ListView
- Android之UI- listview特别的属性
- android UI之ListView的自定义
- android组件之ListView
- Android的UI组件之TextView、EditText
- Android的UI组件之GridView
- 自学Android之UI组件:(三)Fragment的基本使用(中)
- Android常见UI组件之ListView(二)——定制ListView
- 安卓UI组件之ListView详解
- UI组件之ListView实现分页
- UI组件之AdapterView及其子类(一)三种Adapter适配器填充ListView
- MySQL 中 QueryCache 的锁模型
- 字符设备驱动程序
- 常见Web技术之间的关系,你了解多少?
- 设置局域网访问tomcat
- php.ini 放置位置
- Android的UI组件之ListView(三)
- Makefile学习笔记
- CSS属性大全
- Velocity 例子介绍
- Objective-C 类的继承、方法重载
- 一百以内的杨辉三角
- POJ 1113 Wall (凸包)
- poj 1095 Trees Made to Order
- 关于C++中的前置声明(2)