I am trying to set a OnCheckedChangeListener to a CheckBox but my application exits in the run time. I also tried to set listeners for my Tex
You can't set the listener for your CheckBox from the ListView like that(it will probably throw a NullPointerException), instead set the listener in the getView() method(you'll also have to keep the CheckBox status so you don't end up with strange rows status). Bellow is an example:
public class ListViewActivity extends ListActivity {
public class MyCustomAdapter extends ArrayAdapter {
private ArrayList status = new ArrayList();
public MyCustomAdapter(Context context, int textViewResourceId,
String[] objects) {
super(context, textViewResourceId, objects);
for (int i = 0; i < objects.length; i++) {
status.add(false);
}
}
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(R.layout.adapters_listviewactivity_row,
parent, false);
}
TextView label = (TextView) row.findViewById(R.id.weekofday);
label.setText(month[position]);
CheckBox checkBox = (CheckBox) row.findViewById(R.id.checkBox);
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Toast.makeText(getApplicationContext(), "" + position,
Toast.LENGTH_SHORT).show();
if (isChecked) {
status.set(position, true);
} else {
status.set(position, false);
}
}
});
checkBox.setChecked(status.get(position));
return row;
}
}
String[] month = { "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" };
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new MyCustomAdapter(ListViewActivity.this,
R.layout.main, month));
}
}
For the TextView you'll have to do the same thing.