0

这一直困扰着我。我正在制作一个基本上用作学生议程的应用程序。它由保存分配的数据库提供支持。

因此,我的应用程序中的一项活动在顶部有一个 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");
   }
4

0 回答 0