3.30

umiQa發表於2024-03-31

所花時間:3小時

程式碼量:309

部落格篇:1

使用自定義表格查詢示例,

總結統計:

package com.example.studyapplication;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.sql.SQLException;

public class statsFinal extends AppCompatActivity implements View.OnClickListener {

    private LinearLayout mainLinerLayout;
    private RelativeLayout relativeLayout;
    private Ststats[] s=new Ststats[100];
    private Button bt_final;
    private String[] name={"序號","學號","姓名","班級","發表總次數"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stats_final);
        for(int i=0;i<100;i++){
            s[i]=new Ststats();
        }
        bt_final = findViewById(R.id.bt_final);
        bt_final.setOnClickListener(this);
        mainLinerLayout = (LinearLayout) this.findViewById(R.id.MyTable);
        initData();
    }
    private void initData() {
        //初始化標題
        relativeLayout = (RelativeLayout) LayoutInflater.from(statsFinal.this).inflate(R.layout.table, null);
        MyTableTextView title = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_1);
        title.setText(name[0]);
        title.setTextColor(Color.BLUE);
        title = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_2);
        title.setText(name[1]);
        title.setTextColor(Color.BLUE);
        title = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_3);
        title.setText(name[2]);
        title.setTextColor(Color.BLUE);
        title = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_4);
        title.setText(name[3]);
        title.setTextColor(Color.BLUE);
        title = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_5);
        title.setText(name[4]);
        title.setTextColor(Color.BLUE);
        mainLinerLayout.addView(relativeLayout);
        new Thread(new Runnable() {
            @Override
            public void run() {
                Dao d =new Dao();
                try {
                    s=d.statsFinal();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();
        //初始化內容
    }

    @Override
    public void onClick(View v) {
        int number = 1;
        int i=0;
        while (s[i].getId()!=null){
            relativeLayout = (RelativeLayout) LayoutInflater.from(statsFinal.this).inflate(R.layout.table, null);
            MyTableTextView txt = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_1);
            txt.setText(String.valueOf(number));

            txt = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_2);
            txt.setText(s[i].getId());
            txt = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_3);
            txt.setText(s[i].getName());
            txt = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_4);
            txt.setText(s[i].getUnit());
            txt = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_5);
            txt.setText(s[i].getNum());
            mainLinerLayout.addView(relativeLayout);
            number++;
            i++;
        }
    }
}