Android Studio 动态表格显示效果

最终效果

一、先定义明细的样式 table_row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="40dp"android:background="@color/white"android:paddingLeft="10dp"android:paddingTop="2dp"android:layout_gravity="left"android:paddingBottom="2dp"><TextViewandroid:id="@+id/tab_row_col0"android:layout_width="350px"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_centerVertical="true"android:singleLine="true"android:text="row_col0"android:textColor="@color/colorPrimaryDark"android:textSize="18sp" /><TextViewandroid:id="@+id/tab_row_col1"android:layout_width="100px"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_toRightOf="@id/tab_row_col0"android:layout_centerVertical="true"android:singleLine="true"android:text="row_col1"android:textColor="@color/colorPrimaryDark"android:textSize="18sp" /><TextViewandroid:id="@+id/tab_row_col2"android:layout_width="100px"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_toRightOf="@id/tab_row_col1"android:layout_centerVertical="true"android:singleLine="true"android:text="row_col2"android:textColor="@color/colorPrimaryDark"android:textSize="18sp" /><TextViewandroid:id="@+id/tab_row_col3"android:layout_width="100px"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_toRightOf="@id/tab_row_col2"android:layout_centerVertical="true"android:singleLine="true"android:text="row_col3"android:textColor="@color/colorPrimaryDark"android:textSize="18sp" /><TextViewandroid:id="@+id/tab_row_col4"android:layout_width="100px"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_toRightOf="@id/tab_row_col3"android:layout_centerVertical="true"android:singleLine="true"android:text="row_col4"android:textColor="@color/colorPrimaryDark"android:textSize="18sp" /><TextViewandroid:id="@+id/tab_row_col5"android:layout_width="100px"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_toRightOf="@id/tab_row_col4"android:layout_centerVertical="true"android:singleLine="true"android:text="row_col5"android:textColor="@color/colorPrimaryDark"android:textSize="18sp"android:visibility="visible" /><!--可以按需求添加应用的最多列,没用到的地方通过代码隐藏--><Buttonandroid:id="@+id/tab_row_btn"android:layout_width="80sp"  android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_centerVertical="true"android:layout_marginRight="20dp"android:background="@android:color/transparent"android:padding="0dp"android:text="编辑"android:textColor="@color/colorPrimaryDark"android:textSize="18sp" /><!--            android:onClick="btnMinQuantity"--></RelativeLayout>

