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

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

  ask by insomnius1 translate from so

本文未有回复,本站智能推荐: