I had bind AlertDialog
with list in RecyclerView
. I am not able to retrieve the data from RecyclerView
adapter.
Here is my activity where I need to get data
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();
}
}
}
Here is adapter I had written for getting and handling the check selection in 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();
}
}
How to get data from Recycler Adapter to main screen.