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