0

我有一个自定义日历应用程序,它与我们的学校信息亭应用程序集成。日历必须允许添加事件并为每个事件设置通知。如何存储每个事件并在设定的时间和日期通知它?谁能帮我?这是日历代码。

public class MyCalendar extends Activity implements OnClickListener {

public Calendar month;
public CalendarAdapter adapter;
public Handler handler;
public ArrayList<String> items;

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.calendar);

        month = Calendar.getInstance();
    items = new ArrayList<String>();
    adapter = new CalendarAdapter(this, month);

    GridView gridView = (GridView) findViewById(R.id.gridview);
    gridView.setAdapter(adapter);

    handler = new Handler();
    handler.post(calendarUpdater);

    TextView title = (TextView) findViewById(R.id.title);
    title.setText(android.text.format.DateFormat.format("MMMM yyyy", month));

    Button previous = (Button) findViewById(R.id.previous);
    previous.setOnClickListener(this);

    Button next = (Button) findViewById(R.id.next);
    next.setOnClickListener(this);

    gridView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {
            TextView date = (TextView) v.findViewById(R.id.date);
            if (date instanceof TextView && !date.getText().equals("")) {

                Intent intent = new Intent(
                        "com.gpplsmje.mac.calendar.CALENDAREVENTS");

                intent.putExtra(
                        "date",
                        android.text.format.DateFormat
                                .format("MMMM", month)
                                + " "
                                + day
                                + ", "
                                + android.text.format.DateFormat.format(
                                        "yyyy", month));
                startActivity(intent);
            }
        }
    });

public void refreshCalendar() {
    TextView title = (TextView) findViewById(R.id.title);
    daysAdapter.fill();
    adapter.refreshDays();
    adapter.notifyDataSetChanged();
    handler.post(calendarUpdater);
    title.setText(android.text.format.DateFormat.format("MMMM yyyy", month));
}

@Override
public void onNewIntent(Intent intent) {
    String date = intent.getStringExtra("date");
    String[] dateArr = date.split("-");
    month.set(Integer.parseInt(dateArr[0]),
            Integer.parseInt(dateArr[1], Integer.parseInt(dateArr[2])));
}

public Runnable calendarUpdater = new Runnable() {

    @Override
    public void run() {
        items.clear();
        for (int i = 0; i < 31; i++) {
            Random r = new Random();
            if (r.nextInt(10) > 6) {
                items.add(Integer.toString(i));
            }
        }
        adapter.setItems(items);
        adapter.notifyDataSetChanged();
    }

};

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.previous:
        if (month.get(Calendar.MONTH) == month
                .getActualMinimum(Calendar.MONTH)) {
            month.set((month.get(Calendar.YEAR) - 1),
                    month.getActualMaximum(Calendar.MONTH), 1);
        } else {
            month.set(Calendar.MONTH, month.get(Calendar.MONTH) - 1);
        }
        refreshCalendar();
        break;
    case R.id.next:
        if (month.get(Calendar.MONTH) == month
                .getActualMaximum(Calendar.MONTH)) {
            month.set((month.get(Calendar.YEAR) + 1),
                    month.getActualMinimum(Calendar.MONTH), 1);
        } else {
            month.set(Calendar.MONTH, month.get(Calendar.MONTH) + 1);
        }
        refreshCalendar();
        break;
    }
}
}

这是 CalendarAdapter 类:

