android 在activity 监听自定义adapter

android 在activity 监听自定义adapter,第1张

概述android在activity监听自定义adapter1概述2.各个文件tab.xml文件tab_item.xml文件MyAdapter文件MyActivity文件(部分)3.结尾1概述在activity中会使用到adapter,adapter一般为自定义一个MyAdapter继承BaseAdapter类,有时使用场景就是在activity中监听adapter中按钮的点

androID 在activity 监听自定义adapter1概述2.各个文件tab.xml文件tab_item.xml文件MyAdapter文件MyActivity文件(部分)3.结尾

1概述

在activity中会使用到adapter,adapter一般为自定义一个MyAdapter继承BaseAdapter类,有时使用场景就是在activity中监听adapter中按钮的点击事件,但是在activity中直接获取adapter的控件使用setonClickListener方法不能生效,在网上找了下没找到解决方式,最后换种方式成功了,在此记录一下。

2.各个文件

tab.xml文件 activity关联的视图xml文件
tab_item.xml文件 adapter关联的视图xml文件
MyActivity文件 activity文件
MyAdapter文件 Adapter文件
首先说一下:按钮的点击事件可以在activity中设置(即MyActivity获取tab的button进行设置),也可以在Adapter中设置(即MyAdapter获取tab_item的button进行设置),在activity中设置的只能设置Adapter组件外的按钮(例如activity获取tab_item的button进行设置会不生效)。
下列将会使用3种方式交互,使用处有注解标示需要可自取使用
1.在adapter中获取activity传递的值并显示
2.在activity获取adapter中选中的值,进行提交
3.在activity中监听adapter中的点击事件,并作出相应的 *** 作

