android xml 列表展示,Android中ListView实现展示列表数据

1、在activity_main.xml中添加一个ListView

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical"

tools:context="${relativePackage}.${activityClass}" >

android:layout_height="wrap_content"

android:id="@+id/lvList">

2、新建一个layout文件用来作为list的一行格式文件

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

tools:context="${relativePackage}.${activityClass}" >

android:id="@+id/tvId"

android:layout_width="30dip"

android:layout_height="wrap_content"

android:textSize="25sp"

android:gravity="left"

/>

android:id="@+id/tvName"

android:textSize="25sp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

/>

android:id="@+id/tvAge"

android:textSize="25sp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

/>

3、

package com.zlz.androidxml;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import android.app.Activity;

import android.os.Bundle;

import android.os.Environment;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.ListAdapter;

import android.widget.ListView;

import android.widget.SimpleAdapter;

import android.widget.Toast;

import com.zlz.androidxml.domain.Person;

import com.zlz.androidxml.service.ParseService;

import com.zlz.androidxml.service.PullParseServiceImpl;

import com.zlz.androidxml.service.SaxParseServiceImpl;

public class MainActivity extends Activity implements OnClickListener {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

findViewById(R.id.btnPull).setOnClickListener(this);

}

@Override

public void onClick(View v) {

List persons = new ArrayList();

for(int i =1;i<4;i++){

Person person = new Person();

person.id = i;

person.name = "lizhi"+i;

person.age = 12+i;

persons.add(person);

}

popListView(persons);

}

//将List放到一个grid里面展示

private void popListView(List persons) {

List> ls = new ArrayList>();

Map map = null;

//需要将对象封装为一个map的集合

for (Person p : persons) {

map = new HashMap();

map.put("id", p.id);

map.put("name", p.name);

map.put("age", p.age);

ls.add(map);

}

//使用标签ListView存放

ListView lvList = (ListView) findViewById(R.id.lvList);

//将list中每个对象虚拟为一个Item,然后再存入Grid中的每一行,listitemlayout就相当于一个item

ListAdapter adapter = new SimpleAdapter(this, ls, R.layout.listitemlayout,

new String[] {"id","name","age"}, new int[] {R.id.tvId,R.id.tvName,R.id.tvAge});

lvList.setAdapter(adapter);

}

}

将文件放在TableLayout中

1、在activity_main.xml中添加Table_layout

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical"

tools:context="${relativePackage}.${activityClass}" >

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

tools:context="${relativePackage}.${activityClass}" >

android:id="@+id/btnPull"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="Pull" />

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:stretchColumns="0,1,2"

android:id="@+id/tlLayout"

>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="ID"

/>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="NAME"

/>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="AGE"

/>

2、在MainActivity中循环创建Row然后添加到Table上

package com.zlz.androidxml;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.TableLayout;

import android.widget.TableRow;

import android.widget.TextView;

import com.zlz.androidxml.domain.Person;

public class MainActivity extends Activity implements OnClickListener {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

findViewById(R.id.btnSax).setOnClickListener(this);

findViewById(R.id.btnDom).setOnClickListener(this);

findViewById(R.id.btnPull).setOnClickListener(this);

}

@Override

public void onClick(View v) {

List persons = new ArrayList();

for(int i =1;i<4;i++){

Person person = new Person();

person.id = i;

person.name = "lizhi"+i;

person.age = 12+i;

persons.add(person);

}

//获取TableLayout

TableLayout tl = (TableLayout) findViewById(R.id.tlLayout);

int childrenCount = tl.getChildCount();

// 防止每次查询重复添加,所以每次拼装为table时,除了表头,其他的全部干掉

for (int i = childrenCount - 1; i > 0; i--) {

View view = tl.getChildAt(i);

tl.removeView(view);

}

while (cursor.moveToNext()) {

TableRow row = new TableRow(this);

TextView idView = new TextView(this);

idView.setText(cursor.getString(cursor.getColumnIndex("id")));

row.addView(idView);

TextView nameView = new TextView(this);

nameView.setText(cursor.getString(cursor.getColumnIndex("name")));

row.addView(nameView);

TextView ageView = new TextView(this);

ageView.setText(cursor.getString(cursor.getColumnIndex("age")));

row.addView(ageView);

// 将每一行添加到table上

tl.addView(row);

}

}

}

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

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

相关文章

