Android中的自定义数据适配器

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"
        android:src="http://www.cnblogs.com/beijia/archive/2012/05/08/@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 对象实例

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值