如何用日历填写JSON出勤数据?

时间:2018-07-09 05:15:34

标签: android json calendar

在Android中创建出勤活动。我必须在日历中以不同的颜色显示“现在”,“离开”,“缺席”显示。如何在日历中显示?

1 个答案:

答案 0 :(得分:1)

我希望这对您有用。

使用Caldroid库。

从服务器获取数据。制作不同日期的数组列表,即(现在,缺席,离开)。

并根据当前日期(例如绿色),缺席(例如红色)和休假(例如蓝色)为日期分配颜色

在build.gradle文件中添加依赖项

 implementation 'com.roomorama:caldroid:3.0.1'

在包中添加以下活动。

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.roomorama.caldroid.CaldroidFragment;
import com.roomorama.caldroid.CaldroidListener;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class CaldroidSampleActivity extends AppCompatActivity {
    private CaldroidFragment caldroidFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main1);
        final SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
        caldroidFragment = new CaldroidFragment();

        ArrayList<String> stringArrayListPresent = new ArrayList<>();
        stringArrayListPresent.add("07/09/2018");
        stringArrayListPresent.add("07/10/2018");
        stringArrayListPresent.add("07/11/2018");

        ArrayList<String> stringArrayListAbsent = new ArrayList<>();
        stringArrayListAbsent.add("07/12/2018");
        stringArrayListAbsent.add("07/13/2018");
        stringArrayListAbsent.add("07/14/2018");

        ArrayList<String> stringArrayListLeave = new ArrayList<>();
        stringArrayListLeave.add("07/17/2018");
        stringArrayListLeave.add("07/19/2018");

        //For present
        ColorDrawable green = new ColorDrawable(Color.GREEN);
        for (int i = 0; i < stringArrayListPresent.size(); i++) {

            try {
                Date parsedDate = formatter.parse(stringArrayListPresent.get(i));
                Log.e("date   ============", parsedDate + "");
                Log.e("date   ============", formatter.format(parsedDate));
                caldroidFragment.setBackgroundDrawableForDate(green, parsedDate);
                caldroidFragment.refreshView();
            } catch (ParseException e) {
                e.printStackTrace();
            }

        }
        //For Absent
        ColorDrawable red = new ColorDrawable(Color.RED);
        for (int i = 0; i < stringArrayListAbsent.size(); i++) {

            try {
                Date parsedDate = formatter.parse(stringArrayListAbsent.get(i));
                Log.e("date   ============", parsedDate + "");
                Log.e("date   ============", formatter.format(parsedDate));
                caldroidFragment.setBackgroundDrawableForDate(red, parsedDate);
                caldroidFragment.refreshView();
            } catch (ParseException e) {
                e.printStackTrace();
            }

        }

        //For Leave
        ColorDrawable blue = new ColorDrawable(Color.BLUE);
        for (int i = 0; i < stringArrayListLeave.size(); i++) {

            try {
                Date parsedDate = formatter.parse(stringArrayListLeave.get(i));
                Log.e("date   ============", parsedDate + "");
                Log.e("date   ============", formatter.format(parsedDate));
                caldroidFragment.setBackgroundDrawableForDate(blue, parsedDate);
                caldroidFragment.refreshView();
            } catch (ParseException e) {
                e.printStackTrace();
            }

        }
        if (savedInstanceState != null) {
            caldroidFragment.restoreStatesFromKey(savedInstanceState,
                    "CALDROID_SAVED_STATE");
        }
        else {
            Bundle args = new Bundle();
            Calendar cal = Calendar.getInstance();
            args.putInt(CaldroidFragment.MONTH, cal.get(Calendar.MONTH) + 1);
            args.putInt(CaldroidFragment.YEAR, cal.get(Calendar.YEAR));
            args.putBoolean(CaldroidFragment.ENABLE_SWIPE, true);
            args.putBoolean(CaldroidFragment.SIX_WEEKS_IN_CALENDAR, true);

            caldroidFragment.setArguments(args);
        }

        FragmentTransaction t = getSupportFragmentManager().beginTransaction();
        t.replace(R.id.calendar1, caldroidFragment);
        t.commit();

        // Setup listener
        final CaldroidListener listener = new CaldroidListener() {

            @Override
            public void onSelectDate(Date date, View view) {
                Toast.makeText(getApplicationContext(), formatter.format(date),
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onChangeMonth(int month, int year) {
                String text = "month: " + month + " year: " + year;
                Toast.makeText(getApplicationContext(), text,
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onLongClickDate(Date date, View view) {
                Toast.makeText(getApplicationContext(),
                        "Long click " + formatter.format(date),
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCaldroidViewCreated() {
                if (caldroidFragment.getLeftArrowButton() != null) {
                    Toast.makeText(getApplicationContext(),
                            "Caldroid view is created", Toast.LENGTH_SHORT)
                            .show();
                }
            }

        };
        caldroidFragment.setCaldroidListener(listener);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        if (caldroidFragment != null) {
            caldroidFragment.saveStatesToKey(outState, "CALDROID_SAVED_STATE");
        }
    }
}

activity_main1.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/calendar1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"></LinearLayout>

    </LinearLayout>
</ScrollView>

添加了硬编码日期而不是该日期,从json解析日期添加到特定列表中。我希望这对您有用。