So i have a listview in my main activity that contains a toggle button i want to initialize and use toggle button what i know is that i should initialise it in my getView..not in my onCreate since its in the listview but whether i set it in onCreate or getView i get a null pointer exception pointing to my line toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {.. what am i doing wrong
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if(row==null || row.getTag()==null){
LayoutInflater inflater = LayoutInflater.from(activity);
row = inflater.inflate(layoutResource,null);
holder=new ViewHolder();
holder.mTitle=(TextView)row.findViewById(R.id.name);
holder.mDate=(TextView)row.findViewById(R.id.dateText);
holder.mTime=(TextView)row.findViewById(R.id.timeText);
row.setTag(holder);
}else{
holder = (ViewHolder)row.getTag();
}
holder.myAlarm = getItem(position);
holder.mTitle.setText(holder.myAlarm.getTitle());
holder.mDate.setText(holder.myAlarm.getMonth()+" "+holder.myAlarm.getDay()+", "+holder.myAlarm.getYear());
ToggleButton toggle = (ToggleButton) findViewById(R.id.toggleButton);
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Toast.makeText(MainActivity.this, "IT IS CHECKED", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "IT IS NOT CHECKED", Toast.LENGTH_SHORT).show();
}
}
});
return row;
}
any help would be very appreciated
does Your toggle button include on each row of listview? if that instead of using this
ToggleButton toggle = (ToggleButton) findViewById(R.id.toggleButton);
use this
holder.toggle = (ToggleButton) row.findViewById(R.id.toggleButton);