Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android課程表的設計開發

Android課程表的設計開發

編輯:關於Android編程

導語

實現了教務系統中課程的導入,分類顯示課程。學期的修改,增加,修改。課程按照周的顯示。課程修改上課星期和上課周。上課課程的自動歸類。

一、主要功能界面

DrawingDrawing DrawingDrawing Drawing

開發過程

一開始因為畢設有關課程表的要求不明,主要就是利用jsoup拉取學校教務管理系統的課程數據進行課程表界面的填充顯示,並不能課程的個性化調整。
後來重新調整了需求,參考了超級課程表的功能。重新設計了實體類,利用bmob移動端雲作為爬取到的數據的數據服務器進行了重新的開發。

主要代碼

1、課程實體類
package com.mangues.coursemanagement.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.bmob.v3.BmobObject;

public class CourseBean extends BmobObject implements Serializable {
    public static final String TAG = "CourseBean";

    private String studentId;
    private String dataYear;
    private String dataTerm;

    private String courseName = "";  //課程名
    private String courseRoom = "";   //教室
    private String courseTeacher = "";  //老師
    //private String courseWeekNumber = "0";
    private ArrayList courseWeekNumber = new ArrayList<>(); //周數
    private int courseWeek = 0;   //星期幾上課
    private int courseLow = 0;   //第幾排
    private int courseSection = 0;    //延續幾節課
    //private String courseSection = "12";   //第幾節上課1,2,12(2節課)
    //private String courseIn = "3";    //單雙周    1(單),2(雙),3(全)
    public CourseBean() {
        super();
    }

    public void setCourseBase(String studentId, String dataYear, String dataTerm) {
        this.studentId = studentId;
        this.dataYear = dataYear;
        this.dataTerm = dataTerm;
    }

    public CourseBean(String courseName, String courseRoom, String courseTeacher, ArrayList courseWeekNumber, int courseWeek, int courseLow, int courseSection) {
        this.courseName = courseName;
        this.courseRoom = courseRoom;
        this.courseTeacher = courseTeacher;
        this.courseWeekNumber = courseWeekNumber;
        this.courseWeek = courseWeek;
        this.courseLow = courseLow;
        this.courseSection = courseSection;
    }

    /**
     * str 數據到bean
       * @Name: stringToBean
       * @param str
       * @return
       * @Time: 2015-12-21 上午11:00:57
       * @Return: CourseBean
     */
    public static CourseBean stringToBean(String str) {
        return toBean(str);
    }

    //輔助
    private static CourseBean toBean(String courseDatas){
        CourseBean bean = null;
        String[] courseData = courseDatas.split("◇");
        if(courseData.length>3){   //有數據
            bean = new CourseBean();
            String courseName = courseData[0];
            String courseRoom = courseData[2];
            //獲取上課周數
            findWeekNumberFromStr(courseData[1],bean);
            bean.setCourseName(courseName);
            bean.setCourseRoom(courseRoom);
            findCourseInFromStr(courseData[4],bean);
        }
        return bean;
    }

    /**
     *   找出上課周數,老師名字
       * @Name: findFromStr
       * @return
       * @Time: 2015-12-21 上午11:22:30
       * @Return: String
     */
    public static void findWeekNumberFromStr(String courseData,CourseBean bean){
        Pattern pattern = Pattern.compile("(\\w*)\\((\\d+)-(\\d+)\\)");
        Matcher matcher = pattern.matcher(courseData);
        if(matcher.find()){
          String teacher =  matcher.group(1);
          bean.setCourseTeacher(teacher);
          String weekNumberstart =  matcher.group(2);
          String weekNumberfinish =  matcher.group(3);
            Integer weekNumberstartInt = Integer.parseInt(weekNumberstart);
            Integer weekNumberfinishInt = Integer.parseInt(weekNumberfinish);
            for (int i = weekNumberstartInt;i<=weekNumberfinishInt;i++){
                bean.getCourseWeekNumber().add(i);
            }
        }
    }


