lundi 25 novembre 2019

Select All list in Listview With Custom Adapter

I have a listview that I want to implement checkbox with option "SelectAll"

this is the fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <androidx.appcompat.widget.SearchView
                    android:id="@+id/Searchrecord"
                    android:layout_width="230dp"
                    android:layout_height="wrap_content" />

                <CheckBox
                    android:id="@+id/checkBox"
                    android:layout_width="119dp"
                    android:layout_height="match_parent"
                    android:text="Select All" />
            </LinearLayout>

            <ListView
                android:id="@+id/to_print"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:choiceMode="multipleChoice"
                />

</LinearLayout>

Here is the Code inside the fragment

public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        toolsViewModel = ViewModelProviders.of(this).get(ToolsViewModel.class);
        final View root = inflater.inflate(R.layout.fragment_tools, container, false);

        selectall = root.findViewById(R.id.checkBox);
        selectall.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                int itemCount = toprintlist.getCount();
                for(int i=0 ; i < itemCount ; i++)
                    toprintlist.setItemChecked(i, selectall.isChecked());
            }
        });

        toprintlist = root.findViewById(R.id.to_print);
        toprintlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                int checkedItemCount = getCheckedItemCount();

                if(toprintlist.getCount()==checkedItemCount)
                    selectall.setChecked(true);
                else
                    selectall.setChecked(false);
            }
        });
    }

this is my adapter class

public class toprintAdapter extends BaseAdapter
{
        Context context;
        ArrayList<String> account;
        ArrayList<String> name;
        ArrayList<String> address;
        ArrayList<String> meter_number;
        ArrayList<String> multiplier;
        ArrayList<String> type;
        ArrayList<String> billing_period_year;
        ArrayList<String> billing_period_month;
        ArrayList<String> last_reading;
        ArrayList<String> last_date_record;
        ArrayList<String> gen_system_charge;
        ArrayList<String> trans_system_chg;
        ArrayList<String> system_loss_chg;
        ArrayList<String> distr_system_chg;
        ArrayList<String> supply_system_chg;
        ArrayList<String> supply_retail_chg;
        ArrayList<String> meter_system_chg;
        ArrayList<String> meter_retail_chg;
        ArrayList<String> rsfc;
        ArrayList<String> life_line_subsidy;
        ArrayList<String> ppa_refund;
        ArrayList<String> iccs_adjustment;
        ArrayList<String> generation_vat;
        ArrayList<String> transmission_vat;
        ArrayList<String> system_loss_vat;
        ArrayList<String> distribution_vat;
        ArrayList<String> others_vat;
        ArrayList<String> senior_citizen_subsidy;
        ArrayList<String> fit_all;
        ArrayList<String> missionary_elec_spug;
        ArrayList<String> envir_charge;
        ArrayList<String> stranded_contract_cost;
        ArrayList<String> npc_scc_debt;
        ArrayList<String> current_reading;
        ArrayList<String> current_reading_date;
        ArrayList<String> kwh_used;
        ArrayList<String> total_gen_system_charge;
        ArrayList<String> toal_trans_system_chg;
        ArrayList<String> total_system_loss_chg;
        ArrayList<String> total_distr_system_chg;
        ArrayList<String> total_supply_system_chg;
        ArrayList<String> total_supply_retail_chg;
        ArrayList<String> total_meter_system_chg;
        ArrayList<String> total_meter_retail_chg;
        ArrayList<String> total_rsfc;
        ArrayList<String> total_life_line_subsidy;
        ArrayList<String> total_ppa_refund;
        ArrayList<String> total_iccs_adjustment;
        ArrayList<String> total_generation_vat;
        ArrayList<String> total_transmission_vat;
        ArrayList<String> total_system_loss_vat;
        ArrayList<String> total_distribution_vat;
        ArrayList<String> total_others_vat;

        public toprintAdapter(Context context2, ArrayList<String> account, ArrayList<String> name, ArrayList<String> address, ArrayList<String> meter_number, ArrayList<String> multiplier, ArrayList<String> type, ArrayList<String> billing_period_year, ArrayList<String> billing_period_month, ArrayList<String> last_reading, ArrayList<String> last_date_record, ArrayList<String> gen_system_charge, ArrayList<String> trans_system_chg, ArrayList<String> system_loss_chg, ArrayList<String> distr_system_chg, ArrayList<String> supply_system_chg, ArrayList<String> supply_retail_chg, ArrayList<String> meter_system_chg, ArrayList<String> meter_retail_chg, ArrayList<String> rsfc, ArrayList<String> life_line_subsidy, ArrayList<String> ppa_refund, ArrayList<String> iccs_adjustment, ArrayList<String> generation_vat, ArrayList<String> transmission_vat, ArrayList<String> system_loss_vat, ArrayList<String> distribution_vat, ArrayList<String> others_vat, ArrayList<String> senior_citizen_subsidy, ArrayList<String> fit_all, ArrayList<String> missionary_elec_spug, ArrayList<String> envir_charge, ArrayList<String> stranded_contract_cost, ArrayList<String> npc_scc_debt, ArrayList<String> current_reading, ArrayList<String> current_reading_date, ArrayList<String> kwh_used, ArrayList<String> total_gen_system_charge, ArrayList<String> toal_trans_system_chg, ArrayList<String> total_system_loss_chg, ArrayList<String> total_distr_system_chg, ArrayList<String> total_supply_system_chg, ArrayList<String> total_supply_retail_chg, ArrayList<String> total_meter_system_chg, ArrayList<String> total_meter_retail_chg, ArrayList<String> total_rsfc, ArrayList<String> total_life_line_subsidy, ArrayList<String> total_ppa_refund, ArrayList<String> total_iccs_adjustment, ArrayList<String> total_generation_vat, ArrayList<String> total_transmission_vat, ArrayList<String> total_system_loss_vat, ArrayList<String> total_distribution_vat, ArrayList<String> total_others_vat, ArrayList<String> ) {
                this.context = context2;
                this.account = account;
                this.name = name;
                this.address = address;
                this.meter_number = meter_number;
                this.multiplier = multiplier;
                this.type = type;
                this.billing_period_year = billing_period_year;
                this.billing_period_month = billing_period_month;
                this.last_reading = last_reading;
                this.last_date_record = last_date_record;
                this.gen_system_charge = gen_system_charge;
                this.trans_system_chg = trans_system_chg;
                this.system_loss_chg = system_loss_chg;
                this.distr_system_chg = distr_system_chg;
                this.supply_system_chg = supply_system_chg;
                this.supply_retail_chg = supply_retail_chg;
                this.meter_system_chg = meter_system_chg;
                this.meter_retail_chg = meter_retail_chg;
                this.rsfc = rsfc;
                this.life_line_subsidy = life_line_subsidy;
                this.ppa_refund = ppa_refund;
                this.iccs_adjustment = iccs_adjustment;
                this.generation_vat = generation_vat;
                this.transmission_vat = transmission_vat;
                this.system_loss_vat = system_loss_vat;
                this.distribution_vat = distribution_vat;
                this.others_vat = others_vat;
                this.senior_citizen_subsidy = senior_citizen_subsidy;
                this.fit_all = fit_all;
                this.missionary_elec_spug = missionary_elec_spug;
                this.envir_charge = envir_charge;
                this.stranded_contract_cost = stranded_contract_cost;
                this.npc_scc_debt = npc_scc_debt;
                this.current_reading = current_reading;
                this.current_reading_date = current_reading_date;
                this.kwh_used = kwh_used;
                this.total_gen_system_charge = total_gen_system_charge;
                this.toal_trans_system_chg = toal_trans_system_chg;
                this.total_system_loss_chg = total_system_loss_chg;
                this.total_distr_system_chg = total_distr_system_chg;
                this.total_supply_system_chg = total_supply_system_chg;
                this.total_supply_retail_chg = total_supply_retail_chg;
                this.total_meter_system_chg = total_meter_system_chg;
                this.total_meter_retail_chg = total_meter_retail_chg;
                this.total_rsfc = total_rsfc;
                this.total_life_line_subsidy = total_life_line_subsidy;
                this.total_ppa_refund = total_ppa_refund;
                this.total_iccs_adjustment = total_iccs_adjustment;
                this.total_generation_vat = total_generation_vat;
                this.total_transmission_vat = total_transmission_vat;
                this.total_system_loss_vat = total_system_loss_vat;
                this.total_distribution_vat = total_distribution_vat;
                this.total_others_vat = total_others_vat;   
        }


        public int getCount() {
                return account.size();
        }

        public Object getItem(int position) {
                return null;
        }

        public long getItemId(int position) {
                return 0L;
        }

        @Override
        public View getView(int position, View child, ViewGroup parent) {

                toprintAdapter.Holder holder;
                if (child == null) {
                        @SuppressLint("WrongConstant") LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService("layout_inflater");
                        child = layoutInflater.inflate(R.layout.meter_reading_to_print, null);
                        holder = new toprintAdapter.Holder();
                        holder.textprintdefaultaccount = child.findViewById(R.id.p_account_no);
                        holder.textprintdefaultname = child.findViewById(R.id.p_name);
                        holder.textprintdefaultaddress = child.findViewById(R.id.p_address);
                        holder.textprintdefaultmeter_number = child.findViewById(R.id.p_meter_number);
                        holder.textprintdefaultmultiplier = child.findViewById(R.id.p_multiplier);
                        holder.textprintdefaulttype = child.findViewById(R.id.p_type);
                        holder.textprintdefaultbilling_period_year = child.findViewById(R.id.p_billing_period_year);
                        holder.textprintdefaultbilling_period_month = child.findViewById(R.id.p_billing_period_month);
                        holder.textprintdefaultlast_reading = child.findViewById(R.id.p_last_reading);
                        holder.textprintdefaultlast_date_record = child.findViewById(R.id.p_last_record_date_reading);
                        holder.textprintdefaultgen_system_charge = child.findViewById(R.id.p_rate_gen_system_charge);
                        holder.textprintdefaulttrans_system_chg = child.findViewById(R.id.p_rate_trans_system_chg);
                        holder.textprintdefaultsystem_loss_chg = child.findViewById(R.id.p_rate_system_loss_chg);
                        holder.textprintdefaultdistr_system_chg = child.findViewById(R.id.p_rate_distr_system_chg);
                        holder.textprintdefaultsupply_system_chg = child.findViewById(R.id.p_rate_supply_system_chg);
                        holder.textprintdefaultsupply_retail_chg = child.findViewById(R.id.p_rate_supply_retail_chg);
                        holder.textprintdefaultmeter_system_chg = child.findViewById(R.id.p_rate_meter_system_chg);
                        holder.textprintdefaultmeter_retail_chg = child.findViewById(R.id.p_rate_meter_retail_chg);
                        holder.textprintdefaultrsfc = child.findViewById(R.id.p_rate_rsfc);
                        holder.textprintdefaultlife_line_subsidy = child.findViewById(R.id.p_rate_life_line_subsidy);
                        holder.textprintdefaultppa_refund = child.findViewById(R.id.p_rate_ppa_refund);
                        holder.textprintdefaulticcs_adjustment = child.findViewById(R.id.p_rate_iccs_adjsutment);
                        holder.textprintdefaultgeneration_vat = child.findViewById(R.id.p_rate_generation_vat);
                        holder.textprintdefaulttransmission_vat = child.findViewById(R.id.p_rate_transmission_vat);
                        holder.textprintdefaultsystem_loss_vat = child.findViewById(R.id.p_rate_system_loss_vat);
                        holder.textprintdefaultdistribution_vat = child.findViewById(R.id.p_rate_distribution_vat);
                        holder.textprintdefaultothers_vat = child.findViewById(R.id.p_rate_others_vat);
                        holder.textprintdefaultsenior_citizen_subsidy = child.findViewById(R.id.p_rate_senior_ctzn_subsidy);
                        holder.textprintdefaultfit_all = child.findViewById(R.id.p_rate_fit_all);
                        holder.textprintdefaultmissionary_elec_spug = child.findViewById(R.id.p_rate_missnry_elec_spug);
                        holder.textprintdefaultenvir_charge = child.findViewById(R.id.p_rate_envir_charge);
                        holder.textprintdefaultstranded_contract_cost = child.findViewById(R.id.p_rate_strndd_cntrct_cst);
                        holder.textprintdefaultnpc_scc_debt = child.findViewById(R.id.p_rate_npc_scc_debt);
                        holder.textloadcurrent_reading = child.findViewById(R.id.p_current_reading);
                        holder.textloadcurrent_reading_date = child.findViewById(R.id.p_current_date_reading);
                        holder.texttotalkwh_used = child.findViewById(R.id.p_kwh_used);
                        holder.texttotal_gen_system_charge = child.findViewById(R.id.p_total_gen_system_charge);
                        holder.texttotal_trans_system_chg = child.findViewById(R.id.p_total_trans_system_chg);
                        holder.texttotal_system_loss_chg = child.findViewById(R.id.p_total_system_loss_chg);
                        holder.texttotal_distr_system_chg = child.findViewById(R.id.p_total_distr_system_chg);
                        holder.texttotal_supply_system_chg = child.findViewById(R.id.p_total_supply_system_chg);
                        holder.texttotal_supply_retail_chg = child.findViewById(R.id.p_total_supply_retail_chg);
                        holder.texttotal_meter_system_chg = child.findViewById(R.id.p_total_meter_system_chg);
                        holder.texttotal_meter_retail_chg = child.findViewById(R.id.p_total_meter_retail_chg);
                        holder.texttotal_rsfc = child.findViewById(R.id.p_total_rsfc);
                        holder.texttotal_life_line_subsidy = child.findViewById(R.id.p_total_life_line_subsidy);
                        holder.texttotal_ppa_refund = child.findViewById(R.id.p_total_ppa_refund);
                        holder.texttotal_iccs_adjustment = child.findViewById(R.id.p_total_iccs_adjsutment);
                        holder.texttotal_generation_vat = child.findViewById(R.id.p_total_generation_vat);
                        holder.texttotal_transmission_vat = child.findViewById(R.id.p_total_transmission_vat);
                        holder.texttotal_system_loss_vat = child.findViewById(R.id.p_total_system_loss_vat);
                        holder.texttotal_distribution_vat = child.findViewById(R.id.p_total_distribution_vat);
                        holder.texttotal_others_vat = child.findViewById(R.id.p_total_others_vat);

                        child.setTag(holder);
                }
                else
                {
                        holder = (toprintAdapter.Holder)child.getTag();
                }
                holder.textprintdefaultaccount.setText(this.account.get(position));
                holder.textprintdefaultname.setText(this.name.get(position));
                holder.textprintdefaultaddress.setText(this.address.get(position));
                holder.textprintdefaultmeter_number.setText(this.meter_number.get(position));
                holder.textprintdefaultmultiplier.setText(this.multiplier.get(position));
                holder.textprintdefaulttype.setText(this.type.get(position));
                holder.textprintdefaultbilling_period_year.setText(this.billing_period_year.get(position));
                holder.textprintdefaultbilling_period_month.setText(this.billing_period_month.get(position));
                holder.textprintdefaultlast_reading.setText(this.last_reading.get(position));
                holder.textprintdefaultlast_date_record.setText(this.last_date_record.get(position));
                holder.textprintdefaultgen_system_charge.setText(this.gen_system_charge.get(position));
                holder.textprintdefaulttrans_system_chg.setText(this.trans_system_chg.get(position));
                holder.textprintdefaultsystem_loss_chg.setText(this.system_loss_chg.get(position));
                holder.textprintdefaultdistr_system_chg.setText(this.distr_system_chg.get(position));
                holder.textprintdefaultsupply_system_chg.setText(this.supply_system_chg.get(position));
                holder.textprintdefaultsupply_retail_chg.setText(this.supply_retail_chg.get(position));
                holder.textprintdefaultmeter_system_chg.setText(this.meter_system_chg.get(position));
                holder.textprintdefaultmeter_retail_chg.setText(this.meter_retail_chg.get(position));
                holder.textprintdefaultrsfc.setText(this.rsfc.get(position));
                holder.textprintdefaultlife_line_subsidy.setText(this.life_line_subsidy.get(position));
                holder.textprintdefaultppa_refund.setText(this.ppa_refund.get(position));
                holder.textprintdefaulticcs_adjustment.setText(this.iccs_adjustment.get(position));
                holder.textprintdefaultgeneration_vat.setText(this.generation_vat.get(position));
                holder.textprintdefaulttransmission_vat.setText(this.transmission_vat.get(position));
                holder.textprintdefaultsystem_loss_vat.setText(this.system_loss_vat.get(position));
                holder.textprintdefaultdistribution_vat.setText(this.distribution_vat.get(position));
                holder.textprintdefaultothers_vat.setText(this.others_vat.get(position));
                holder.textprintdefaultsenior_citizen_subsidy.setText(this.senior_citizen_subsidy.get(position));
                holder.textprintdefaultfit_all.setText(this.fit_all.get(position));
                holder.textprintdefaultmissionary_elec_spug.setText(this.missionary_elec_spug.get(position));
                holder.textprintdefaultenvir_charge.setText(this.envir_charge.get(position));
                holder.textprintdefaultstranded_contract_cost.setText(this.stranded_contract_cost.get(position));
                holder.textprintdefaultnpc_scc_debt.setText(this.npc_scc_debt.get(position));
                holder.textloadcurrent_reading.setText(this.current_reading.get(position));
                holder.textloadcurrent_reading_date.setText(this.current_reading_date.get(position));
                holder.texttotalkwh_used.setText(this.kwh_used.get(position));
                holder.texttotal_gen_system_charge.setText(this.total_gen_system_charge.get(position));
                holder.texttotal_trans_system_chg.setText(this.toal_trans_system_chg.get(position));
                holder.texttotal_system_loss_chg.setText(this.total_system_loss_chg.get(position));
                holder.texttotal_distr_system_chg.setText(this.total_distr_system_chg.get(position));
                holder.texttotal_supply_system_chg.setText(this.total_supply_system_chg.get(position));
                holder.texttotal_supply_retail_chg.setText(this.total_supply_retail_chg.get(position));
                holder.texttotal_meter_system_chg.setText(this.total_meter_system_chg.get(position));
                holder.texttotal_meter_retail_chg.setText(this.total_meter_retail_chg.get(position));
                holder.texttotal_rsfc.setText(this.total_rsfc.get(position));
                holder.texttotal_life_line_subsidy.setText(this.total_life_line_subsidy.get(position));
                holder.texttotal_ppa_refund.setText(this.total_ppa_refund.get(position));
                holder.texttotal_iccs_adjustment.setText(this.total_iccs_adjustment.get(position));
                holder.texttotal_generation_vat.setText(this.total_generation_vat.get(position));
                holder.texttotal_transmission_vat.setText(this.total_transmission_vat.get(position));
                holder.texttotal_system_loss_vat.setText(this.total_system_loss_vat.get(position));
                holder.texttotal_distribution_vat.setText(this.total_distribution_vat.get(position));
                holder.texttotal_others_vat.setText(this.total_others_vat.get(position));               
                return child;
        }

