zoukankan      html  css  js  c++  java
  • 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 对象实例

  • 相关阅读:
    19 个必须知道的 Visual Studio 快捷键
    面试感悟:一名3年工作经验的程序员应该具备的技能
    来自开发者技术前线 高级程序员,你需要养成这7个习惯
    来自极客头条的 15个常用的javaScript正则表达式
    来自极客头条的 35 个 Java 代码性能优化总结
    [Visual studio] Visual studio 2017添加引用时报错未能正确加载ReferenceManagerPackage包的解决方法
    [Visual Studio] 未能完成操作 不支持此接口
    未能加载文件或程序集“Benlai.SOA.Framework.Common, Version=1.4.0.0, Culture=neutral, PublicKeyToken=null”或它的某一个依赖项。系统找不到指定的文件。
    [Visual Studio] NuGet发布自定义包(Library Package)
    未能加载视图状态。正在向其中加载视图状态的控件树必须与前一请求期间用于保存视图状态的控件树相匹配。例如,当以动态方式添加控件时,在回发期间添加的控件必须与在初始请求期间添加的控件的类型和位置相匹配
  • 原文地址:https://www.cnblogs.com/clarence/p/3428598.html
Copyright © 2011-2022 走看看