    /**
     *   找出 上課是不是單雙周,幾節課
       * @Name: findCourseInFromStr
       * @param courseData
       * @return
       * @Time: 2015-12-21 下午1:29:05
       * @Return: String
     */
    public static void findCourseInFromStr(String courseData,CourseBean bean){
        Pattern pattern = Pattern.compile("(\\w*)\\{(\\d*)節\\}");
        Matcher matcher = pattern.matcher(courseData);
        if(matcher.find()){
          String str =  matcher.group(1);
            ArrayList list = bean.getCourseWeekNumber();
          switch (str) {
               case "單周":
                   for (int i = 0;i< list.size();i++){
                       Integer weekNumber = list.get(i);
                       if (weekNumber%2==0){  //移除偶數
                           bean.getCourseWeekNumber().remove(i);
                       }
                   }
                   break;
               case "雙周":
                   for (int i = 0;i< list.size();i++){
                       Integer weekNumber = list.get(i);
                       if (weekNumber%2!=0){  //移除奇數
                           bean.getCourseWeekNumber().remove(i);
                       }
                   }
                    break;
               default:
                   break;
         }
            String str2 =  matcher.group(2);
            String[] args = str2.split("");
            if (args.length==3){
                Integer integer = Integer.parseInt(args[1]);
                Integer integer2 = Integer.parseInt(args[2]);
                bean.setCourseLow(integer);
                bean.setCourseSection(integer2-integer+1);


            }else if (args.length==4){
                Integer integer = Integer.parseInt(args[1]);
                Integer integer2 = Integer.parseInt(args[2]+args[3]);
                bean.setCourseLow(integer);
                bean.setCourseSection(integer2-integer+1);

            }else if (args.length==5){
                Integer integer = Integer.parseInt(args[1]+args[2]);
                Integer integer2 = Integer.parseInt(args[3]+args[4]);
                bean.setCourseLow(integer);
                bean.setCourseSection(integer2-integer+1);

            }else if (args.length==2){
                Integer integer = Integer.parseInt(args[1]);
                bean.setCourseLow(integer);
                bean.setCourseSection(1);
            }




        }
    }




    public String getCourseName() {
        return courseName;
    }

    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }

    public String getCourseRoom() {
        return courseRoom;
    }

    public void setCourseRoom(String courseRoom) {
        this.courseRoom = courseRoom;
    }

    public String getCourseTeacher() {
        return courseTeacher;
    }

    public void setCourseTeacher(String courseTeacher) {
        this.courseTeacher = courseTeacher;
    }

    public ArrayList getCourseWeekNumber() {
        return courseWeekNumber;
    }

    public void setCourseWeekNumber(ArrayList courseWeekNumber) {
        this.courseWeekNumber = courseWeekNumber;
    }

    public int getCourseWeek() {
        return courseWeek;
    }

    public void setCourseWeek(int courseWeek) {
        this.courseWeek = courseWeek;
    }

    public int getCourseLow() {
        return courseLow;
    }

    public void setCourseLow(int courseLow) {
        this.courseLow = courseLow;
    }

    public int getCourseSection() {
        return courseSection;
    }

    public void setCourseSection(int courseSection) {
        this.courseSection = courseSection;
    }

    public String getStudentId() {
        return studentId;
    }

    public void setStudentId(String studentId) {
        this.studentId = studentId;
    }

    public String getDataYear() {
        return dataYear;
    }

    public void setDataYear(String dataYear) {
        this.dataYear = dataYear;
    }

    public String getDataTerm() {
        return dataTerm;
    }

    public void setDataTerm(String dataTerm) {
        this.dataTerm = dataTerm;
    }
}
2、課程歸類處理

    //按天查詢數據
    private void getDayDate(List list){
        boolean boo = SaveUtil.getSharedPreferencesSwitch(mContext);
        ArrayList integerA = new ArrayList<>();
        if (boo){
            if (weekNum!=-1){
                for (int l=0;l integerArrayList = list.get(l).getCourseWeekNumber();
                    if (!integerArrayList.contains(weekNum)){  //不包含,就是不是本周,去除
                        integerA.add(list.get(l));
                    }
                }
                    list.removeAll(integerA);


            }
        }





        List list1  = null;
        Map> map = new HashMap<>();

        for (CourseBean be :list) {


            Integer weekNum = be.getCourseWeek();
            if (map.containsKey(weekNum)){  //有數據
                list1 = map.get(weekNum);
            }else {
                list1 = new ArrayList<>();
                map.put(weekNum,list1);
            }
            list1.add(be);
        }

        ArrayList ls = new ArrayList<>();

        //按星期幾處理
        for (Map.Entry> entry : map.entrySet()) {
            List  mapw = handleRepeat(entry.getValue(),entry.getKey());
                ls.addAll(mapw);


        }

        //本地存儲存儲使用
        TimeTableBmob bmob = new TimeTableBmob();
        bmob.setStudentId(CourseApplication.getInstance().getUserInfo().getStudentId());
        bmob.setCourseList(ls);
        bmob.setTerm(dataTerm);
        bmob.setYear(dataYear);
        CourseApplication.getInstance().setTimeTableBmob(bmob);

        dataBack.onDataBack(ls);
    }




    //處理重復
    private List  handleRepeat(List list,Integer weekNum){

        Collections.sort(list,new Comparator(){
            public int compare(CourseBean arg0, CourseBean arg1) {
                Integer year1 = arg0.getCourseLow();
                Integer year2 = arg1.getCourseLow();
                return year1.compareTo(year2);
            }
        });



        List listKey = new ArrayList<>();
        List liststr = new ArrayList<>();

        int size = list.size();

            for (int h=0;h=low){   //包含在內
                        isAdd = true;
                        CourseBeanMap cousermap = listKey.get(kk);
                        cousermap.setCourseLow(low1);
                        cousermap.setCourseWeek(weekNum);
                        cousermap.setCourseSection(sesson+low-low1);
                        liststr.set(kk,low1+"-"+(sesson+low-low1));//修改key值
                        cousermap.add(bean);
                    }
                }


                if (isAdd==false){
                    CourseBeanMap cousermap = new CourseBeanMap();
                    cousermap.setCourseLow(low);
                    cousermap.setCourseWeek(weekNum);
                    cousermap.setCourseSection(sesson);
                    cousermap.add(bean);
                    listKey.add(cousermap);
                    liststr.add(low+"-"+sesson);
                }
            }


        return listKey;
    }
