我只想根据当天显示数据

时间:2017-07-04 14:26:27

标签: java android android-fragments

我正在为我的部门开发一个时间表应用程序,我希望根据当天(即周一,周二,周三等)显示当天的时间表

package com.example.aravinth1.timetable;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.Date;

public class Today extends Fragment {
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    getActivity().setTitle("Today");
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    //code for getting day
    Date currentDate = new Date(System.currentTimeMillis());
    String date = currentDate.toString();
    String arr[] = date.split(" ");
    String dayOfWeek = arr[0];
    //switch to select the layout
    //according to the day
    switch (dayOfWeek){
        case "Mon":
            return inflater.inflate(R.layout.moday, container, false);
        break;
        case "Tue":
            return inflater.inflate(R.layout.tuesday, container, false);
        break;
        case "Wed":
            return inflater.inflate(R.layout.wednesday, container, false);
        break;
        case "Thu":
            return inflater.inflate(R.layout.thursday, container, false);
        break;
        case "Fri":
            return inflater.inflate(R.layout.friday, container, false);
        break;
        case "Sat":
            return inflater.inflate(R.layout.saturday, container, false);
        break;
        case "Sun":
            return inflater.inflate(R.layout.sunday, container, false);
        break;
    }
    //how to return the inflated view
}
}

在前面提到的代码中,我如何根据当天返回膨胀的视图?在这种情况下使用switch语句是否正确?

1 个答案:

答案 0 :(得分:0)

使用Calendar课程在Android中获取当天的当天。

Calendar calendar = Calendar.getInstance();
int today = calendar.get(Calendar.DAY_OF_WEEK); 

switch (today) {    
    case Calendar.MONDAY:
        return inflater.inflate(R.layout.moday, container, false);

    case Calendar.TUESDAY:
        return inflater.inflate(R.layout.tuesday, container, false);

    case Calendar.WEDNESDAY:
        //etc...
}