Ich habe einen Code, den ich gemacht habe, um einen Kalender mit einem GridView
anzuzeigen, und ich möchte wissen, ob es eine einfachere Methode zum Erstellen eines Kalenders gibt oder ob es eine Möglichkeit gibt, die ich ändern kann das Erscheinen eines einzelnen Artikels in der GridView
für den aktuellen Tag. Code ist unten.Erstellen eines Kalenders mit einer GridView
TextView tvMonth;
GridView gvCal;
DateFormat dateFormat;
Date date;
public static String[] days;
public static int[] months = {31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int today, beginOfMonth;
String month, year;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calender);
gvCal = (GridView) findViewById(R.id.gridView);
dateFormat = new SimpleDateFormat("yyyy");
date = new Date();
months[1] = Feb(Integer.parseInt(dateFormat.format(date))); // Find the amount of days in Feb
dateFormat = new SimpleDateFormat("MM");
int numDays = months[Integer.parseInt(dateFormat.format(date))-1] + 6; // Number of days in the month as well as making sure not to override the day names
// Check which day of the month the month started on. Eg: April 1st 2016 is a Friday
dateFormat = new SimpleDateFormat("MM");
month = dateFormat.format(date);
dateFormat = new SimpleDateFormat("yyyy");
year = dateFormat.format(date);
try {
beginOfMonth = (Day("01"+month+year))-1; // Get the beginning of the month (-1 because Android recognizes Sunday as the first day)
} catch (ParseException pe) {
Toast.makeText(getApplicationContext(), pe.getMessage(), Toast.LENGTH_LONG).show();
}
if (beginOfMonth == 0) {
beginOfMonth = 7;
}
days = new String[numDays+beginOfMonth];
days[0] = "Mon";
days[1] = "Tue";
days[2] = "Wed";
days[3] = "Thu";
days[4] = "Fri";
days[5] = "Sat";
days[6] = "Sun";
dateFormat = new SimpleDateFormat("dd");
String temp = dateFormat.format(date);
today = Integer.parseInt(temp);
if(beginOfMonth != 0) {
for (int i = 7; i <= (5 + beginOfMonth); i++) {
days[i] = "";
}
}
for (int i = (6 + beginOfMonth); i <= (days.length-1); i++) {
days[i] = Integer.toString(i-beginOfMonth-5);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.cal_days, days);
gvCal.setAdapter(adapter);
gvCal.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
tvMonth = (TextView) findViewById(R.id.textView);
dateFormat = new SimpleDateFormat("MMMM"); // Get month name
tvMonth.setText(dateFormat.format(date));
}
public int Feb(int year) {
int temp;
try {
temp = year/4;
} catch (Exception e) {
return 28;
}
return 29;
}
public int Day(String day) throws ParseException {
DateFormat df = new SimpleDateFormat("ddMMyyyy");
try {
Date d = df.parse(String.valueOf(day));
Calendar c = Calendar.getInstance();
c.setTime(d);
return c.get(Calendar.DAY_OF_WEEK);
} catch (Exception e) {
ParseException pe = new ParseException("There was a problem getting the date.", 0);
throw pe;
}
}
Überprüfen Sie diese Repo jetzt, danke Alter. – Jono
Hey, ich habe Probleme, diese Bibliothek zu benutzen, könntest du mir bitte eine kleine Hilfe geben? – Jono
Was ist das Problem genau? –