tab.xml文件
<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:tools="http://schemas.androID.com/tools"    androID:layout_wIDth="match_parent"    androID:ID="@+ID/tab"    androID:layout_height="match_parent"    tools:ignore="MissingDefaultResource">    <linearLayout        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content">        <TextVIEw            androID:ID="@+ID/time"            androID:layout_wIDth="125dp"            androID:layout_height="match_parent"            androID:text="日期"            androID:textSize="22sp" />        <TextVIEw            androID:ID="@+ID/name"            androID:layout_wIDth="125dp"            androID:layout_height="match_parent"            androID:text="名称"            androID:textSize="22sp" />        <TextVIEw            androID:ID="@+ID/operating"            androID:layout_wIDth="125dp"            androID:layout_height="match_parent"            androID:text=" *** 作"            androID:textSize="22sp" />    </linearLayout>    <button        androID:ID="@+ID/submit"        androID:layout_wIDth="80dp"        androID:layout_height="35dp"        androID:layout_weight="1"        androID:text="提交"        androID:layout_alignParentRight="true"        androID:textSize="14sp"/>    <ListVIEw        androID:ID="@+ID/tab4_List"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:layout_below="@+ID/text"        androID:layout_weight="1"        androID:paddingtop="36dp"></ListVIEw></relativeLayout>
tab_item.xml文件
<?xml version="1.0" enCoding="utf-8"?><linearLaxyout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:tools="http://schemas.androID.com/tools"    androID:ID="@+ID/item"    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:orIEntation="vertical"    androID:paddingleft="@dimen/activity_horizontal_margin"    androID:paddingtop="@dimen/activity_vertical_margin"    androID:paddingRight="@dimen/activity_horizontal_margin"    androID:paddingBottom="@dimen/activity_vertical_margin"    tools:ignore="MissingDefaultResource">         <linearLayout        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"        tools:ignore="OrIEntation">        <TextVIEw            androID:ID="@+ID/time_item"            androID:layout_wIDth="125dp"            androID:layout_height="match_parent"            androID:text="日期"            androID:textSize="22sp" />        <TextVIEw            androID:ID="@+ID/name_item"            androID:layout_wIDth="125dp"            androID:layout_height="match_parent"            androID:text="名称"            androID:textSize="22sp" />        <TextVIEw            androID:ID="@+ID/operating_item"            androID:layout_wIDth="125dp"            androID:layout_height="match_parent"            androID:text=" *** 作"            androID:textSize="22sp" />        <button            androID:ID="@+ID/start"            androID:layout_wIDth="52dp"            androID:layout_height="wrap_content"            androID:text="开始"/>        <CheckBox            androID:ID="@+ID/option"            androID:layout_wIDth="30dp"            androID:scaleX="3"            androID:scaleY="1.5"            androID:layout_height="23dp"            androID:focusable="false"            androID:visibility="visible"            androID:clickable="true"/>    </linearLayout></linearLaxyout>
MyAdapter文件
package com.example.zscj_app_hexin.com.example;import com.example.zscj_app_hexin.R;import androID.app.AlertDialog;import androID.content.Context;import androID.content.DialogInterface;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.BaseAdapter;import androID.Widget.button;import androID.Widget.CheckBox;import androID.Widget.TextVIEw;import java.text.SimpleDateFormat;import java.util.HashMap;import java.util.List;import java.util.Map;public class MyAdapter extends BaseAdapter {    List<HashMap<String, Object>> ListItem ;//用于设置各个列的值    private Context context;//必传 用于关联tab_item    private Map<Integer,Boolean> map=new HashMap<>();//创建map 用于存储按钮列是否被点击的布尔值,checkBox用,可无需要可不用    private Map<Integer,String> map2=new HashMap<>();//创建map列 用于存储点击了的数组,checkBox用,可无需要可不用    private String ID;//返回给activity用,可无需要可不用    private OnbuttonClickListener Listener;//设置监听    //构造方法    public MyAdapter2(List<HashMap<String, Object>> ListItem, Context context, OnbuttonClickListener Listener) {        this.ListItem = ListItem;        this.context = context;        this.Listener=Listener;    }    public int getCount() {        return ListItem.size();    }    public Object getItem(int i) {        return ListItem.get(i);    }    public long getItemID(int i) {        return i;    }    public Map<Integer, String> getMap2() {        return map2;    }    public VIEw getVIEw(final int i, VIEw vIEw, VIEwGroup vIEwGroup) {        VIEwHoder hd = new VIEwHoder();        if (vIEw == null) {            LayoutInflater layoutInflater = LayoutInflater.from(context);            vIEw = layoutInflater.inflate(R.layout.tab_item, null);            hd.name=vIEw.findVIEwByID(R.ID.name);//进行关联            vIEw.setTag(hd);        }        final HashMap<String, Object> mListItem=ListItem.get(i);        hd = (VIEwHoder) vIEw.getTag();        hd.name.setText((String)mListItem.get("name"));//获取activity传入的值并进行设置  即第一种 在adapter中获取activity传递的值并显示        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");        if (mListItem.get("time") != null){            hd.time.setText(sdf.format(mListItem.get("time")));//由于传过来的 mListItem.get("time") 是日期格式所以转为string        }else {            hd.time.setText("");        }        final VIEwHoder finalHd = hd;        hd.start.setonClickListener(new VIEw.OnClickListener() {            public voID onClick(VIEw v) {                AlertDialog.Builder builder = new AlertDialog.Builder(context);                builder.setTitle("确定要开始吗?").                        setNegativebutton("取消", new DialogInterface.OnClickListener() {                    public voID onClick(DialogInterface dialogInterface, int i) {                        ID=null;                        dialogInterface.dismiss();                    }                });                builder.setPositivebutton("确定", new DialogInterface.OnClickListener() {                    public voID onClick(DialogInterface dialogInterface, int i) {                        ID=(String) mListItem.get("ID");                        Listener.onbuttonClick(finalHd.start,ID);                    }                });                builder.show();            }        });        hd.checkBox.setChecked((Boolean) mListItem.get("checkBox"));        final CheckBox checkBox=vIEw.findVIEwByID(R.ID.option);        checkBox.setonClickListener(new VIEw.OnClickListener() {            public voID onClick(VIEw v) {                if (checkBox.isChecked()){                    map.put(i,true);                    map2.put(i,String.valueOf(mListItem.get("ID")));                }else {                    map.remove(i);                    map2.remove(i);                }            }        });        if(map!=null&&map.containsKey(i)){            checkBox.setChecked(true);        }else {            checkBox.setChecked(false);        }        return vIEw;    }    class VIEwHoder{        TextVIEw time;        TextVIEw name;        CheckBox checkBox;        button start;    }    public interface OnbuttonClickListener{        voID onbuttonClick(VIEw vIEw, String ID);    }}
MyActivity文件(部分)

@H_502_91@

3.结尾

个人使用安卓不多,有错误欢迎指正,会更正在文中,本次梳理总结是从原有项目中摘抄所需部分出来,MyActivity文件太多只摘取使用部分,可能会有所遗漏,有需要可私信。

总结

以上是内存溢出为你收集整理的android 在activity 监听自定义adapter全部内容,希望文章能够帮你解决android 在activity 监听自定义adapter所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1029359.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存