Android 如何从活动的alertdialog中检索多个选中复选框

Android 如何从活动的alertdialog中检索多个选中复选框,android,android-recyclerview,android-alertdialog,Android,Android Recyclerview,Android Alertdialog,我在RecyclerView中用列表绑定了AlertDialog。我无法从RecyclerView适配器检索数据 这是我需要获取数据的活动 package com.labs.ankitt.hma; import android.content.DialogInterface; import android.databinding.DataBindingUtil; import android.support.v7.app.AlertDialog; import android.support.

我在
RecyclerView
中用列表绑定了
AlertDialog
。我无法从
RecyclerView
适配器检索数据

这是我需要获取数据的活动

package com.labs.ankitt.hma;

import android.content.DialogInterface;
import android.databinding.DataBindingUtil;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.labs.ankitt.hma.databinding.ActivityHealthBinding;
import com.labs.ankitt.hma.databinding.ItemRecyclerContainerBinding;

import java.util.ArrayList;
import java.util.List;

public class HealthActivity extends AppCompatActivity implements View.OnClickListener {

    private ActivityHealthBinding healthBinding;
    private ItemRecyclerContainerBinding containerBinding;
    private ChoiceAdapter adapter;

    private List<Health> healthList = new ArrayList<>();
    private Health health;
    private String user[];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        healthBinding = DataBindingUtil.setContentView(this, R.layout.activity_health);

        addList();
        setListener();
    }

    private void addList() {
        user = getResources().getStringArray(R.array.person_array);
        for (String anUser : user) {
            health = new Health();
            health.setTitle(anUser);
            healthList.add(health);
        }
    }

    private void setListener() {
        healthBinding.buttonPerson.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (view == healthBinding.buttonPerson) {
            LayoutInflater inflater = this.getLayoutInflater();
            AlertDialog.Builder alert = new AlertDialog.Builder(this);
            containerBinding = DataBindingUtil.inflate(inflater, R.layout.item_recycler_container, null, false);
            alert.setView(containerBinding.getRoot());
            alert.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });

            alert.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });
            adapter= new ChoiceAdapter(this, healthList);
            containerBinding.recyclerContiner.setLayoutManager(new LinearLayoutManager(this));
            containerBinding.recyclerContiner.setHasFixedSize(true);
            containerBinding.recyclerContiner.setAdapter(adapter);
            AlertDialog dialog= alert.create();
            dialog.show();
        }
    }
}

如何将数据从回收器适配器获取到主屏幕。

您的代码可以压缩到某些行。

最佳方法 您可以使用
复选框
列表轻松设置
警报对话框
。这是安卓本身提供的。下面是你如何做到这一点

final CharSequence[] items = {" A "," B "," C "," D "};

final ArrayList seletedItems=new ArrayList();

AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Select One")
.setMultiChoiceItems(items, null, new DialogInterface.OnMultiChoiceClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int indexSelected, boolean isChecked) {
        if (isChecked) {
            seletedItems.add(indexSelected);
        } else if (seletedItems.contains(indexSelected)) {
            seletedItems.remove(Integer.valueOf(indexSelected));
        }
    }
}).setPositiveButton("OK", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int id) {
        //  Your code when user clicked on OK
    }
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int id) {
       //todo
    }
}).create();
dialog.show();
代码的版本(如果您想知道) 所以,您做得不对,只是将单个
运行状况
对象全局化,并在
检查更改
处更改其值。这将导致更改列表中每个项目的单个对象。你可以做到这样

通过下面的代码,您可以通过方法
adapter.getSelectedList()
获取所选项目列表

