第七週週三

a_true發表於2024-04-10

學習了adapter的使用

package com.example.studyrecords;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.StyleSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import com.example.studyrecords.DAO.Teacher_function;
import com.example.studyrecords.bean.DailyRecord;
import com.example.studyrecords.bean.Gen_table;
import com.example.studyrecords.mysql.DBManager;

import java.util.List;

public class MainActivity_tea_menu extends AppCompatActivity implements View.OnClickListener {

    private Button btn_collect_daily; //所有人每日,彙總統計
    private Button btn_search_daily; //查詢每日
    private Button btn_gen_table; //生成總結報表
    private TableLayout tableLayout ;

    private ListView listView;
    private List<DailyRecord> dailyRecordsList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_tea_menu);

        initView();

    }

    private void initView() {

        btn_collect_daily = findViewById(R.id.btn_collect_daily);
        btn_search_daily = findViewById(R.id.btn_search_daily);
        btn_gen_table = findViewById(R.id.btn_gen_table);
        //tableLayout = findViewById(R.id.tableLayout);

        btn_collect_daily.setOnClickListener(this);
        btn_search_daily.setOnClickListener(this);
        btn_gen_table.setOnClickListener(this);
    }

    public void onClick(View view) {
        if(view.getId() == R.id.btn_collect_daily) {
            listView = findViewById(R.id.listView);
            // 獲取資料來源 dailyRecordsList
            dailyRecordsList = Teacher_function.CollectDaily();
            DailyRecordAdapter adapter = new DailyRecordAdapter(this, dailyRecordsList);
            listView.setAdapter(adapter);
        } else if (view.getId() == R.id.btn_search_daily) {
            clearTable();

        } else if (view.getId() == R.id.btn_gen_table) {
            clearTable();

        }
    }

    private class DailyRecordAdapter extends BaseAdapter{
        private LayoutInflater inflater;
        private List<DailyRecord> dailyRecordsList;

        public DailyRecordAdapter(Activity activity, List<DailyRecord> dailyRecordsList) {
            this.dailyRecordsList = dailyRecordsList;
            inflater = LayoutInflater.from(activity);
        }

        @Override
        public int getCount() {
            return dailyRecordsList.size();
        }

        @Override
        public Object getItem(int position) {
            return dailyRecordsList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;

            if (convertView == null) {
                convertView = inflater.inflate(R.layout.item_daily_record, parent, false);
                holder = new ViewHolder();
                holder.stuidTextView = convertView.findViewById(R.id.stuidTextView);
                holder.stunameTextView = convertView.findViewById(R.id.stunameTextView);
                holder.dateTextView = convertView.findViewById(R.id.dateTextView);
                holder.recordTextView = convertView.findViewById(R.id.recordTextView);
                holder.levelTextView = convertView.findViewById(R.id.levelTextView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            DailyRecord record = dailyRecordsList.get(position);
            holder.stuidTextView.setText(record.getStu_id());
            holder.stunameTextView.setText(record.getStu_name());
            double resultInHours = Double.valueOf(record.getResult()) / 60;
            String formattedResult = String.format("%.2f", resultInHours);
            holder.dateTextView.setText(formattedResult);
            holder.recordTextView.setText(record.getDailyRecords());
            holder.levelTextView.setText(record.getFinish_level());

            // 為特定的 TextView 設定點選事件監聽器
            holder.stuidTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 在這裡處理點選事件,position 參數列示被點選的列表項的位置
                    Toast.makeText(MainActivity_tea_menu.this, "學號被點選了:" + dailyRecordsList.get(position).getStu_id(), Toast.LENGTH_SHORT).show();
                }
            });

            return convertView;
        }


        private class ViewHolder {
            TextView stuidTextView;
            TextView stunameTextView;
            TextView dateTextView;
            TextView recordTextView;
            TextView levelTextView;
        }
    }

    // 顯示輸入框對話方塊
    private void showInputDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("輸入搜尋內容");

        // 設定輸入框
        final EditText input = new EditText(this);
        builder.setView(input);

        // 設定搜尋按鈕
        builder.setPositiveButton("搜尋", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String searchText = input.getText().toString();
                // 處理搜尋邏輯
                searchAndDisplay(searchText);
                dialog.dismiss();
            }
        });

        // 顯示對話方塊
        builder.show();
    }

    // 根據搜尋內容更新表格資料
    private void searchAndDisplay(String searchText) {
        // 在這裡根據搜尋內容 searchText 更新表格資料
        // 更新完資料後重新整理表格顯示
        SearchDaily(searchText);
    }
    private void clearTable() {
        tableLayout.removeAllViews(); // 移除表格中的所有子檢視
    }

    private void collect_daily(){
        List<DailyRecord> dailyRecordsList = Teacher_function.CollectDaily();



    }

    private void gen_table() {

        List<Gen_table> genTablesList = Teacher_function.GenTable();

        if (genTablesList.isEmpty()){
            Log.d("報表","空");
        }else Log.d("報表","非空");

// 建立表頭行
        TableRow headerRow = new TableRow(this);
        TableRow.LayoutParams headerLayoutParams = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
        headerRow.setLayoutParams(headerLayoutParams);

        TextView idHeaderTextView = new TextView(this);
        idHeaderTextView.setText("序號");
        idHeaderTextView.setPadding(10, 10, 10, 10);
        headerRow.addView(idHeaderTextView);

        TextView stu_classHeaderTextView = new TextView(this);
        stu_classHeaderTextView.setText("班級");
        stu_classHeaderTextView.setPadding(10, 10, 10, 10);
        headerRow.addView(stu_classHeaderTextView);

// 建立日期列標題
        TextView stu_idHeaderTextView = new TextView(this);
        stu_idHeaderTextView.setText("學號");
        stu_idHeaderTextView.setPadding(10, 10, 10, 10);
        headerRow.addView(stu_idHeaderTextView);

// 建立記錄列標題
        TextView stu_nameHeaderTextView = new TextView(this);
        stu_nameHeaderTextView.setText("姓名");
        stu_nameHeaderTextView.setPadding(10, 10, 10, 10);
        headerRow.addView(stu_nameHeaderTextView);

        TextView times_allHeaderTextView = new TextView(this);
        times_allHeaderTextView.setText("發表總次數");
        times_allHeaderTextView.setPadding(10, 10, 10, 10);
        headerRow.addView(times_allHeaderTextView);


// 將表頭行新增到表格佈局中
        tableLayout.addView(headerRow);

// 遍歷每日記錄列表並新增資料行
        for (Gen_table record : genTablesList) {
            TableRow row = new TableRow(this);
            TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
            row.setLayoutParams(layoutParams);

            //序號
            TextView idTextView = new TextView(this);
            idTextView.setText(String.valueOf(record.getId()));
            idTextView.setPadding(10, 10, 10, 10);
            row.addView(idTextView);

            //班級
            TextView stu_classTextView = new TextView(this);
            stu_classTextView.setText(record.getStu_class());
            stu_classTextView.setPadding(10, 10, 10, 10);
            row.addView(stu_classTextView);

            // 學號
            TextView stu_idTextView = new TextView(this);
            stu_idTextView.setText(record.getStu_id());
            stu_idTextView.setPadding(10, 10, 10, 10);
            row.addView(stu_idTextView);

            //姓名
            TextView stu_nameTextView = new TextView(this);
            stu_nameTextView.setText(record.getStu_name());
            stu_nameTextView.setPadding(10, 10, 10, 10);
            row.addView(stu_nameTextView);

            //發表總次數
            TextView times_allTextView = new TextView(this);
            times_allTextView.setText(String.valueOf(record.getTimes_all()));
            times_allTextView.setPadding(10, 10, 10, 10);
            row.addView(times_allTextView);


            // 將資料行新增到表格佈局中
            tableLayout.addView(row);
        }



    }

    private void SearchDaily(String searchText){
        List<DailyRecord> dailyRecordsList = Teacher_function.SearchDaily(searchText);

// 建立表頭行
        TableRow headerRow = new TableRow(this);
        TableRow.LayoutParams headerLayoutParams = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
        headerRow.setLayoutParams(headerLayoutParams);

        TextView stuidHeaderTextView = new TextView(this);
        stuidHeaderTextView.setText("學號");
        stuidHeaderTextView.setPadding(10, 10, 10, 10);
        headerRow.addView(stuidHeaderTextView);

        TextView stunameHeaderTextView = new TextView(this);
        stunameHeaderTextView.setText("姓名");
        stunameHeaderTextView.setPadding(10, 10, 10, 10);
        headerRow.addView(stunameHeaderTextView);

// 建立日期列標題
        TextView dateHeaderTextView = new TextView(this);
        dateHeaderTextView.setText("日期");
        dateHeaderTextView.setPadding(10, 10, 10, 10);
        headerRow.addView(dateHeaderTextView);

// 建立記錄列標題
        TextView recordHeaderTextView = new TextView(this);
        recordHeaderTextView.setText("記錄");
        recordHeaderTextView.setPadding(10, 10, 10, 10);
        headerRow.addView(recordHeaderTextView);

        TextView startimeHeaderTextView = new TextView(this);
        startimeHeaderTextView.setText("開始時間");
        startimeHeaderTextView.setPadding(10, 10, 10, 10);
        headerRow.addView(startimeHeaderTextView);

        TextView endtimeHeaderTextView = new TextView(this);
        endtimeHeaderTextView.setText("結束時間");
        endtimeHeaderTextView.setPadding(10, 10, 10, 10);
        headerRow.addView(endtimeHeaderTextView);


// 將表頭行新增到表格佈局中
        tableLayout.addView(headerRow);

// 遍歷每日記錄列表並新增資料行
        for (DailyRecord record : dailyRecordsList) {
            TableRow row = new TableRow(this);
            TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
            row.setLayoutParams(layoutParams);

            //學號
            TextView stuidTextView = new TextView(this);
            stuidTextView.setText(record.getStu_id());
            stuidTextView.setPadding(10, 10, 10, 10);
            row.addView(stuidTextView);

            //姓名
            TextView stunameTextView = new TextView(this);
            stunameTextView.setText(record.getStu_name());
            stunameTextView.setPadding(10, 10, 10, 10);
            row.addView(stunameTextView);


            // 新增日期資料
            TextView dateTextView = new TextView(this);
            dateTextView.setText(record.getDailyTime());
            dateTextView.setPadding(10, 10, 10, 10);
            row.addView(dateTextView);

            // 新增記錄資料
            TextView recordTextView = new TextView(this);
            recordTextView.setText(record.getDailyRecords());
            recordTextView.setPadding(10, 10, 10, 10);
            row.addView(recordTextView);


            //起始時間
            TextView startrecordTextView = new TextView(this);
            startrecordTextView.setText(record.getStartTime());
            startrecordTextView.setPadding(10, 10, 10, 10);
            row.addView(startrecordTextView);

            //結束時間
            TextView endrecordTextView = new TextView(this);
            endrecordTextView.setText(record.getEndTime());
            endrecordTextView.setPadding(10, 10, 10, 10);
            row.addView(endrecordTextView);


            // 將資料行新增到表格佈局中
            tableLayout.addView(row);
        }

    }

}

佈局檔案:

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="10dp">

    <TextView
        android:id="@+id/stuidTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"/>

    <TextView
        android:id="@+id/stunameTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:layout_marginStart="16dp"/>

    <TextView
        android:id="@+id/dateTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:layout_marginStart="16dp"/>

    <TextView
        android:id="@+id/recordTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:layout_marginStart="16dp"/>

    <TextView
        android:id="@+id/levelTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:layout_marginStart="16dp"/>

</LinearLayout>