How To Get The Position Of Toggle Button Clicked Within Listview?
I have a listview that contains alarm times and a toggle button to turn off/on that alarm. When I click the toggle buttons for the specific listview, I would like to set/cancel tha
Solution 1:
I suggest you create a custom ArrayAdapter
class and override the getView()
method. From that method, you have access to the position of the list item from which the toggle was set, and can create a unique OnCheckedChangeListener
for each toggle button, passing it the position of the list.
I'm assuming you already have an XML layout file for whatever is supposed to be held in each ListView item, since you mention an alarm and toggle buttons.
Update:
In order to get the alarm time and send it to the enableAlarm()
method, you need to save it as a final
variable within getView()
so you can access it within the onCheckedChangeListener
. Look at the code I added after getting the ToggleButton.
@overridepublic View getView(int position, View convertView, ViewGroup parent) {
Viewv= convertView;
if (v == null) {
LayoutInflaterinflater= getContext().getLayoutInflater();
v = inflater.inflate(R.layout.alarm_item, parent, false);
}
ToggleButtontoggle= (ToggleButton) v.findViewById(R.id.alarmToggle);
//get the alarm timeTextViewtimeView= (TextView) v.findViewById(R.id.theAlarmTextView);
finalStringalarmTime= timeView.getText();
toggle.setOnCheckedChangeListener(newOnCheckedChangeListener() {
publicvoidonCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
//modify your enableAlarm method to take in the time as a String
enableAlarm(buttonView, alarmTime);
}
}
});
return v;
}
Post a Comment for "How To Get The Position Of Toggle Button Clicked Within Listview?"