导入android.annotation.SuppressLint;
导入android.content.Context;
导入android.databinding.DataBindingUtil;
导入android.support.annotation.NonNull;
导入android.support.v7.widget.RecyclerView;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.CompoundButton;
导入java.util.ArrayList;
导入java.util.List;
公共类类扩展了RecyclerView.Adapter{
私人语境;
私人名单健康名单;
公共选项适配器(上下文、列表健康列表){
this.context=上下文;
this.healthList=healthList;
}
公共列表getSelectedList(){
所选列表=新建ArrayList();
用于(健康h:健康列表){
如果(h.isChecked()){
选中。添加(h);
}
}
返回选中的;
}
@非空
@凌驾
public ChoiceHolder onCreateViewHolder(@NonNull ViewGroup父级,int-viewType){
RowItemDataBinding dataBinding=DataBindingUtil.inflate(LayoutInflater.from)(上下文),
R.layout.row_item_data,null,false);
ChoiceHolder=新的ChoiceHolder(dataBinding.getRoot(),dataBinding);
报税表持有人;
}
@凌驾
public void onBindViewHolder(@NonNull final ChoiceHolder,@SuppressLint(“RecyclerView”)final int位置){
Health=healthList.get(位置);
holder.itemDataBinding.checkBoxUser.setOnCheckedChangeListener(新建CompoundButton.OnCheckedChangeListener()){
@凌驾
检查更改后的公共无效(复合按钮视图,布尔值已检查){
运行状况。设置已检查(已检查)
}
});
holder.itemDataBinding.checkBoxUser.setChecked(health.isChecked());
holder.itemDataBinding.checkTextUser.setText(health.getTitle());
}
@凌驾
public int getItemCount(){
返回healthList.size();
}
公共类ChoiceHolder扩展了RecyclerView.ViewHolder{
RowItemDataBinding itemDataBinding;
公共选项持有者(查看项目视图、行项目数据绑定数据绑定){
超级(项目视图);
itemDataBinding=数据绑定;
}
}
公共字符串getSelection(运行状况){
返回health.getTitle();
}
}

您可能需要查看关于如何为可检查微调器适配器执行此操作的答案。不完全一样,但你可能会有一些想法如果你想让我编辑你的代码,然后告诉我,但最好的方法是上面提到的。不,我的条件不同。我需要在关闭和重新打开alertdialog时处理检查。我还想用来自webservice的arraylist更新检查。条件是我想在关闭和重新打开时处理检查选择保持检查并用webservice更新检查
final CharSequence[] items = {" A "," B "," C "," D "};

final ArrayList seletedItems=new ArrayList();

AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Select One")
.setMultiChoiceItems(items, null, new DialogInterface.OnMultiChoiceClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int indexSelected, boolean isChecked) {
        if (isChecked) {
            seletedItems.add(indexSelected);
        } else if (seletedItems.contains(indexSelected)) {
            seletedItems.remove(Integer.valueOf(indexSelected));
        }
    }
}).setPositiveButton("OK", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int id) {
        //  Your code when user clicked on OK
    }
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int id) {
       //todo
    }
}).create();
dialog.show();
import android.annotation.SuppressLint;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;

import java.util.ArrayList;
import java.util.List;


public class classss extends RecyclerView.Adapter<ChoiceAdapter.ChoiceHolder> {

    private Context context;
    private List<Health> healthList;

    public ChoiceAdapter(Context context, List<Health> healthList) {
        this.context = context;
        this.healthList = healthList;
    }

    public List<Health> getSelectedList() {
        List<Health> selected = new ArrayList<>();
        for (Health h : healthList) {
            if (h.isChecked()) {
                selected.add(h);
            }
        }
        return selected;
    }

    @NonNull
    @Override
    public ChoiceHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RowItemDataBinding dataBinding = DataBindingUtil.inflate(LayoutInflater.from(context),
                R.layout.row_item_data, null, false);
        ChoiceHolder holder = new ChoiceHolder(dataBinding.getRoot(), dataBinding);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final ChoiceHolder holder, @SuppressLint("RecyclerView") final int position) {
        Health health = healthList.get(position);
        holder.itemDataBinding.checkBoxUser.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                health.setChecked(isChecked)
            }
        });
        holder.itemDataBinding.checkBoxUser.setChecked(health.isChecked());
        holder.itemDataBinding.checkTextUser.setText(health.getTitle());
    }

    @Override
    public int getItemCount() {
        return healthList.size();
    }

    public class ChoiceHolder extends RecyclerView.ViewHolder {

        RowItemDataBinding itemDataBinding;

        public ChoiceHolder(View itemView, RowItemDataBinding dataBinding) {
            super(itemView);
            itemDataBinding = dataBinding;
        }
    }

    public String getSelection(Health health) {
        return health.getTitle();
    }
}