Learn which checkboxes are selected on Adapter and perform operations on Activity based on selected checks

Asked

Viewed 158 times

0

inserir a descrição da imagem aquiHello, I have a list (Listview) and when I select option 1 in my spinner it from Visible in the framelayout to show on the screen a checkbox to cadad list item with status 0p, I wanted to know which checksboxs are selected in Adapter so that in Activity I can include in a list and change status. This is my Activity:

spinnerSync.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(final AdapterView<?> parent, final View view,  final int position, long id) {

            //Guardo na variavel estatica para no adapter fazer a verificação se mostra ou não a check
            POSITION=position;
                //Cria a lista na activity
                adapter2= new SyncAdapter2(listaForm, getApplicationContext());
                listView.setAdapter(adapter2);



                buttonSync.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //Verificar quais itens da listView estão checkados(checkbox)
                        // Pegar a posição dos itens checkado

                        //inclui form no SQLite e consequentemente na lista agenda
                        if (listaForm.get(position).getStatus()==0) {
                            dao.incluir(listaForm);
                        }


                        Handler handler = new Handler();
                        handler.postDelayed(
                                new Runnable() {
                                    public void run() {
                                        load.setVisibility(View.GONE);
                                    }
                                }, 3000L);
                        load.setVisibility(View.VISIBLE);
                    }
                });
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

And this my Adapter:

@Override
public View getView(final int position, final View convertView, ViewGroup parent) {
    View v = View.inflate(context, R.layout.sinc_item_listview2, null);

    checkBox = (CheckBox) v.findViewById(R.id.checkbox);

    checkBox.setVisibility(View.GONE);

    if (SincActivity.POSITION == 1) {

        if (listForm.get(position).getStatus() != 1)
            checkBox.setVisibility(View.VISIBLE);
            TextView tvDayNumber = (TextView) v.findViewById(R.id.tvDayNumber);
            tvDayNumber.setText(listForm.get(position).getDataForm());
            tvDayNumber.setTypeface(tf);
            TextView tvDayWeek = (TextView) v.findViewById(R.id.tvDayWeek);
            tvDayWeek.setText(listForm.get(position).getDataForm());
            tvDayWeek.setTypeface(tf);
            TextView tvLocation = (TextView) v.findViewById(R.id.tvLocation);
            tvLocation.setText("#" + listForm.get(position).getNomeLoja());
            tvLocation.setTypeface(tf);
            TextView tvHour = (TextView) v.findViewById(R.id.tvHour);
            tvHour.setText(listForm.get(position).getHora() + " | ");
            tvHour.setTypeface(tf);
            TextView tvForms = (TextView) v.findViewById(R.id.tvForm);
            tvForms.setText(listForm.get(position).getNomeFom());
            tvForms.setTypeface(tf);

            ImageView imageInfo = (ImageView) v.findViewById(R.id.imageInfo);
            if (listForm.get(position).getStatus() == 1) {
                imageInfo.setImageResource(R.drawable.checked);
            } else {
                imageInfo.setImageResource(R.drawable.exclamation);
            }
    } else {
        TextView tvDayNumber = (TextView) v.findViewById(R.id.tvDayNumber);
        tvDayNumber.setText(listForm.get(position).getDataForm());
        tvDayNumber.setTypeface(tf);
        TextView tvDayWeek = (TextView) v.findViewById(R.id.tvDayWeek);
        tvDayWeek.setText(listForm.get(position).getDataForm());
        tvDayWeek.setTypeface(tf);
        TextView tvLocation = (TextView) v.findViewById(R.id.tvLocation);
        tvLocation.setText("#" + listForm.get(position).getNomeLoja());
        tvLocation.setTypeface(tf);
        TextView tvHour = (TextView) v.findViewById(R.id.tvHour);
        tvHour.setText(listForm.get(position).getHora() + " | ");
        tvHour.setTypeface(tf);
        TextView tvForms = (TextView) v.findViewById(R.id.tvForm);
        tvForms.setText(listForm.get(position).getNomeFom());
        tvForms.setTypeface(tf);

        ImageView imageInfo = (ImageView) v.findViewById(R.id.imageInfo);
        if (listForm.get(position).getStatus() == 1) {
            imageInfo.setImageResource(R.drawable.checked);
        } else {
            imageInfo.setImageResource(R.drawable.exclamation);
        }
    }



    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked){
                Toast.makeText(context,"Checkado",Toast.LENGTH_SHORT).show();
                Log.w("id",position+"");
                listaInteger.add(listForm.get(position).getIdForm());

            }else{
                Toast.makeText(context,"uncheck",Toast.LENGTH_SHORT).show();
                Log.w("id",position+"");
                listaInteger.remove(listForm.get(listForm.get(position).getIdForm()).getIdForm());
            }
        }
    });


    v.setTag(listForm.get(position).getIdForm());

    return v;
}
  • I don’t understand what you really want to do. I think that with getAdapter() it is possible to solve this, if I understand. Put a print, because you cannot imagine the screen.

  • print

No answers

Browser other questions tagged

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