Search code examples
androidandroid-fragmentscaldroid

Caldroid does not refresh


I have put Caldroid inside my activity with this code in onCreate method.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    app_font = Typeface.createFromAsset(getAssets(), "custom_font.ttf");
    dbHelper = new DatabaseHelper(this);
    caldroidFragment = new CaldroidFragment();
    if (savedInstanceState != null) {
        caldroidFragment.restoreStatesFromKey(savedInstanceState,
                "CALDROID_SAVED_STATE");
    } else {
        Bundle args = new Bundle();
        args.putInt(CaldroidFragment.MONTH, calendar.get(Calendar.MONTH) + 1);
        args.putInt(CaldroidFragment.YEAR, calendar.get(Calendar.YEAR));
        args.putBoolean(CaldroidFragment.ENABLE_SWIPE, true);
        args.putBoolean(CaldroidFragment.SIX_WEEKS_IN_CALENDAR, true);
        args.putInt(CaldroidFragment.START_DAY_OF_WEEK, CaldroidFragment.MONDAY);
        caldroidFragment.setArguments(args);
    }
    FragmentTransaction t = getSupportFragmentManager().beginTransaction();
    t.replace(R.id.calendar_holder, caldroidFragment, FRAGMENT_TAG);
    t.commit();
    fragmentMounted = true;
    CaldroidListener listener = new CaldroidListener() {

        @Override
        public void onSelectDate(Date date, View view) {
            Intent k = new Intent(MainActivity.this, DateActivity.class);
            k.putExtra(TIME, date.getTime());
            MainActivity.this.startActivity(k);
        }

        @Override
        public void onChangeMonth(int month, int year) {
            if (marker != null) {
                marker.stopWorking();
            }
            marker = new DateMarker(MainActivity.this, month, year);
            marker.start();
        }

        @Override
        public void onLongClickDate(Date date, View view) {
            // do nothing
        }

        @Override
        public void onCaldroidViewCreated() {
            // do nothing
        }

    };
    caldroidFragment.setCaldroidListener(listener);
}

As you notice, there is a Thread being called inside listener every time user switches to other month. This thread accesses database and marks dates which have events.

NOTE: this is just inner sample of code inside run method of that thread.

// more code above
if (MainActivity.dbHelper.getNotesOnDate(date).size() == 0) {
    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            activity.caldroidFragment.setBackgroundResourceForDate(R.color.SeaGreen, date);
            activity.caldroidFragment.setTextColorForDate(R.color.white, date);
            activity.caldroidFragment.refreshView();
        }
    });
}
//more code under

I start another activity for user to log in while all this loads. The problem is only markings that are made BEFORE replacing fragment with holder view in onCreate method. How can I refresh caldroidFragment after replacing it with its holder? refreshView() seems not to be working after placing it.

EDIT: I did research and I notice Caldroid is using app V4 Fragment. I tried to detach and reattach it to get refresh effect but I only got NullPointerException while trying to do it.


Solution

  • I found the answer, do not use Java's Date class, it does not work right.

    Use Joda's DateTime. Caldroid works fine with it. Also, I found minor bug in my database access, but it should have worked even though it was there.