我已将 AlertDialog
与 RecyclerView
中的列表绑定(bind)。我无法从 RecyclerView
适配器检索数据。
这是我需要获取数据的 Activity
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();
}
}
}
这是我为获取和处理 RecyclerView
中的检查选择而编写的适配器
package com.labs.ankitt.hma;
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.util.Log;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Toast;
import com.labs.ankitt.hma.databinding.RowItemDataBinding;
import java.util.ArrayList;
import java.util.List;
public class ChoiceAdapter extends RecyclerView.Adapter<ChoiceAdapter.ChoiceHolder> {
private Context context;
private List<Health> healthList;
private ArrayList<Void> mCheckedState;
private Health health;
public ChoiceAdapter(Context context, List<Health> healthList) {
this.context = context;
this.healthList = healthList;
mCheckedState = new ArrayList<>();
}
@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 = healthList.get(position);
holder.itemDataBinding.checkBoxUser.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
health = healthList.get(position);
if (isChecked) {
health.setChecked(true);
} else {
health.setChecked(false);
}
}
});
holder.itemDataBinding.checkBoxUser.setChecked(healthList.get(position).isChecked());
getSelection(healthList.get(position));
Log.i("Test", "onBindViewHolder: " +healthList.get(position).getTitle());
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();
}
}
如何从 Recycler Adapter 获取数据到主屏幕。
最佳答案
您的代码可以缩减为几行。
最佳方法
您可以使用CheckBox
列表轻松设置AlertDialog
。那是安卓自己提供的。以下是您的操作方法。
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();
你的代码的版本(如果你想知道)
所以你做错了,只是在全局范围内创建了单个 Health
对象,并在 checkChange
处更改了它的值。这导致为列表的每个项目更改单个对象。你可以这样实现。
通过下面的代码,您可以通过方法adapter.getSelectedList()
获取选中的项目列表。
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();
}
}
关于android - 如何从 Activity 警报对话框中检索多项检查选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51341107/