Android自定义adapter的listview
2016-06-19 17:22
459 查看
在开发中,我们经常使用到ListView这个控件,但ListView自带适配SimpleAdapter,SimpleCursorAdapter等扩展性比较差(如Button,imageView等),严重制约我们的开发。不过Android也提供自定义的适配器adapter给我扩展这些控件,下面做个简单例子(listview里面含有textview,imageview,button)
1.定义一个数据类,不用数据集,也是为扩展
2.每个列需显示的内容
3.自定义适配器adapter
4.在activity上给listview加载数据
5.最后实现的效果和点击button的效果
自定义adapter已经完成,这也是adapter的一个模板,具体代码http://download.csdn.net/detail/qq_29955091/9553940
1.定义一个数据类,不用数据集,也是为扩展
public class Data { <span style="white-space:pre"> </span>private int img_id;//图片id <span style="white-space:pre"> </span>private String name;//名字 <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>public Data(int img_id, String name) { <span style="white-space:pre"> </span>super(); <span style="white-space:pre"> </span>this.img_id = img_id; <span style="white-space:pre"> </span>this.name = name; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public int getImg_id() { <span style="white-space:pre"> </span>return img_id; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public void setImg_id(int img_id) { <span style="white-space:pre"> </span>this.img_id = img_id; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public String getName() { <span style="white-space:pre"> </span>return name; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public void setName(String name) { <span style="white-space:pre"> </span>this.name = name; <span style="white-space:pre"> </span>}<span style="white-space:pre"> </span> <span style="white-space:pre"> </span> }
2.每个列需显示的内容
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000000" > <LinearLayout android:gravity="center" <span style="white-space:pre"> </span>android:layout_width="wrap_content" android:layout_height="wrap_content" > <ImageView android:layout_width="50dp" android:layout_height="50dp" android:id="@+id/img" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ffffff" /> </LinearLayout> <Button android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/bt" android:text="@string/comfirm" /> </RelativeLayout>
3.自定义适配器adapter
public class Adapter extends BaseAdapter{ private Context context; private int itemLayoutID; private int[] itemIds; <span style="white-space:pre"> </span>private List<Data> dataList; <span style="white-space:pre"> </span>public Adapter(Context context, int itemLayoutID, int[] itemIds, List<Data> dataList ) { <span style="white-space:pre"> </span>this.context = context; this.itemLayoutID = itemLayoutID; this.itemIds = itemIds; this.dataList = dataList; <span style="white-space:pre"> </span> } @Override public int getCount() { // TODO Auto-generated method stub return dataList.size(); } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return dataList.get(arg0); } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ViewHolder holder; if (convertView == null) {//定义每行的布局 LayoutInflater layoutInflater = LayoutInflater.from(context); convertView = layoutInflater.inflate(itemLayoutID, null); holder = new ViewHolder(); holder.name = (TextView) convertView .findViewById(itemIds[1]); holder.image = (ImageView) convertView .findViewById(itemIds[0]); holder.bt = (Button) convertView .findViewById(itemIds[2]); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } //根据listview的位置获取数据 final String name = dataList.get(position).getName(); holder.name.setText(name); holder.image.setImageResource(dataList.get(position).getImg_id()); holder.bt.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub new AlertDialog.Builder(context).setTitle("系统提示")//设置对话框标题 .setMessage(name)//设置显示的内容 .setPositiveButton("确定",new DialogInterface.OnClickListener() {//添加确定按钮 @Override public void onClick(DialogInterface dialog, int which) {//确定按钮的响应事件 // TODO Auto-generated method stub } }).show();//在按键响应事件中显示此对话框 } }); return convertView; } static class ViewHolder { public TextView name; public ImageView image; public Button bt; }
4.在activity上给listview加载数据
<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="match_parent" android:background="#000000" > <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView> </RelativeLayout>
public class MainActivity extends Activity { private List<Data> dataList; private int[] img_id = {R.drawable.coffee1,R.drawable.coffee2, R.drawable.coffee3,R.drawable.coffee4,R.drawable.coffee5}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化数据 dataList = new ArrayList<Data>(); for(int i=0;i<img_id.length;i++){ int img = img_id[i]; String name = this.getResources().getString(R.string.coffee) + i; Data data = new Data(img, name); dataList.add(data); } // 创建一个自定义Adapter Adapter adapter = new Adapter(MainActivity.this,R.layout.list_item, new int[]{R.id.img, R.id.name, R.id.bt}, dataList); ListView list = (ListView) findViewById(R.id.lv); // 为ListView设置Adapter list.setAdapter(adapter); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
5.最后实现的效果和点击button的效果
自定义adapter已经完成,这也是adapter的一个模板,具体代码http://download.csdn.net/detail/qq_29955091/9553940
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories