Android取消选中复选框时如何删除项目

时间:2017-02-24 02:54:17

标签: android checkbox

我设法在我的复选框被检查时我的textview将附加复选框中的文本,否则当复选框取消选中时,文本将被移除。如果取消选中该复选框,如何从arraylist中删除。

public class condimentlist extends BaseAdapter {

    LayoutInflater mInflater;

    private ArrayList<Integer> positions = new ArrayList<Integer>();
    public ArrayList<Integer> getPositions() {
        return positions;
    }

    public condimentlist() {
        mInflater = LayoutInflater.from(getActivity());
    }

    @Override
    public int getCount() {
        return CondimentList.size();
    }

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

    @Override
    public long getItemId(int position) {
        return position;
    }

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

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.condimentlistview_details, null);
        }
        condcb  = (CheckBox) convertView.findViewById(R.id.condcb);
        final TextView tremark = (TextView) convertView.findViewById(R.id.Tremark);

        final TextView condimenttext=(TextView)convertView.findViewById(R.id.condimenttext);
        Condiment myObj = CondimentList.get(position);
        condimenttext.setText("" +myObj.getCondimentName());

        condcb.setOnClickListener(new View.OnClickListener() {
                                      @Override
                                      public void onClick(View v) {
                                          String i = condimenttext.getText().toString();
                                          Toast.makeText(getActivity(),i,Toast.LENGTH_SHORT).show();
                                          SharedPreferences pref = getActivity().getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
                                          SharedPreferences.Editor editor1 = pref.edit();
                                          if (((CheckBox) v).isChecked()) {
                                              data.add(i);
                                              String array[] = data.toArray(new String[0]);
                                              for (int j = 0; j < array.length; j++) {
                                                        remark.append(String.valueOf(array[j]));
                                              }
                                          } else {
                                              SharedPreferences settings = getActivity().getSharedPreferences("checkbox1", 0);
                                              SharedPreferences.Editor editor = settings.edit();
                                              editor.putBoolean(((CheckBox) v).getText().toString(), false);
                                              editor.commit();
                                              data.remove(position)

                                          }

                                      }
                                  }
        );
        return convertView;
    }
}

0 个答案:

没有答案