二、demo页面 activity_show_table.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="fill_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".Meals"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="60dp"android:orientation="horizontal"><!--定义查询条件--><RelativeLayoutandroid:id="@+id/laytitle"android:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="left|top"android:padding="5dp"><EditTextandroid:id="@+id/ed_mealname"android:layout_width="150dp"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:hint="菜品名称"android:text="" /><EditTextandroid:id="@+id/ed_mealpy"android:layout_width="150dp"android:layout_height="wrap_content"android:layout_marginLeft="20dp"android:layout_toRightOf="@id/ed_mealname"android:hint="拼音码"android:text="" /><Buttonandroid:id="@+id/btn_query"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_toRightOf="@id/ed_mealpy"android:text="查询" /><Buttonandroid:id="@+id/btn_downmeal"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_toRightOf="@id/btn_query"android:text="下载菜品" /></RelativeLayout></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="380dp"android:orientation="vertical"><!--显示空数据--><includeandroid:id="@+id/empty_view"layout="@layout/empty_list_item"android:layout_width="match_parent"android:layout_height="match_parent"/><!--表头, 和table_row.xml基本属性相同 --><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="@color/tv_hint_gray"android:paddingLeft="10dp"android:paddingTop="2dp"android:layout_gravity="left"android:paddingBottom="2dp"><!-- 可以根据表头各列的宽度 通过代码控制 显示明细的列的宽度,单位请用px--><TextViewandroid:id="@+id/tab_th_col0"android:layout_width="350px"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_centerVertical="true"android:singleLine="true"android:text="菜品名称"android:textColor="@color/colorPrimaryDark"android:textSize="18sp" /><TextViewandroid:id="@+id/tab_th_col1"android:layout_width="100px"android:layout_height="wrap_content"android:layout_toRightOf="@id/tab_th_col0"android:layout_centerVertical="true"android:layout_marginLeft="10dp"android:singleLine="true"android:text="单价"android:textColor="@color/colorPrimaryDark"android:textSize="18sp" /><TextViewandroid:id="@+id/tab_th_col2"android:layout_width="100px"android:layout_height="wrap_content"android:layout_toRightOf="@id/tab_th_col1"android:layout_centerVertical="true"android:layout_marginLeft="10dp"android:singleLine="true"android:text="数量"android:textColor="@color/colorPrimaryDark"android:textSize="18sp" /><TextViewandroid:id="@+id/tab_th_col3"android:layout_width="100px"android:layout_height="wrap_content"android:layout_toRightOf="@id/tab_th_col2"android:layout_centerVertical="true"android:layout_marginLeft="10dp"android:singleLine="true"android:text="th_col3"android:textColor="@color/colorPrimaryDark"android:textSize="18sp" /><TextViewandroid:id="@+id/tab_th_col4"android:layout_width="100px"android:layout_height="wrap_content"android:layout_toRightOf="@id/tab_th_col3"android:layout_centerVertical="true"android:layout_marginLeft="10dp"android:singleLine="true"android:text="th_col4"android:textColor="@color/colorPrimaryDark"android:textSize="18sp" /><TextViewandroid:id="@+id/tab_th_col5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_toRightOf="@id/tab_th_col4"android:layout_centerVertical="true"android:layout_marginLeft="10px"android:singleLine="true"android:text="th_col5"android:textColor="@color/colorPrimaryDark"android:textSize="18sp"android:visibility="gone" /><Buttonandroid:id="@+id/tab_th_btn"android:layout_width="80sp"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_centerVertical="true"android:layout_marginRight="20dp"android:background="@android:color/transparent"android:padding="0dp"android:text="编辑"android:textColor="@color/colorPrimaryDark"android:textSize="18sp" /></RelativeLayout><!--显示数据明细--><ListViewandroid:id="@+id/lsv_detail"android:layout_width="match_parent"android:layout_height="380dp" ></ListView></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="90dp"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><Buttonandroid:id="@+id/btn_nextpage"android:layout_width="60dp"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_centerVertical="true"android:layout_marginRight="20dp"android:paddingLeft="5dp"android:paddingRight="5dp"android:text="》" /><Buttonandroid:id="@+id/btn_uppage"android:layout_width="60dp"android:layout_height="wrap_content"android:layout_centerVertical="true"android:layout_marginRight="10dp"android:layout_toLeftOf="@id/btn_nextpage"android:text="《" /><TextViewandroid:id="@+id/tv_totalinfo"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_centerVertical="true"android:layout_marginLeft="10dp"android:text="共查询到100条记录,1/5页" /></RelativeLayout></LinearLayout></LinearLayout>

三、ShowTable.java

