mardi 16 juin 2015

Having problems while trying to delete single rows from a custom ToDo list?

I want to delete selected rows from a todo list by clicking on the checkbox and deleting them trough the delete button, for that I am within my custom adapter setting a setOnCheckedChangeListener on my checkbox and setOnClickListener on my delete button, now keep in mind that the delete button is inflated on my fragment view and I am using it on my row view, but the problem is only the last element from my todo list is getting deleted not the rest of them, I tried working within the fragment view and notify the adapter later on but all I got was a null pointer error on my checkbox.

Row View:

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();

            mDelete.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    if (mDelete.isPressed() && (todoCheckBox.isChecked())) {

                     //convertView.clearFocus(position);
                     mAdapter.clear();
                     //mAdapter.notifyDataSetChanged();

                    }
                }
            });
        }
    });

Fragment View:

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

} }




Aucun commentaire:

Enregistrer un commentaire