首页 > 其他分享 >第七周周三

第七周周三

时间:2024-04-10 21:11:41浏览次数:14  
标签:10 第七 setText 周三 addView import new TextView

学习了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>

 

标签:10,第七,setText,周三,addView,import,new,TextView
From: https://www.cnblogs.com/atrue/p/18127432

相关文章

  • 大菜菜学习RabbitMQ——第七篇
    这一篇文章我们讲的是另一个转换机topic转换机topic:话题相对于其他交换机来说这个交换机有很强的灵活性首先我们需要创建两个queue,名字的话这样就可以 然后创建对应的交换机在这里交换机的类型应该要选择为topic然后我们就可以开始binding了 这里我们看到有个符号我们......
  • 第七章 常用API&异常
    7-1API的基本使用System类方法说明publicstaticvoidexit(intstatus) // 终止当前运行的Java虚拟机,非零表示异常终止publicstaticlongcurrentTimeMillis() //返回当前时间(以毫秒为单位)  Objects类(应用)说明publicstaticStringtoString(对象)//返回......
  • 【Spring进阶系列丨第七篇】Spring框架新注解分类及详解
    文章目录一、Spring新注解1.1、Configuration注解1.1.1、定义一个类1.1.2、使用Configuration注解修饰类1.1.3、作用1.2、Bean注解1.2.1、定义bean1.2.2、在主配置类中注册bean1.2.3、测试容器中是否有该bean1.2.4、注册bean的同时可以指定bean名称1.2.5、补充内容1.2.5......
  • 第七周后端学习报告
    CJT的学习报告 本周学习及进展 AOPLogback自定义注解 具体进展使用这三个技术栈实现了日志框架日志框架github地址:cjt666-hhh/sosdDemo(github.com)首先使用AOP技术进行解耦,使得日志框架能够灵活配置到多个方法以及接口,后续对于对于日志框架维护成本降低,仅需修......
  • 学python的第七天
    python中的数据结构python中有四种数据结构,列表,字典,元组,集合列表(list):list=[val1,val2,val3,val4]字典(dict):dict={key1:val1,key2:val2}元组(tuple):tuple=(val1,val2,val3,val4)集合(set):set={val1,val2,val3,val4}1,列表#1,列表#列表中的每一个元素都是可变......
  • ETL工具-nifi干货系列 第七讲 处理器JoltTransformJSON(续)
    第六讲教程只简单介绍了Jolt的chain转换模式,本节课介绍下Jolt的各种转换模式。点击的处理器JoltTransformJSON高级配置选项,进行测试Jolt的转换模式。 1、Cardinality:更改了输入JSON数据元素的基数,适用于jsonObj和jsonList之间的转换。list转为obj input{"review......
  • ETL工具-nifi干货系列 第七讲 处理器JoltTransformJSON(续)
    第六讲教程只简单介绍了Jolt的chain转换模式,本节课介绍下Jolt的各种转换模式。点击的处理器JoltTransformJSON高级配置选项,进行测试Jolt的转换模式。1、Cardinality:更改了输入JSON数据元素的基数,适用于jsonObj和jsonList之间的转换。list转为objinput{"review......
  • 大学教材《C语言程序设计》(浙大版)课后习题解析 | 第七、八章
    概述    本文主要提供《C语言程序设计》(浙大版)第七、八章的课后习题解析,以方便同学们完成题目后作为参考对照。后续将更新第九、十章节的课后习题解析,如想了解更多,请持续关注该专栏。专栏直达链接:《C语言程序设计》(浙大版)_孟俊宇-MJY的博客-CSDN博客​http://t.cs......
  • JavaWeb学习笔记——第七天
    MySQL(二)数据库查询-DQLDQL英文全称是DataQueryLanguage(数据查询语言),用来查询数据库表中的记录。关键字:SELECT语法关键字和参数说明select字段列表基本查询from表名列表where条件列表条件查询groupby分组字段列表分组查询having分组后条件列......
  • 第七章 流编辑器sed
    第七章流编辑器sedsed是一个脚本型,非交互式的编辑器。sed没有交互式的编辑界面,光标移动以及庞大的快捷键功能。1,工作原理1,sed一次处理一行内容。处理时,把当前的行存储在临时缓存区(模式空间)中,接着用sed命令处理缓冲区中的内容,处理完成后,再把缓冲区的内容输出到屏幕。如此......