android的证书链管理,Android手册X509证书链验证

我已在我的代码中实现了javax.net.ssl.X509TrustManager,因此我可以验证我的软件访问的自签名证书.但是,我仍然需要验证其他一些“标准”网站SSL证书.我使用CertPathValidator.validate()来做到这一点,但我刚刚意识到我传递的一个证书链(对于maps.googleapis.com)实际上并不包含…

lua android弹窗关闭,安卓精灵lua调用pm指令实现清除App缓存,举个栗子的说

今天刚接触这玩意&#xff0c;试着写点Demo玩玩, 功能要求能自动登陆&#xff0c;在登陆前要先清空游戏的缓存数据&#xff0c;不然游戏就自动登陆了(无法切到账号输入窗口)本想写图色脚本来实现吧&#xff0c;多种品牌的手机&#xff0c;多种模拟器&#xff0c;多种桌面主题&a…

android打印html页面,Android打印HTML文档

来源官网,总结用.WebView类在Android 4.4(API Level 19)中得到了更新&#xff0c;使得它可以打印HTML内容。该类允许我们加载一个本地HTML资源或者从网页下载一个页面&#xff0c;创建一个打印任务&#xff0c;并把它交给Android打印服务。1.[代码]如何构建一个HTML的字符串并将…

cocos android-1,Cocos2D-Android-1之源码详解:5.Box2dTest

Cocos2D-Android-1之源码详解&#xff1a;5.Box2dTest发布时间&#xff1a;2020-08-06 06:19:28来源&#xff1a;51CTO阅读&#xff1a;398作者&#xff1a;abab99package org.cocos2d.tests;import java.util.Iterator;import org.cocos2d.actions.UpdateCallback;import org.…

三星s10能升级android11,三星 S10+手机已在测试 Android 11 系统

IT之家2月25日消息 谷歌本月初发布了首个Android 11开发者预览版&#xff0c;首先面向Pixel手机&#xff0c;不过看起来三星已经在Galaxy S10 手机开始测试最新系统。IT之家从Geekbench数据库中获知&#xff0c;上面出现了运行Android R&#xff0c;型号为SM-G975F的三星Galaxy…

android 5. 蓝牙 mesh,蓝牙mesh组网

智能照明是智能家居的一个重要入口&#xff0c;传统照明方案存在布线复杂&#xff0c;控制单一等问题。搭配飞易通MESH组网模组替换传统方案&#xff0c;无需额外的布线。提供更智能的控制&#xff0c;更极致的用户体验。一、MESH应用领域:蓝牙5.0MESH是由SIG蓝牙联盟发布建立的…

android opencv 银行卡识别,NDK 开发之使用 OpenCV 实现银行卡号识别

前言在日常的开发中&#xff0c;我们有时会遇到添加银行卡的需求&#xff0c;这时候&#xff0c;产品可能会让你仿一下支付宝之类的相机扫描识别银行卡号。很多时候&#xff0c;做这样的需求会去找找稳定的第三方&#xff0c;本文通过 OpenCV 结合识别的需求带你分析如何实现银…

鸿蒙测试机型微博,华为多款机型开启鸿蒙尝鲜:微博已适配HarmonyOS小尾巴

日前&#xff0c;华为已经正式宣布&#xff0c;将于6月2日晚20点召开鸿蒙操作系统及华为全场景新品发布会&#xff0c;届时将正式发布鸿蒙OS正式版。同时&#xff0c;今天华为还开启了鸿蒙OS首批消费者尝鲜计划&#xff0c;其中正式版可参与机型包括Mate 40系列、Mate X2、Mate…

android今日头条刷新,仿今日头条刷新vector动画

一般的刷新动画是一个圈圈在转&#xff0c;而头条的比较特殊&#xff0c;直接上写好的效果图(一直不知道怎么把图片尺寸调小o(╯□╰)o)吧~刷新动画_.gif首先整个效果是通过SVG和vector来实现的&#xff0c;如果不是很了解&#xff0c;请看大佬的文章&#xff1a;SVG学习--Anim…

android懒加载单实例,【 Android 10 设计模式 】系列 -- 单例

前言由于源码分析的代码量比较大&#xff0c;大部分博客网站的内容显示页面都比较窄&#xff0c;显示出来的效果都异常丑陋&#xff0c;所以您也可以直接查看 《 Thinking in Android 》 来阅读这边文章&#xff0c;希望这篇文章能帮你梳理清楚 “ 单例模式 ”。一、概述1.1 什…