public class CalendarAdapter extends BaseAdapter {

static final int FIRST_DAY_OF_WEEK = 0;

private Context context;

private java.util.Calendar month;
private Calendar selectedDate;
private ArrayList<String> items;

public String[] days;

public CalendarAdapter(Context c, Calendar monthCalendar) {
    month = monthCalendar;
    selectedDate = (Calendar) monthCalendar.clone();
    context = c;
    month.set(Calendar.DAY_OF_MONTH, 1);
    this.items = new ArrayList<String>();
    refreshDays();
}

public void setItems(ArrayList<String> items) {
    for (int i = 0; i != items.size(); i++) {
        if (items.get(i).length() == 1) {
            items.set(i, "0" + items.get(i));
        }
    }
    this.items = items;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return days.length;
}

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    TextView dayView;
    if (convertView == null) { // if it's not recycled, initialize some
                                // attributes
        LayoutInflater vi = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.calendar_item, null);
    }
    dayView = (TextView) v.findViewById(R.id.date);

    // disable empty days from the beginning

    if (days[position].equals("")) {
        dayView.setClickable(false);
        dayView.setFocusable(false);
    } else {
        // mark current day as focused
        if (month.get(Calendar.YEAR) == selectedDate.get(Calendar.YEAR)
                && month.get(Calendar.MONTH) == selectedDate
                        .get(Calendar.MONTH)
                && days[position].equals(""
                        + selectedDate.get(Calendar.DAY_OF_MONTH))) {
            v.setBackgroundResource(R.drawable.item_background_focused);
        } else {
            v.setBackgroundResource(R.drawable.calendar_item_bg);
        }
    }
    dayView.setText(days[position]);

    // create date string for comparison
    String date = days[position];

    if (date.length() == 1) {
        date = "0" + date;
    }
    String monthStr = "" + (month.get(Calendar.MONTH) + 1);
    if (monthStr.length() == 1) {
        monthStr = "0" + monthStr;
    }

    // show icon if date is not empty and it exists in the items array
    ImageView iw = (ImageView) v.findViewById(R.id.date_icon);
    if (date.length() > 0 && items != null && items.contains(date)) {
        iw.setVisibility(View.VISIBLE);
    } else {
        iw.setVisibility(View.INVISIBLE);
    }
    return v;
}

public void refreshDays() {
    // clear items
    items.clear();

    int lastDay = month.getActualMaximum(Calendar.DAY_OF_MONTH);
    int firstDay = month.get(Calendar.DAY_OF_WEEK);

    int remain = 0;
    int minus = firstDay - 1;
    if (lastDay == 31) {
        remain = 11 - minus;
        if (firstDay == 1) {
            days = new String[lastDay + (FIRST_DAY_OF_WEEK * 6) + remain];
        } else {
            days = new String[lastDay + firstDay - (FIRST_DAY_OF_WEEK + 1) + remain];
        }
    } else if (lastDay == 30) {
        remain = 12 - minus;
        if (firstDay == 1) {
            days = new String[lastDay + (FIRST_DAY_OF_WEEK * 6) + remain];
        } else {
            days = new String[lastDay + firstDay - (FIRST_DAY_OF_WEEK + 1) + remain];
        }
    } else if (lastDay == 29) {
        remain = 13 - minus;
        if (firstDay == 1) {
            days = new String[lastDay + (FIRST_DAY_OF_WEEK * 6) + remain];
        } else {
            days = new String[lastDay + firstDay - (FIRST_DAY_OF_WEEK + 1) + remain];
        }
    } else if (lastDay == 28) {
        remain = 14 - minus;
        if (firstDay == 1) {
            days = new String[lastDay + (FIRST_DAY_OF_WEEK * 6) + remain];
        } else {
            days = new String[lastDay + firstDay - (FIRST_DAY_OF_WEEK + 1) + remain];
        }
    }
    // figure size of the array
    int dummy;
    if (firstDay == 1) {
        dummy = lastDay + (FIRST_DAY_OF_WEEK * 6);
    } else {
        dummy = lastDay + firstDay - (FIRST_DAY_OF_WEEK + 1);
    }

    int j = FIRST_DAY_OF_WEEK;

    // populate empty days before first real day
    if (firstDay > 1) {
        for (j = 0; j < firstDay - FIRST_DAY_OF_WEEK; j++) {
            days[j] = "";
        }
    } else {
        for (j = 0; j < FIRST_DAY_OF_WEEK * 6; j++) {
            days[j] = "";
        }
        j = FIRST_DAY_OF_WEEK * 6 + 1; // sunday => 1, monday => 7
    }

    // populate days
    int dayNumber = 1;
    int i;
    for (i = (j - 1); i < dummy; i++) {
        days[i] = "" + dayNumber;
        dayNumber++;
    }
    for(int k = i; k < days.length; k++){
        days[k] = "";
    }
}

}
4

0 回答 0