Android自定义adapter的listview
来源:互联网 发布:文明5 mac mod文件夹 编辑:程序博客网 时间:2024/05/17 18:44
在开发中,我们经常使用到ListView这个控件,但ListView自带适配SimpleAdapter,SimpleCursorAdapter等扩展性比较差(如Button,imageView等),严重制约我们的开发。不过Android也提供自定义的适配器adapter给我扩展这些控件,下面做个简单例子(listview里面含有textview,imageview,button)
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> } @Overridepublic int getCount() {// TODO Auto-generated method stubreturn dataList.size();}@Overridepublic Object getItem(int arg0) {// TODO Auto-generated method stubreturn dataList.get(arg0);}@Overridepublic long getItemId(int arg0) {// TODO Auto-generated method stubreturn 0;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubViewHolder 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() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubnew 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};@Overrideprotected 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);}@Overridepublic 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
0 0
- Android 自定义Adapter的Listview
- Android 自定义Adapter的ListView
- Android 自定义Adapter的ListView
- Android自定义adapter的listview
- Android之自定义Adapter的ListView
- 【转】Android之自定义Adapter的ListView
- Android之自定义Adapter的ListView
- Android之自定义Adapter的ListView
- Android之自定义Adapter的ListView
- Android之自定义Adapter的ListView
- [转载]Android之自定义Adapter的ListView
- Android之自定义Adapter的ListView
- Android之自定义Adapter的ListView
- Android之自定义Adapter的ListView
- Android——自定义Adapter的ListView
- 【转】Android之自定义Adapter的ListView
- Android之自定义Adapter的ListView
- 【转】Android之自定义Adapter的ListView
- 把字符串转为整数
- mybatis_user_guide(3)XML配置
- 进程与线程的区别
- Oracle PL/SQL之DDL导致的隐式提交
- ramdisk制作过程记录
- Android自定义adapter的listview
- 读取Assets目录下的数据库
- 基于mfc的对话框编程中,实现控件随对话框大小自动缩放以及通过滚动条实现控件移动功能
- 重温7 布局相关
- 基于C的2048游戏
- eqweqweqwe
- Java内部类的作用
- java伪随机数
- CentOS7安装MariaDB服务无法启动的问题