我的日历包含GridView
,其中包含TextView
个项目。当日历布局打开时,用户可以单击项目。
它显示一个图标,单击时不显示图标。我想保存点击的TextView
图标,以便当用户关闭并再次打开日历布局时,它会显示点击的TextView
图标。
我搜索了所有相关问题,但无法解决我的问题。哪种方式是保存图标的最佳方式?我也尝试过使用SharedPreferences
CalendarView(clickListener)
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Date date = (Date)parent.getItemAtPosition(position);
Date currentDate = new Date();
textView = (TextView)view.findViewById(R.id.text_view);
if(date.after(currentDate)) {
Toast.makeText(getContext(), "Day in the future", Toast.LENGTH_SHORT).show();
}
if(date.compareTo(currentDate) == -1 ) {
if(textView.getBackground() == null) {
editor.putBoolean("selected", true);
editor.apply();
textView.setBackgroundResource(R.drawable.draw_cross);
} else {
editor.putBoolean("selected", false);
editor.apply();
textView.setBackground(null);
}
}
}
});
}
CalendarAdapter
public class CalendarAdapter extends ArrayAdapter<Date> {
// days with events
private HashSet<Date> eventDays;
// for view inflation
private LayoutInflater inflater;
@NonNull
@Override
public Context getContext() {
return super.getContext();
}
public CalendarAdapter(Context context, ArrayList<Date> days, HashSet<Date> eventDays)
{
super(context, R.layout.control_calendar_day, days);
this.eventDays = eventDays;
inflater = LayoutInflater.from(context);
}
@Nullable
@Override
public Date getItem(int position) {
return getItem(position);
}
@Override
public long getItemId(int position) {
return getItemId(position);
}
@Override
public View getView(int position, View view, ViewGroup parent) {
// day in question
Date date = getItem(position);
int day = date.getDate();
int month = date.getMonth();
int year = date.getYear();
// today
Date today = new Date();
// inflate item if it does not exist yet
if (view == null)
view = inflater.inflate(R.layout.control_calendar_day, parent, false);
int screenHeight = ((Activity) getContext()).getWindowManager()
.getDefaultDisplay().getHeight();
view.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.MATCH_PARENT, screenHeight/7));
TextView text = (TextView)view.findViewById(R.id.text_view);
text.setTextColor(Color.BLACK);
text.setTextSize(25);
if (month != today.getMonth() || year != today.getYear()) {
// if this day is outside current month, grey it out
text.setTextColor(getContext().getResources().getColor(R.color.greyed_out));
}
else if (day == today.getDate()) {
// if it is today, set it to blue/bold
text.setTypeface(null, Typeface.NORMAL);
text.setTextColor(getContext().getResources().getColor(R.color.today));
text.setTextSize(45);
}
// set text
text.setText(String.valueOf(date.getDate()));
return view;
}
}