3.課程表界面

利用了網上找到的一段代碼進行部分修改完成

    //初始化課程表界面
  private void initTable() {
        // 獲得列頭的控件
        empty = (TextView) this.findViewById(R.id.test_empty);
        monColum = (TextView) this.findViewById(R.id.test_monday_course);
        tueColum = (TextView) this.findViewById(R.id.test_tuesday_course);
        wedColum = (TextView) this.findViewById(R.id.test_wednesday_course);
        thrusColum = (TextView) this.findViewById(R.id.test_thursday_course);
        friColum = (TextView) this.findViewById(R.id.test_friday_course);
        satColum = (TextView) this.findViewById(R.id.test_saturday_course);
        sunColum = (TextView) this.findViewById(R.id.test_sunday_course);
        course_table_layout = (RelativeLayout) this
                .findViewById(R.id.test_course_rl);
        course_table_layout.removeAllViews();  //清楚頁面數據
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        // 屏幕寬度
        int width = dm.widthPixels;
        // 平均寬度
        int aveWidth = width / 8;
        // 第一個空白格子設置為25寬
        empty.setWidth(aveWidth * 3 / 4);
        monColum.setWidth(aveWidth * 33 / 32 + 1);
        tueColum.setWidth(aveWidth * 33 / 32 + 1);
        wedColum.setWidth(aveWidth * 33 / 32 + 1);
        thrusColum.setWidth(aveWidth * 33 / 32 + 1);
        friColum.setWidth(aveWidth * 33 / 32 + 1);
        satColum.setWidth(aveWidth * 33 / 32 + 1);
        sunColum.setWidth(aveWidth * 33 / 32 + 1);
        this.screenWidth = width;
        this.aveWidth = aveWidth;
        int height = dm.heightPixels;
        gridHeight = height / 12;
        // 設置課表界面
        // 動態生成12 * maxCourseNum個textview
        for (int i = 1; i <= 12; i++) {

            for (int j = 1; j <= 8; j++) {

                TextView tx = new TextView(mContext);
                tx.setId((i - 1) * 8 + j);
                // 除了最後一列,都使用course_text_view_bg背景(最後一列沒有右邊框)
                if (j < 8)
                    tx.setBackgroundDrawable(mContext.getResources()
                            .getDrawable(R.drawable.course_text_view_bg));
                else
                    tx.setBackgroundDrawable(mContext.getResources()
                            .getDrawable(R.drawable.course_table_last_colum));
                // 相對布局參數
                RelativeLayout.LayoutParams rp = new RelativeLayout.LayoutParams(
                        aveWidth * 33 / 32 + 1, gridHeight);
                // 文字對齊方式
                tx.setGravity(Gravity.CENTER);
                // 字體樣式
                tx.setTextAppearance(mContext, R.style.courseTableText);
                // 如果是第一列,需要設置課的序號(1 到 12)
                if (j == 1) {
                    tx.setAlpha(0.3f);
                    tx.setText(String.valueOf(i));
                    rp.width = aveWidth * 3 / 4;
                    // 設置他們的相對位置
                    if (i == 1)
                        rp.addRule(RelativeLayout.BELOW, empty.getId());
                    else
                        rp.addRule(RelativeLayout.BELOW, (i - 1) * 8);
                } else {
                    tx.setAlpha(0f);
                    rp.addRule(RelativeLayout.RIGHT_OF, (i - 1) * 8 + j - 1);
                    rp.addRule(RelativeLayout.ALIGN_TOP, (i - 1) * 8 + j - 1);
                    tx.setText("");
                }

                tx.setLayoutParams(rp);
                course_table_layout.addView(tx);
            }
        }
    }

