选中后如何获取Checkboxes文本

时间:2016-08-28 08:36:00

标签: java android listview checkbox android-checkbox

这种方法:

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

可以点击一个按钮;但我希望当我点击所有复选框时此方法有效。

这是我的代码:

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

1 个答案:

答案 0 :(得分:0)

如果要在通过onClick()方法检查后显示CheckBoxes文本,请执行此操作

public  class MyListFragment extends Fragment implements View.OnClickListener{
..........

@Override
public void onClick(View v) {

    switch (v.getId()) {
        case checkBox1.getId:

            Toast.makeText(
                    getActivity(),
                    "blah blah", Toast.LENGTH_SHORT).show();
                }
            ...
            break;

        // Add other cases here

    }

}
}

并按checkBox1.setOnClickListener(this);

加载checkBox1和其他内容

修改 抱歉,我没有看到您正在使用ListView lv,因此您也可以迭代ListView以查找checkbox的状态。对于旧的复选框,最近未经检查存储在收藏中以供您说服,如列表

public  class MyListFragment extends Fragment implements View.OnClickListener{
List list;
..........

@Override
public void onClick(View v) {
  for (int i = 0; i < lv.getChildCount(); i++) {
     View v = lv.getChildAt(i);
     if (v instanceof CheckBox) {
     if (((CheckBox) v).isChecked())
      //use toast
     else
       list.add("uncheck one");
  // use toast for unchecked one
  } 
}