dimanche 28 août 2016

how show checkboxes's names onClick on checkboxes

this method:

public static int showResultTotale(View v) {

        int totalAmount=0;

        for (Planet p : plAdapter.getBox()) {
            if (p.isSelected()){
                int quantitaInt= Integer.parseInt(p.getQuantità() );
                int distanceInt= Integer.parseInt(p.getDistance());
                totalAmount+=distanceInt * quantitaInt;
            }
        }
        return totalAmount;
    }

works onClick a button;but i want that this method works when i click on all checkbox.

This is my code:

public  class MyListFragment extends Fragment implements
        CompoundButton.OnCheckedChangeListener{



    ListView lv;
    ArrayList<Planet> planetList;
    static PlanetAdapter plAdapter;
    private TextView txtName;
    private TextView txtEmail;
    private Button btnLogout;
    ListView listView;
    String user="";
    private Spinner spinner;
    String selState;
    EditText cristo;
    private String zao;
    private CheckBox ck;
    private SQLiteHandler db;
    private SessionManager session;
    BirraAdapter biAdapter;
    PlanetAdapter.PlanetHolder holder;
    private static Context context = null;
    private static FragmentActivity mInstance;

    Integer[] imageId = {
            R.mipmap.androtuto,
            R.mipmap.ic_launcher,
            R.mipmap.ic_launcher,
            R.mipmap.ic_launcher,
            R.mipmap.ok,
            /*R.drawable.image6,
            R.drawable.image7*/

    };



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the custom_spinner_items for this fragment
        //super.onDestroy();

        SharedPreferences settings = getContext().getSharedPreferences("states", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.clear();
        editor.commit();

        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_list2, container, false);





        lv = (ListView) rootView.findViewById(R.id.listview);
        ListAdapter listAdapter = new MyListAdapter(getContext());
        lv.setAdapter(listAdapter);



        context = getActivity();
        mInstance = getActivity();
    //  txtName = (TextView) rootView.findViewById(R.id.name);
    //  txtEmail = (TextView) rootView.findViewById(R.id.numero_telefonico);
        btnLogout = (Button) rootView.findViewById(R.id.btnLogout);
        //spinner  = (Spinner) rootView.findViewById(R.id.simpleSpinner);
//      selState = (String) spinner.getSelectedItem().toString();
        //cristo=(EditText)rootView.findViewById(R.id.editText2);
        ck=(CheckBox)rootView.findViewById(R.id.chk_box);




        return rootView;
    }







    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);



        lv = (ListView)getView().findViewById(R.id.listview);
        displayPlanetList();


    }


    private void displayPlanetList() {

        planetList = new ArrayList<Planet>();
        planetList.add(new Planet("Margherita", "6", "€","(pomodoro e mozzarella)"));
        planetList.add(new Planet("Diavola", "7","€","(pomodoro,mozzarella e salsiccia piccante)"));
        planetList.add(new Planet("Bufalina", "5","€","(pomodoro e mozzarella di bufala)"));
        planetList.add(new Planet("Marinara", "5", "€","(pomodoro)"));
        planetList.add(new Planet("Viennese", "4", "€", "(pomodoro,mozzarella e wrustel)"));

        plAdapter = new PlanetAdapter(planetList, getContext(),imageId) {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                int pos = lv.getPositionForView(buttonView);
                if (pos != ListView.INVALID_POSITION) {
                    Planet p = planetList.get(pos);
                    p.setSelected(isChecked);


            /*Toast.makeText(
                    getActivity(),
                    "Clicked on Pizza: " + p.getName() + ". State: is "
                            + isChecked, Toast.LENGTH_SHORT).show();*/
                }


            }
        };

        lv.setAdapter(plAdapter);
    }



    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        String pizzeOrdinate="";

        for (Planet p : plAdapter.getBox()) {
            if (p.isSelected()){
                pizzeOrdinate+="\n" + p.getName() + " " +p.getTipo()  + " " +p.getDistance() + "€" + "q.tà :" + p.getQuantità();

            }
        }
        Toast.makeText(
                getActivity(),
                "Clicked on Pizza: " + pizzeOrdinate + ". State: is "
                        + isChecked, Toast.LENGTH_SHORT).show();

    }


    @TargetApi(Build.VERSION_CODES.KITKAT)


    public String showResultTotale2(View v) {

        //selState=cristo.getText().toString();


        int totalAmount=0;
        String pizzeOrdinate="";

        for (Planet p : plAdapter.getBox()) {
            if (p.isSelected()){
                pizzeOrdinate+="\n" + p.getName() + " " +p.getTipo()  + " " +p.getDistance() + "€" + "q.tà :" + p.getQuantità();

            }
        }
        return pizzeOrdinate;
    }


    public static int showResultTotale(View v) {

        int totalAmount=0;

        for (Planet p : plAdapter.getBox()) {
            if (p.isSelected()){
                int quantitaInt= Integer.parseInt(p.getQuantità() );
                int distanceInt= Integer.parseInt(p.getDistance());
                totalAmount+=distanceInt * quantitaInt;
            }
        }
        return totalAmount;
    }





}

Who can help me?

THANKS IN ADVANCE!




Aucun commentaire:

Enregistrer un commentaire