//數據填充
private void setCourseData(final CourseBeanMap map, final int index) {
        final CourseBean bean = map.returnfirstTrue(weekNum);

        // 添加課程信息
        TextView courseInfo = new TextView(mContext);
        courseInfo.setText(bean.getCourseName() + "@" + bean.getCourseRoom());
        // 該textview的高度根據其節數的跨度來設置
        RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(

                aveWidth * 31 / 32, (gridHeight - 5) * map.getCourseSection());
        // textview的位置由課程開始節數和上課的時間(day of week)確定
        rlp.topMargin = 5 + (map.getCourseLow() - 1) * gridHeight;
        rlp.leftMargin = 1;
        // 偏移由這節課是星期幾決定
        rlp.addRule(RelativeLayout.RIGHT_OF, map.getCourseWeek());
        // 字體劇中
        courseInfo.setGravity(Gravity.CENTER);
        // 設置一種背景
        final int back;
        final int huiback;
        if (map.isDoubleBean()) {// 有單雙周
            back = ColorDrawable.getCourseBgMulti(map.getCourseLow(),
                    map.getCourseWeek());
            huiback = ColorDrawable.grayColorMulti;
        } else {
            back = ColorDrawable.getCourseBg(map.getCourseLow(),
                    map.getCourseWeek());
            huiback = ColorDrawable.grayColor;
        }
        if (map.returntTrue(weekNum)){
            courseInfo.setBackgroundResource(back);
        }else {
            courseInfo.setBackgroundResource(huiback);
        }


        courseInfo.setTextSize(12);
        courseInfo.setLayoutParams(rlp);
        courseInfo.setTextColor(Color.WHITE);
        // 設置不透明度
        courseInfo.getBackground().setAlpha(222);
        final int upperCourseIndex = 0;
        // 設置監聽事件
        courseInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                if (map.isDoubleBean()) {// 大於雙數
                    // 如果有多個課程,則設置點擊彈出gallery 3d 對話框
                    // LayoutInflater layoutInflater =
                    // (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    View galleryView = layoutInflater.inflate(
                            R.layout.course_info_gallery_layout, null);
                    final Dialog coursePopupDialog = new AlertDialog.Builder(
                            mContext).create();
                    coursePopupDialog.setCanceledOnTouchOutside(true);
                    coursePopupDialog.setCancelable(true);
                    coursePopupDialog.show();
                    WindowManager.LayoutParams params = coursePopupDialog
                            .getWindow().getAttributes();
                    params.width = WindowManager.LayoutParams.FILL_PARENT;
                    coursePopupDialog.getWindow().setAttributes(params);

                    DisplayMetrics dm = new DisplayMetrics();
                    getWindowManager().getDefaultDisplay()
                            .getMetrics(dm);
                    // 屏幕寬度
                    int width = dm.widthPixels;

                    CourseInfoAdapter adapter = new CourseInfoAdapter(mContext,
                            map, width, back,weekNum);
                    CourseInfoGallery gallery = (CourseInfoGallery) galleryView
                            .findViewById(R.id.course_info_gallery);
                    gallery.setSpacing(10);
                    gallery.setAdapter(adapter);
                    gallery.setSelection(upperCourseIndex);
                    gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView arg0, View arg1,
                                                int arg2, long arg3) {
                            CourseBean bean = map.get(arg2);
                            Intent intent = new Intent(mContext,CourseInfoActivity.class);
                            Bundle mBundle = new Bundle();
                            mBundle.putSerializable(CourseBean.TAG, bean);
                            mBundle.putInt("index", index);
                            mBundle.putInt("courseBeanIndex", arg2);
                            intent.putExtras(mBundle);
                            startActivity(intent);
                            coursePopupDialog.cancel();
                        }
                    });
                    coursePopupDialog.setContentView(galleryView);
                }else { //沒有單雙周
                    Intent intent = new Intent(mContext,CourseInfoActivity.class);
                    Bundle mBundle = new Bundle();
                    mBundle.putInt("index", index);
                    mBundle.putInt("courseBeanIndex", 0);
                    mBundle.putSerializable(CourseBean.TAG, bean);
                    intent.putExtras(mBundle);
                    startActivity(intent);
                }
            }

        });
        course_table_layout.addView(courseInfo);
    }
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved