在ListView中实现单选功能、实现数据回调

声明:本教程不收取任何费用,欢迎转载请注明出处,尊重作者劳动成果,不得用于商业用途,侵权必究!!!

 

目录

一、在ListView中实现单选功能

二、实现数据回调

1、在 Dialog 里面写监听接口

2、Dialog点击ListView某item,通过接口传递数据

3、在 Activity 里面进行监听,得到回调的数据并进行显示


一、在ListView中实现单选功能

之前在项目中也做过类似案例,今天项目又碰到了,就没找之前代码了,

在网上搜索了一下关键字,可值得参考博文:https://www.jianshu.com/p/6af0a7a59756

 

二、实现数据回调

ListView做了一下数据监听,由于正在做项目,这里只写一下思路,贴一下关键代码!

说白了就是,Activity上面弹出一个Dialog,Dialog里面有一个ListView实现了单选,

当某项item被单选的时候,把这项item的数据对象,传到Activity进行显示并关闭Dialog

 

1、在 Dialog 里面写监听接口

public interface OnCallBackChangeAddressListener {
    public void onCallBackChangeAddress(AddressListBean.AddressListBeanData addressListBeanData);
}

public OnCallBackChangeAddressListener onCallBackAddLibaryListener;

public void setOnCallBackAddLibaryListener(OnCallBackChangeAddressListener onCallBackAddLibaryListener) {
    this.onCallBackAddLibaryListener = onCallBackAddLibaryListener;
}

 

2、Dialog点击ListView某item,通过接口传递数据

binding.listAddressList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {

        //获取选中的参数
        selectPosition = position;
        addressListDialogAdapter.setSelectPosition(selectPosition,addressListDialogAdapter);

        addressListBeanData = addressListBeanDataList.get(position);

        Toast.makeText(context,"您选中的地址是:"
                +addressListBeanDataList.get(position).getUserName(),Toast.LENGTH_SHORT).show();

        if (onCallBackAddLibaryListener != null) {
                onCallBackAddLibaryListener.onCallBackChangeAddress(
                        addressListBeanDataList.get(position));
                DialogAddressList.this.dismiss();
        }

    }
});

 

3、在 Activity 里面进行监听,得到回调的数据并进行显示

private DialogAddressList dialogAddressList;

dialogAddressList = new DialogAddressList(OrderDetailsActivity.this,R.style.MyDialog,
        loginTokenStr, addressListBeanDataList);

Window dialogWindow = dialogAddressList.getWindow();
dialogWindow.setGravity(Gravity.CENTER);

dialogAddressList.setOnCallBackAddLibaryListener(new DialogAddressList.OnCallBackChangeAddressListener() {
    @Override
    public void onCallBackChangeAddress(AddressListBean.AddressListBeanData addressListBeanData) {
        OrderDetailsActivity.this.addressListBeanData = addressListBeanData;

        orderDetailsBinding.tvUserNmae.setText(OrderDetailsActivity.this.addressListBeanData.getUserName());
        orderDetailsBinding.tvContact.setText(OrderDetailsActivity.this.addressListBeanData.getContact());
        orderDetailsBinding.tvAddress.setText(OrderDetailsActivity.this.addressListBeanData.getAddress());
    }
});

dialogAddressList.show();

 

 

 

你可能感兴趣的:(安卓开发)