How to save checkbox on sqlite using recyclerView - android

Asked

Viewed 275 times

0

So I have a project that has a checkbox list that when clicked would record in a column in the bank that items were marked, so far so good. Only I did not take into account the size of the list and then when I tested using a list that used scrollrow and the items that went up were unchecked, I solved this using recyclerView. Only now I don’t know where to use the method to update checkbox for the bank, because in my project I did in Activity that displayed the items with oncheckechanged, but now it’s all in the Adapter class. I already have the method of updating checkbox working the problem is that I do not know how to fit this method into the Adapter class or how to pick up 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 the case of checbox: This is being used in Activiy Tarefachecklist to display 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 Adapter-Tarefaadapter class code with recyclerView methods:

                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 code of the Nossoviewholder class below, this class is just below my recyclerView methods:

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

                    }


                }

                }
  • You can place the function within the class NossoViewHolder

  • @Valdeirpsr So when I instate the Database class it asks for the context and does not accept to put the following line of coded codeDados = new Database(Nossoviewholder.this); if you have another form, please let me know.

  • If possible post click on [Edit] and post the contents of the class NossoViewHolder. When you pass the view (in the method onCreateViewHolder), you - automatically - are passing the context. You can also pass the context as follows: new NossoViewHolder(view, view.getContext());

  • @Valdeirpsr, as requested I put the code of the class Nossoviewholder, thanks for the help!

  • @Valdeirpsr does not forget me! Blz?

  • Emerson, I’ve made a very basic example that you can follow: https://hastebin.com/ezokuqutop.java

  • @Valdeirpsr, it was worth a lot I will analyze it and adapt it by the example you gave me! Thank you very much for the help!

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.