【问题标题】:how to get Checkboxes text after selected选择后如何获取复选框文本
【发布时间】:2016-08-30 17:35:55
【问题描述】:

这个方法:

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

【问题讨论】:

  • 选择后是否要获取复选框文本..??问题令人困惑,请编辑您的问题
  • 是的!!!!!!!!!!!!你明白
  • toast 必须显示选中的旧复选框和选中的新复选框

标签: java android listview checkbox android-checkbox


【解决方案1】:

如果您想在通过 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 的状态,因为您的旧复选框最近被 Unchecked Store in Collection 用于像 List 一样说服您

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

【讨论】:

  • 但在我的列表视图中,复选框的 id 相同
  • 但是用你的代码 toast 显示当前选中的复选框....我还想要之前选中的复选框....
猜你喜欢
  • 2011-03-11
  • 1970-01-01
  • 2013-10-26
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多