• 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="@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 对象实例

  • 相关阅读:
    iOS越狱系列(一):使用Reveal分析APP
    ios-异步消息同步问题-典型使用场景: 微信私信界面
    ios 消息跳转处理
    iOS开发UI篇—IOS CoreText.framework --- 基本用法
    IOS开发之实现App消息推送(最新)
    Thread 1: signal SIGABRT-内存管理的陋习
    别用symbolicatecrash来解析crash Log了by 风之枫
    通过崩溃trace来查找问题原因 .
    Xcode 6视图调试小贴士
    调试message send to deallocated instance问题
  • 原文地址:https://www.cnblogs.com/beijia/p/adapter.html
Copyright © 2020-2023  润新知