怎么和不同类的变量?

时间:2016-05-06 10:52:01

标签: android listview android-fragments android-studio

我有两个listview,当我点击botton时,我想要只选择两个listviews中选中的复选框的int值。我会知道我怎么做 我希望你能帮助我。

由于

这是我的代码:

public class MyListFragment extends Fragment implements
    android.widget.CompoundButton.OnCheckedChangeListener {

ListView lv;
ArrayList<Planet> planetList;
PlanetAdapter plAdapter;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_list2, container, false);
    Button mButton = (Button) rootView.findViewById(R.id.button);
    mButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
        showResult(v);


        }
    });
    //return inflater.inflate(R.layout.fragment_list2, container, false);
    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, "€"));
    planetList.add(new Planet("Diavola", 7,"€"));
    planetList.add(new Planet("Bufalina", 5,"€"));
    planetList.add(new Planet("Marinara", 5,"€"));
    planetList.add(new Planet("Viennese", 4,"€"));

    plAdapter = new PlanetAdapter(planetList, getContext()) {
        @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) {

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


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

}


public void showResult(View v) {
    String  result = "Selected Product are :";
    int totalAmount=0;
    String b="";

    for (Planet p : plAdapter.getBox()) {
        if (p.selected){

            result += "\n" + p.name+" "+p.distance+"€"+"q.tà :"+p.getQuantità();
            int quantitaInt= Integer.parseInt(p.getQuantità() );
            totalAmount+=p.distance * quantitaInt;
            b=String.valueOf(totalAmount);


        }
    }

第二片:

public class ThreeFragment extends Fragment implements
        android.widget.CompoundButton.OnCheckedChangeListener {

    ListView lv2;
    ArrayList<Birra> birraList;
    BirraAdapter biAdapter;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_three, container, false);
        Button mButton = (Button) rootView.findViewById(R.id.button2);
        mButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showResult(v);


            }
        });
        //return inflater.inflate(R.layout.fragment_list2, container, false);
        return  rootView;
    }


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

        lv2 = (ListView)getView().findViewById(R.id.listview2);
        displayBirraList();
    }


    private void displayBirraList() {

        birraList = new ArrayList<Birra>();
        birraList.add(new Birra("Paulaner", 6, "€"));
        birraList.add(new Birra("Forst", 7,"€"));
        birraList.add(new Birra("Peroni", 5,"€"));
        birraList.add(new Birra("Corona", 5,"€"));
        birraList.add(new Birra("Nastro Azzurro", 4,"€"));
        biAdapter = new BirraAdapter(birraList, getContext()) {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int pos = lv2.getPositionForView(buttonView);
                if (pos != ListView.INVALID_POSITION) {
                    Birra b = birraList.get(pos);
                    b.setSelected(isChecked);


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


            }
        };
        lv2.setAdapter(biAdapter);
    }



    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 Planet: " + p.getName() + ". State: is "
                        + isChecked, Toast.LENGTH_SHORT).show();*//*
        }*/

    }


    public void showResult(View v) {
        String  result = "Selected Product are :";
        int totalAmount=0;
        String c="";

        for (Birra b : biAdapter.getBox()) {
            if (b.selected){

                result += "\n" + b.name+" "+b.distance+"€"+"q.tà :"+b.getQuantità();
                int quantitaInt= Integer.parseInt(b.getQuantità());
                totalAmount+=b.distance * quantitaInt;
                c=String.valueOf(totalAmount);


            }
        }

0 个答案:

没有答案