2

Get Selected Data

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.

Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
  • You may want to check the answers at https://stackoverflow.com/questions/38417984/android-spinner-dropdown-checkbox on how to do this for a checkable spinner adapter. Not exactly the same, but you might get some ideas – Tyler V Jul 14 '18 at 16:11

1 Answers1

0

Your code can be shrinked to some lines.

Best Approach

You can easily set a AlertDialog with CheckBox list. That is provided by Android itself. Here is how you do that.

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();

Edition for your code (If you want know)

So you were doing wrong that just made single Health object globally, and changing its value at checkChange. That lead to change single object for every item of list. You can achieve like this.

By the below code you can get selected items list by method 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();
    }
}
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
  • If you want me to edit your code, then tell me, but best approach is this above. – Khemraj Sharma Jul 14 '18 at 16:21
  • No my condition are different I need to handle the check when closed and reopen of the alertdialog also I want to update the check with coming arraylist from webservice [Need Help For this Code](https://gist.github.com/ankittale/58638977d71f46d4e9da03279fd148b3) –  Jul 14 '18 at 17:32
  • Condition are I want to handle check selection while closing and reopening hold check and update that check with webservice –  Jul 14 '18 at 17:34