I am adding data through an editable text to a listview, listview is custom made and each row contains a textview and a checkbox, for now I want a toast message to appear whenever I click on a checkbox of each row for that I am implementing View.OnClickListener
on the custom adapter and through the OnClick
method setting the if else conditions so that my toast messages can appear but the problem here is while the listview is being populated the checkboxes are not working at all, I think the problem might be that the convertview might be inflating incorrectly or the checkbox might be working with the wrong view, can someone please help me on this?
public class todoFragment extends ListFragment{
private EditText mToDoField;
private Button mAdd;
UsersAdapter mAdapter;
private TextView todoTextView;
private CheckBox todoCheckBox;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setTitle(R.string.todo_title);
}
public class UsersAdapter extends ArrayAdapter<String> implements View.OnClickListener{
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 void onClick(View view) {
todoCheckBox.setOnClickListener(this);
if (todoCheckBox.isChecked()){
Toast.makeText(getContext(), "CheckBox is clicked.", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(getContext(), "CheckBox is not clicked.", Toast.LENGTH_LONG).show();
}
}
@Override
public View getView(int position, View convertView, 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));
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);
ListView listViewToDo = (ListView) v.findViewById (android.R.id.list);
listViewToDo.setAdapter(mAdapter);
mToDoField = (EditText) v.findViewById(R.id.todo_editText);
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()){
}
mAdapter.add(toDo);
mToDoField.setText("");
}
});
return v;
}
Aucun commentaire:
Enregistrer un commentaire