        public class Holder
        {
                TextView textprintdefaultaccount;
                TextView textprintdefaultname;
                TextView textprintdefaultaddress;
                TextView textprintdefaultmeter_number;
                TextView textprintdefaultmultiplier;
                TextView textprintdefaulttype;
                TextView textprintdefaultbilling_period_year;
                TextView textprintdefaultbilling_period_month;
                TextView textprintdefaultlast_reading;
                TextView textprintdefaultlast_date_record;
                TextView textprintdefaultgen_system_charge;
                TextView textprintdefaulttrans_system_chg;
                TextView textprintdefaultsystem_loss_chg;
                TextView textprintdefaultdistr_system_chg;
                TextView textprintdefaultsupply_system_chg;
                TextView textprintdefaultsupply_retail_chg;
                TextView textprintdefaultmeter_system_chg;
                TextView textprintdefaultmeter_retail_chg;
                TextView textprintdefaultrsfc;
                TextView textprintdefaultlife_line_subsidy;
                TextView textprintdefaultppa_refund;
                TextView textprintdefaulticcs_adjustment;
                TextView textprintdefaultgeneration_vat;
                TextView textprintdefaulttransmission_vat;
                TextView textprintdefaultsystem_loss_vat;
                TextView textprintdefaultdistribution_vat;
                TextView textprintdefaultothers_vat;
                TextView textprintdefaultsenior_citizen_subsidy;
                TextView textprintdefaultfit_all;
                TextView textprintdefaultmissionary_elec_spug;
                TextView textprintdefaultenvir_charge;
                TextView textprintdefaultstranded_contract_cost;
                TextView textprintdefaultnpc_scc_debt;
                TextView textloadcurrent_reading;
                TextView textloadcurrent_reading_date;
                TextView texttotalkwh_used;
                TextView texttotal_gen_system_charge;
                TextView texttotal_trans_system_chg;
                TextView texttotal_system_loss_chg;
                TextView texttotal_distr_system_chg;
                TextView texttotal_supply_system_chg;
                TextView texttotal_supply_retail_chg;
                TextView texttotal_meter_system_chg;
                TextView texttotal_meter_retail_chg;
                TextView texttotal_rsfc;
                TextView texttotal_life_line_subsidy;
                TextView texttotal_ppa_refund;
                TextView texttotal_iccs_adjustment;
                TextView texttotal_generation_vat;
                TextView texttotal_transmission_vat;
                TextView texttotal_system_loss_vat;
                TextView texttotal_distribution_vat;
                TextView texttotal_others_vat;            
                public Holder()
                {

                }
        }
}

I want to get this function I saw in [http://wptrafficanalyzer.in/blog/implementing-checkall-and-uncheckall-for-a-listview-in-android/#comment-1163160]

enter image description here

But I can't since I'm using Custom Layout, can someone help me? It's not giving an error but there's also no check function on the listview item




Aucun commentaire:

Enregistrer un commentaire