public class ShowTable extends AppCompatActivity implements View.OnClickListener {EditText ed_mealname;EditText ed_mealpy;Button btn_query;Button btn_downmeal;ListView lsv_detail;Button btn_nextpage;Button btn_uppage;TextView tv_totalinfo;List<dishmeal> mlst=null;MyAdapter<dishmeal> myAdapter=null;private View emptyView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_show_table);ed_mealname=findViewById(R.id.ed_mealname);ed_mealpy=findViewById(R.id.ed_mealpy);btn_query=findViewById(R.id.btn_query);btn_downmeal=findViewById(R.id.btn_downmeal);lsv_detail=findViewById(R.id.lsv_detail);btn_nextpage=findViewById(R.id.btn_nextpage);btn_uppage=findViewById(R.id.btn_uppage);tv_totalinfo=findViewById(R.id.tv_totalinfo);this.emptyView = findViewById(R.id.empty_view);btn_query.setOnClickListener(this);btn_downmeal.setOnClickListener(this);btn_nextpage.setOnClickListener(this);btn_uppage.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()){case R.id.btn_query:{DBManager.getInstance().init(ShowTable.this);List<String> qmlst=new ArrayList<>();mlst=DBManager.getInstance().getmeal(qmlst);myAdapter=new MyAdapter<dishmeal>(ShowTable.this , (ArrayList)mlst,R.layout.table_row,emptyView) {@Overridepublic void bindView(ViewHolder holder, dishmeal obj) {//设置值及对应列宽度(根据表头列的宽度)//注意各列的宽度和表头列的宽度要相同,否则会显示错位 ((TextView)findViewById(R.id.tab_th_col0)).getWidth()holder.setText(R.id.tab_row_col0, obj.getMealName(),((TextView)findViewById(R.id.tab_th_col0)).getWidth());holder.setText(R.id.tab_row_col1, String.valueOf(obj.getSellPrice()),((TextView)findViewById(R.id.tab_th_col1)).getWidth());holder.setText(R.id.tab_row_col2, "1",((TextView)findViewById(R.id.tab_th_col2)).getWidth());//设置隐藏及对应列宽度holder.setVisibility(R.id.tab_row_col3,1,((TextView)findViewById(R.id.tab_th_col3)).getWidth());holder.setVisibility(R.id.tab_row_col4,1,((TextView)findViewById(R.id.tab_th_col4)).getWidth());holder.setVisibility(R.id.tab_row_col5,0);holder.setVisibility(R.id.tab_row_btn,1,((Button)findViewById(R.id.tab_th_btn)).getWidth());//点击事件holder.setOnClickListener(R.id.tab_row_btn, new View.OnClickListener() {@Overridepublic void onClick(View v) {int iv=holder.getItemPosition();dishmeal m=mlst.get(iv);//Logger.d("dishmeal-"+iv+":"+ JSON.toJSONString(m));m.setSellPrice(130);//更新第iv行的值myAdapter.update(iv,m);}});}};lsv_detail.setAdapter(myAdapter);break;}case R.id.btn_downmeal:{break;}case R.id.btn_nextpage:{break;}case R.id.btn_uppage:{break;}default:{break;}}}}

四、MyAdapter  代码

CSDN

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/bicheng/52154.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

集团数字化转型方案(四)

集团数字化转型方案通过全面部署人工智能&#xff08;AI&#xff09;、大数据分析、云计算和物联网&#xff08;IoT&#xff09;技术&#xff0c;创建了一个智能化的企业运营平台&#xff0c;涵盖从业务流程自动化、实时数据监控、精准决策支持&#xff0c;到个性化客户服务和高…

实验七:独立按键实验

硬件电路图和题目; LED1-LD8是 P2口8个管脚 mian.c #include<reg52.h>sbit But1=P3^1 ; sbit But2=P3^0 ; sbit But3=P3^2 ; sbit But4=P3^3 ;sbit LED1 =P2^0 ; sbit LED2 =P2^1 ; sbit LED3 =P2^2 ; sbit LED4 =P2^3 ;#define PRESS_1 1 #define PRESS_…

SpringBoot内部模拟http请求到Controller(不通过本机网络)

使用场景: 1. 想实现自定义协议请求数据,但是还有拥有spring的 controller方便的业务处理流程和注解。 2. 想要调用对应的 controller 却不想处理自定义的 mapping。 一、首先是通过参考 spring-test 中的模拟实现的HttpServlet <dependency><groupId>org.spri…

CUTLASS 中的 47_ampere_gemm_universal_streamk 示例

前一篇文章介绍了 Stream-K: Work-centric Parallel Decomposition for Dense Matrix-Matrix Multiplication on the GPU 论文&#xff0c;下面对其代码实现进行分析。 cutlass 的 examples/47_ampere_gemm_universal_streamk 展示了 GEMM Stream-K 算法在 Ampere 架构上的使用…

Java 网络编程练习

InternetExercise1 package InternetExercise20240815;public class InternetExercise1 {public static void main(String[] args) {// 网络编程// 在网络通信协议下&#xff0c;不同计算机上面运行的程序&#xff0c;可以实现不同计算机上的数据传输// 网络编程三要素// 1.IP…

JNPF 5.0升级钜惠,感恩回馈永远在路上

尊敬的JNPF用户们&#xff1a; 经过引迈团队数月的辛勤努力和不断的技术创新&#xff0c;JNPF快速开发平台迎来全新升级——5.0版本&#xff01;此次5.0版本的迭代革新&#xff0c;不仅代表着我们技术实力的进一步提升&#xff0c;是我们对用户需求的深度理解和积极回应。为了…

重修设计模式-创建型-单例模式

重修设计模式-创建型-单例模式 一个类只允许创建一个对象&#xff08;或实例&#xff09;&#xff0c;那这个类就是一个单例类&#xff0c;这种模式叫做单例设计模式。 单例的主要使用场景有两个&#xff0c;一是使用单例控制全局的资源访问&#xff0c;也就是用单例封装一些工…

基于C# winform部署图像动漫化AnimeGANv2部署onnx模型

【界面截图】 【效果演示】 【部分实现代码】 using System; using System.Diagnostics; using System.Windows.Forms; using OpenCvSharp;namespace FIRC {public partial class Form1 : Form{Mat src null;public Form1(){InitializeComponent();}private void button1_Cli…

html+css+js网页设计 天猫首页

htmlcssjs网页设计 天猫首页 网页作品代码简单&#xff0c;可使用任意HTML编辑软件&#xff08;如&#xff1a;Dreamweaver、HBuilder、Vscode 、Sublime 、Webstorm、Text 、Notepad 等任意html编辑软件进行运行及修改编辑等操作&#xff09;。 获取源码 1&#xff0c;访问…

git本地仓库同步到远程仓库

整个过程分为如下几步&#xff1a; 1、本地仓库的创建 2、远程仓库的创建 3、远程仓库添加key 4、同步本地仓库到远程仓库 1、本地仓库的创建&#xff1a; 使用如下代码创建本地仓库&#xff1a; echo "# test" >> README.md git init git add README.md …

用户增长:策略与实践,驱动SaaS企业持续繁荣

在当今这个数字化时代&#xff0c;用户增长已成为所有行业&#xff0c;尤其是SaaS&#xff08;Software as a Service&#xff0c;软件即服务&#xff09;企业生存与发展的核心驱动力。用户增长不仅关乎市场份额的扩大&#xff0c;更是企业价值实现和持续盈利的基石。那么&…

【计算机网络】网络版本计算器

此前我们关于TCP协议一直写的都是直接recv或者read&#xff0c;有了字节流的概念后&#xff0c;我们知道这样直接读可能会出错&#xff0c;所以我们如何进行分割完整报文&#xff1f;这就需要报头来解决了&#xff01; 但当前我们先不谈这个话题&#xff0c;先从头开始。 将会…

【秋招笔试】8.18大疆秋招(第一套)-后端岗

🍭 大家好这里是 春秋招笔试突围,一起备战大厂笔试 💻 ACM金牌团队🏅️ | 多次AK大厂笔试 | 编程一对一辅导 ✨ 本系列打算持续跟新 春秋招笔试题 👏 感谢大家的订阅➕ 和 喜欢💗 和 手里的小花花🌸 ✨ 笔试合集传送们 -> 🧷春秋招笔试合集 🍒 本专栏已收…

Springboot发邮件功能如何实现?详细步骤?

Springboot发邮件配置指南&#xff1f;如何集成Spring Mail发邮件&#xff1f; 无论是用户注册、密码重置还是重要通知的发送&#xff0c;邮件都是不可或缺的沟通方式。Springboot作为一个流行的Java开发框架&#xff0c;提供了简洁易用的方式来实现邮件功能。AokSend将详细探…

音频转换器有哪些?一键转换,轻松享受

暑假里&#xff0c;你是否也沉浸在激情四溢的演唱会中&#xff0c;用手机记录下了那些难忘的现场音频&#xff1f; 但回到家中&#xff0c;想要将这些珍贵的现场记忆从手机迁移到电脑上永久保存时&#xff0c;却遇到了格式不兼容的难题。 别担心&#xff0c;今天我们就要解决…

基于Python的机器学习系列(8):Newton Raphson逻辑回归

在本篇博文中&#xff0c;我们将探讨一种比传统梯度下降更高效的优化方法——Newton Raphson方法&#xff0c;并学习如何在逻辑回归中应用它。Newton Raphson方法通过利用二阶导数的曲率信息&#xff0c;快速地找到使代价函数最小化的参数。尽管这种方法在处理较小规模的数据集…

04 C++语言---数据类型

C中数据分为两种&#xff0c;一种是可以被修改的数据&#xff0c;这种数据一般被成为变量&#xff0c;还有一种数据是常量。 变量&#xff1a;变量在程序中的使用场景有很多&#xff0c;一般常用的数据类型都属于常量。例如 int a&#xff1b;等。 常量&#xff1a;常量是在程…

前端项目重新打包部署后如何通知用户更新

前端项目重新打包部署后如何通知用户更新 前端项目重新打包部署后如何通知用户更新常用的webSocket解决方案纯前端方案路由拦截多线程main.ts中 创建多线程多线程逻辑处理 前端项目重新打包部署后如何通知用户更新 前端项目重新打包部署后&#xff0c;由于用户没及时更新页面&…

关于python的Django项目性能优化

CPU—— Python编写的Django程序在服务器上消耗CPU的主要原因包括频繁的IO操作、大量的计算任务、以及第三方库的性能问题。‌ ‌频繁的IO操作‌&#xff1a;当Python程序频繁进行IO操作&#xff0c;如读写文件、网络请求等&#xff0c;会导致CPU占用过高。这是因为IO操作通常是…

什么是OpenTiny?

OpenTiny 是一套企业级的 Web 前端开发解决方案&#xff0c;提供跨端、跨框架的 UI 组件库和低代码引擎&#xff0c;帮助开发者高效构建 Web 应用 。企业运用开发中&#xff0c;可以利用 OpenTiny 的以下核心组件和优势&#xff1a; TinyVue 组件库&#xff1a;一个丰富的组件库…