I have a custom ListView with a Text and a checkbox. The listView show all the days of the week. I'm trying to save this days in the preferences, to use this data in another screen. But I can't do it. I've read some other similars questions here but not work for me.
I must take into account that the user can save one day, four days, or all that he want. This code is working....
Thanks in advance
MainClass:
public class PlayAllDay extends Activity implements CompoundButton.OnCheckedChangeListener{
protected ListView lv;
protected ArrayList<Day> dayList;
protected HmPlayAllDayAdapter dayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_greetings_settings_play_all_day);
lv = (ListView) findViewById(R.id.listView);
displayDayList();
}
public void displayDayList(){
dayList = new ArrayList<Day>();
dayList.add(new Day("Monday"));
dayList.add(new Day("Tuesday"));
dayList.add(new Day("Wednesday"));
dayList.add(new Day("Thursday"));
dayList.add(new Day("Friday"));
dayList.add(new Day("Saturday"));
dayList.add(new Day("Sunday"));
dayAdapter = new PlayAllDayAdapter(dayList, this);
lv.setAdapter(dayAdapter);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
int position = lv.getPositionForView(buttonView);
if(position != ListView.INVALID_POSITION){
Day d = dayList.get(position);
d.setSelected(isChecked);
Toast.makeText(this, "Clicked on Day: " + d.getName(), Toast.LENGTH_SHORT).show();
}
}
}
}
MyAdapter class.
class Day{
protected String name;
protected boolean selected = false;
public Day(String name){
super();
this.name = name;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public boolean isSelected(){
return selected;
}
public void setSelected(boolean selected){
this.selected = selected;
}
}
public class PlayAllDayAdapter extends ArrayAdapter<Day> {
private List<Day> dayList;
private Context context;
public HmPlayAllDayAdapter(List<Day> dayList, Context context) {
super(context, R.layout.main_greetings_settings_list_days, dayList);
this.dayList = dayList;
this.context = context;
}
public static class DayHolder {
public TextView dayName;
public CheckBox chekBoxDay;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
DayHolder holder = new DayHolder();
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.main_greetings_settings_list_days, null);
holder.dayName = (TextView) v.findViewById(R.id.day);
holder.chekBoxDay = (CheckBox) v.findViewById(R.id.checkbox_day);
holder.chekBoxDay.setOnCheckedChangeListener((HmPlayAllDay) context);
} else {
holder = (DayHolder) v.getTag();
}
Day d = dayList.get(position);
holder.dayName.setText(d.getName());
holder.chekBoxDay.setChecked(d.isSelected());
holder.chekBoxDay.setTag(d);
return v;
}
}
Aucun commentaire:
Enregistrer un commentaire