How to save checkbox in sqlite using recyclerView - android

1

So I have a project that has a checkbox list that when clicked it would record in a column in the bank that items were marked, so that's fine. So I did not take into account the size of the list and then when I did test using a list that used scrollrow and the items that went up were unmarked, I solved this using recyclerView. So now I do not know where to use the method to update checkbox for the bank, because in my project I did in the activity that exhibited the items with oncheckechanged, so now everything is in the class Adapter. I already have the method of updating the checkbox working the problem is that I do not know how to fit this method into the adapter class or how to get information (checboxes that have been marked) from the Adapter class and use them in another class. Thank you for your attention.

This is the method to update in the database in case checbox: This is being used in the Activiy TarefaCheckList to display the items to be checked:

            //Instancia do banco de dados
            BancoDeDados db = new BancoDeDados              

            //Objeto tarefa que será representado no checkbox
            Tarefa t = new Tarefa();

            //Metodo do banco para escolher a tarefa que sera 
            atualizado a conforme a posicao da lista
            t = db.selecionarTarefa(i);

            //Metodo que atualiza no banco, onde o numero 1 vai para coluna quando o item é checkado
            db.atualizaTarefa(new Tarefa(t.getCodigotar(), t.getCodigoCliente(), t.getNometarefa(), 1));

This is the code of the Adapter-TaskAdapter class with methods of the recyclerView:

                public class TarefaAdapter extends 
                            RecyclerView.Adapter {
                private Context context;
                private final ArrayList<Tarefa> tarefas;
                private SparseBooleanArray sba = new SparseBooleanArray();

                public TarefaAdapter(Context context, ArrayList<Tarefa> _tarefas) {
                this.context = context;
                this.tarefas = _tarefas;
                }


            //Metodos do recyclerView 
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            View view = LayoutInflater.from(context).inflate(R.layout.tarefa, parent, false);
            NossoViewHolder holder = new NossoViewHolder(view);
            return holder;
            }


            @Override
            public void onBindViewHolder(RecyclerView.ViewHolder _holder, final int position) {

            final NossoViewHolder holder = (NossoViewHolder) _holder;
            final Tarefa tarefa = tarefas.get(position);
            holder.bind(position);



            holder.nomeCheck.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                int posicao = position;

                if (!sba.get(posicao, false)) {

                    holder.nomeCheck.setChecked(true);
                    sba.put(posicao, true);


                } else {
                    holder.nomeCheck.setChecked(false);
                    sba.put(posicao, false);

                }


                }

            });


            holder.nomeTarefa.setText(tarefa.getNometarefa());

            }


            @Override
            public int getItemCount() {

            if (tarefas == null) {
                return 0;

            } else {
                return tarefas.size();
            }
            }

The class of the class NossoViewHolder below, this class is just below my methods of recyclerView:

                public class NossoViewHolder extends RecyclerView.ViewHolder {

                final CheckBox nomeCheck;
                final TextView nomeTarefa;


                public NossoViewHolder(View view) {
                    super(view);
                    nomeCheck = view.findViewById(R.id.tarefaChBox);
                    nomeTarefa = view.findViewById(R.id.nomeTarefa);


                }

                void bind(int position) {
                    if (!sba.get(position, false)) {
                    nomeCheck.setChecked(false);


                    } else {
                    nomeCheck.setChecked(true);

                    }


                }

                }
    
asked by anonymous 10.04.2018 / 23:46

0 answers