使用CalendarView和ListView在日历中显示事件

时间:2014-04-20 01:46:28

标签: android android-layout listview calendarview

这一直困扰着我。我正在制作一个基本上可以作为学生议程的应用程序。它由一个包含任务的数据库提供支持。

因此,我的应用程序中的一个活动顶部有一个CalendarView,底部有一个ListView。理想情况下,当您点击日历上的某一天时,与该日相关的分配将显示在ListView中。但是,当你改变日子时它只会崩溃,我无法弄清楚原因。我测试了我的getDayAssignments()数据库方法,它按预期执行。

那么我该如何让它发挥作用呢?

这是我的代码:

package com.example.studentagenda;

import java.util.ArrayList;
import java.util.Calendar;

import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.view.Menu;
import android.widget.CalendarView;
import android.widget.CalendarView.OnDateChangeListener;
import android.widget.ListView;

public class SchoolCalendar extends Activity {
protected CalendarView calendar;

protected ArrayList<Assignment> dayAssign;
protected ListView assignments;
protected CalendarAdapter calAdapt;
protected DatabaseConnector dBase;
protected Calendar c;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_calendar);
    calendar = (CalendarView)findViewById(R.id.calendarView1);
    assignments = (ListView) findViewById(R.id.AssignListView);
    dayAssign = new ArrayList<Assignment>();

    this.calAdapt = new CalendarAdapter(this, dayAssign);
    assignments.setAdapter(calAdapt);
    initListener();

}
public void initListener()
{
    calendar.setOnDateChangeListener(new OnDateChangeListener(){

        @Override
        public void onSelectedDayChange(CalendarView view,
        int year, int month, int dayOfMonth) {
            getDay(dayOfMonth, month+1, year);
            ;}});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.calendar, menu);
    return true;
}

public void getDay(int day, int month, int year)
{
    int hour, key;
    String desc;

    dBase.open();
    Cursor cur = dBase.getDayAssignments(day, month, year);

    cur.moveToFirst();

    while(cur.isAfterLast() == false)
    {
        day = cur.getInt(cur.getColumnIndex("day"));
        month = cur.getInt(cur.getColumnIndex("month"));
        desc = cur.getString(cur.getColumnIndex("desc"));
        hour = cur.getInt(cur.getColumnIndex("hours"));
        key = cur.getInt(cur.getColumnIndex("_id"));
        Assignment newAssig = new Assignment(day, month, desc, hour, key);
        dayAssign.add(newAssig);

        cur.moveToNext();
    }
    cur.close();
    dBase.close();
}

}

这是我的XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/lined_paper"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Calendar" >

<CalendarView
    android:id="@+id/calendarView1"
    android:layout_width="match_parent"
    android:layout_height="250dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" />

<ListView
    android:id="@+id/AssignListView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/calendarView1"
    android:layout_below="@+id/calendarView1" >
</ListView>

</RelativeLayout>

这是我的数据库电话,如果它有用的话。

 public Cursor getDayAssignments(int day, int month, int year)
   {
       String whereClause = "day = " + day + " AND month = " + month +" AND        year = " + year;

       return database.query("assignCalendar", new String[]{"_id", "day",         "month", "desc", "hours"}, whereClause, null, null, null, "hours");
   }

0 个答案:

没有答案