尝试通过复选框删除自定义待办事项列表的每一行的膨胀视图时遇到问题?

时间:2015-06-11 18:46:25

标签: android listview checkbox android-studio

我通过可编辑的文本填充列表视图,列表视图是自定义的,每行包含一个textview和一个复选框,现在我希望通过单击复选框并通过删除按钮删除每个行的膨胀视图但是我如何操纵values.get(position) todoCheckBox.isChecked()以便我能够移除每个位置,其次如何删除我之前完成的每一行的膨胀视图values.remove(positions)但仅限于从arraylist中删除了元素,而todoTextView.setText(“”)只更改了textview的文本,但该行的视图仍然存在。

请记住,我是初学者。 。

public class todoFragment extends ListFragment{

private EditText mToDoField;
private Button mAdd;
private Button mDelete;
UsersAdapter mAdapter;
private TextView todoTextView;
private CheckBox todoCheckBox;
ListView listViewToDo;
String val;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setTitle(R.string.todo_title);
}

public class UsersAdapter extends ArrayAdapter<String>{

public Context context;
public ArrayList<String> values;

public UsersAdapter(Context context, ArrayList<String> values) {
    super(context, 0, values);

    this.context = context;
    this.values = values;
}

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

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    convertView = LayoutInflater.from(getContext()).inflate(R.layout.todo_list, parent, false);

    todoTextView = (TextView) convertView.findViewById(R.id.todo_TextView);
    todoCheckBox = (CheckBox) convertView.findViewById(R.id.todo_CheckBox);

    todoTextView.setText(values.get(position));

    todoCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

      @Override
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
          //Toast.makeText(getContext(), " CheckBox Status: " + isChecked, Toast.LENGTH_LONG).show();

          if (todoCheckBox.isChecked()){

              val = "true";
              values.get(position).contains(val);
          }


          mDelete.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {

                  if (mDelete.isPressed() && (values.get(position).contains(val))){

                     //convertView.clearFocus(position);

                      todoTextView.setText("");

                  }
              }
          });


      }
     });

    return convertView;
}
}



@TargetApi(9) // remember this for isEmpty()
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_todo, container, false);

ArrayList<String> todoList = new ArrayList<String>();
mAdapter = new UsersAdapter(getActivity(), todoList);
listViewToDo = (ListView) v.findViewById (android.R.id.list);
listViewToDo.setAdapter(mAdapter);

mToDoField = (EditText) v.findViewById(R.id.todo_editText);
mDelete = (Button) v.findViewById(R.id.delete_button);

mAdd = (Button)v.findViewById(R.id.add_button);
mAdd.setOnClickListener(new View.OnClickListener() {
    @Override
        public void onClick(View view) {

        String toDo = mToDoField.getText().toString().trim();

        if (toDo.isEmpty()){
            return;
        }

        mAdapter.add(toDo);

        mToDoField.setText("");
    }
});

return v;
}

1 个答案:

答案 0 :(得分:0)

保留values.remove(positions),然后在适配器上调用notifyDataSetChanged()