Android中的自定义数据适配器
2412 点击·0 回帖
![]() | ![]() | |
![]() | 应用程序实体类 public class App{ private int appId; // 应用程序id private String appName;// 应用程序名称 private String appIcon; // 应用程序图标 public int getAppId(){ return this.appId; } public void setAppId(){ this.appId=value; } public int getAppName(){ return this.appName; } public void setAppName(){ this.appId=appName; } public int getAppIcon(){ return this.appIcon; } public void setAppIcon(){ this.appId=appIcon; } } app_item.xml 文件 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:Android="http://schemas.Android.com/apk/res/Android" Android:layout_width="fill_parent" Android:layout_height="wrap_content"> <ImageView Android:id="@+id/imgIcon" Android:layout_width="50px" Android:layout_height="50px" Android:layout_alignParentLeft="true" Android:layout_alignParentTop="true" Android:layout_marginLeft="5dip" Android:layout_marginTop="2dip" Androidrc="@drawable/portrait" /> <TextView Android:id="@+id/txtName" Android:layout_width="wrap_content" Android:layout_height="wrap_content" Android:layout_marginLeft="10dip" Android:layout_centerVertical="true" Android:layout_toRightOf="@id/imgPortrait" Android:textColor="@Android:color/black" Android:textSize="16dip" Android:gravity="center" Android:text="" /> <Button Android:id="@+id/btnDel" Android:layout_width="wrap_content" Android:layout_height="wrap_content" Android:layout_alignParentRight="true" Android:layout_centerVertical="true" Android:layout_marginRight="10dip" Android:text="删除" Android:textSize="12dip" Android:focusable="false" Android:focusableInTouchMode="false" /> // 注意:当设置 Android:focusable="false" 和 Android:focusableInTouchMode="false" 时,可避免和ListView的Item点击事件冲突,导致item的click事件无效。 </RelativeLayout> 自定义数据适配器 public class AppAdapter extends BaseAdapter implements View.OnClickListener { private Context context; private List<App> appList; private final String inflater = Context.LAYOUT_INFLATER_SERVICE; private LayoutInflater layoutInflater; private Handler handler; private AsyncImageLoader imageLoader; // 异步加载图片的类 // 视图容器类,属性对应布局文件元素 private class ViewHolder { ImageView imgIcon; TextView txtName; Button btnDel; } // 构造函数 public AppAdapter (Context c, List<App> list) { if (null != list) { appList= list; } else { appList= new ArrayList<App>(); } this.context = c; layoutInflater = (LayoutInflater) context.getSystemService(inflater); handler = new Handler(); imageLoader = new AsyncImageLoader(); } // 添加单个项(自定义方法) public void addItem(App item) { if (item != null) { appList.add(item); notifyDataSetChanged(); // 通知适配器数据已改变 } } // 添加多个项(自定义方法) public void addItem(List<App> list) { if (null != list ;; list.size() > 0) { for (int i = 0; i < list.size(); i++) { appList.add(list.get(i)); } notifyDataSetChanged(); // 通知适配器数据已改变 } } // 删除项(自定义方法) public void removeItem(int position) { if (appList.get(position) != null) { appList.remove(position); notifyDataSetChanged(); // 通知适配器数据已改变 } } // 获取总数 public int getCount() { return appList.size(); } // 获取单条数据 public App getItem(int position) { return appList.get(position); } // 获取当前位置项的id(自定义方法) public int getItemId(int position) { return appList.get(position).getAppId(); } // 获取视图 public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (null == convertView) { // 装载布局文件 app_item.xml convertView = (RelativeLayout) layoutInflater.inflate(R.layout.app_item, null); holder = new ViewHolder(); holder.imgIcon = (ImageView) convertView.findViewById(R.id.imgIcon ); holder.txtNick = (TextView) convertView.findViewById(R.id.txtNick ); holder.btnDel= (Button) convertView.findViewById(R.id.btnDel); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } App app = appList.get(position); // 获取当前项数据 if (null != app) { holder.txtName.setText(app.getAppName()); holder.btnDel.setOnClickListener(this); // 添加按钮点击事件 holder.btnDel.setTag(app.getAppId()); // 设置按钮"tag"为应用程序的id,便于删除时使用 imageLoader.loadImage(app.getAppIcon(), holder.imgIcon); // 异步加载图片 } return convertView; } // 实现 View.OnClickListener 接口方法 public void onClick(View v) { Button btn = (Button) v; // 获取当前删除项的id int id= Integer.parseInt(btn.getTag().toString()); // 调用删除方法 removeItem(id); } } 在Activity类中调用如下: List<App> list = new List<App>(); // 获取数据 AppAdapter adapter = new AppAdapter(this, list ); listView.setAdapter(adapter); // listView 为 ListView 对象实例 | |
![]() | ![]() |