android资产目录,android – 从非目录设备中的资产文件夹复制数据库

我正在尝试从资产文件夹将数据库复制到设备.此代码在模拟器和根设备上正常工作.我只是想知道是否在无人看管的设备上创建任何问题,否则它会相同.private void StoreDatabase() {File DbFile new File("data/data/packagename/DBname.sqlite");if (DbFile.exists()) …

在html中标题字号一共有几种,HTML中常用的几种标签

在HTML中&#xff0c;标签是首要的&#xff0c;也是最重要的东西。一旦进入HTML&#xff0c;认识和理解标签是基本的需要&#xff0c;因为这是区分HTML代码与普通文本的分隔符。这些标签是用来显示文档中的普通文本或转化文本的指令的标签。什么是转化后的文本&#xff1f;要显…

html静态页面引用其他页面,Shtml完美解决静态页面内部调用其他页面(非Iframe、Object、Js方法)...

我想这个是所有前端工程师都会碰到的问题&#xff0c;在你做了很多页面&#xff0c;需要调用同一个头部或者底部的时候&#xff0c;需要嵌套一下&#xff0c;这个时候怎么办Iframe、Object、Js调用的方法就不讨论了&#xff0c;网上搜索一大堆&#xff0c;不过兼容性不好这里给…

鸿蒙手机如何录屏,安卓手机如何屏幕录制视屏?手机视频录制方法

安卓手机如何屏幕录制视屏&#xff1f;手机视频录制方法2018年12月17日 17:05作者&#xff1a;黄页编辑&#xff1a;黄页分享随着科技的不断进步发展,手机已经成为人类不可缺少的一种生活神器,人们已经不满足只是用来打打电话、发发短信那么简单了,手机的用途主要用来社交、娱乐…

html判断为空的函数,javascript怎么判断是否为空字符串?

JavaScript中可以使用if(typeof obj"undefined"||objnull||obj"")语句通过判断字符串的数据类型来判断字符串是否为空。判断字符串是否为空的方法函数&#xff1a;function isEmpty(obj){if(typeof obj "undefined" || obj null || obj "…

html或原生js是单一对应绑定的,原生js数据绑定

双向数据绑定是非常重要的特性 —— 将JS模型与HTML视图对应&#xff0c;能减少模板编译时间同时提高用户体验。我们将学习在不使用框架的情况下&#xff0c;使用原生JS实现双向绑定 —— 一种为Object.observe_(译注&#xff1a;现已废弃&#xff0c;作者写博客时为14年11月)&…

et200sp模块接线手册_西门子PN/PN耦合器学习应用系列(1)-外观及接线

早在2017年我曾写过两篇文章介绍西门子PN/PN耦合器&#xff0c;文章链接如下&#xff1a;初识西门子PNPN耦合器(PN/PN Coupler)&#xff1b;如何在博途(TIA Portal)环境下组态PNPN耦合器&#xff1f;当时PN/PN耦合器的固件版本还是V3.x。随着产品的升级&#xff0c;新版本的PN/…

textview加载html glide,TextView加载HTML,文字和图片

原文出处链接&#xff1a;《TextView加载HTML&#xff0c;文字和图片》工具类&#xff1a;public class ImageGetterUtils {public static MyImageGetter getImageGetter(Context context, TextView textView) {MyImageGetter myImageGetter new MyImageGetter(context, textV…

js 条码枪扫描_年会展台 精彩不断 | 沧田:从打印到扫描录入 国产品牌从未停止...

11月23日-25日&#xff0c;中国现代办公行业年会(以下简称COAA年会)在南昌召开。今年对于OA行业而言&#xff0c;国产品牌的崛起成为主要特征之一。以针式打印机起家的沧田&#xff0c;在本次展会中展示了多款重量级产品&#xff0c;涵盖了针式打印机、激光一体机、条码打印机、…

投后管理岗面试_2020天津水务招79人,管理岗+操作岗,专科起报

Hello大家好&#xff0c;我们今天的国企招聘主要说的是天津水务。天津水务的公告和去年相比晚了几个月&#xff0c;而且要求也变了一些——变成了校招&#xff08;要求2020年应届生&#xff09;&#xff0c;虽然条件还是不高——大专起报。2点要求基本的条件就是要求&#xff1…