ListActivity

【Android笔记100】Android之列表活动组件ListActivity的使用 ArrayAdapter是一个数组数据适配器,它只能够显示一个简单的布局,并且只能是【TextView】文本组件。它有多个构造方法,通过指定【TextView】组件的资源ID,告诉android应该将数据渲染到哪个组件上面。/**Context context:上下文对象int resource:ListView布局中每一项item的布局文件IDint textViewResourceId:对应的TextView组件的资源IDT[] objects:需要渲染的数组数据*/ 阅读详情

ListActivity中比较复杂的部分就是配置其Adapter,Adapter常用的有ArrayAdapter和SimpleAdapter。

 

首先我们先看一下简单的ArrayAdapter:ListActivity不同于普通Activity的地方是该Activity里边有一个ListView,该ListView绑定了Adapter中的数据。在这个示例中,该ListView绑定了包含三个元素的ArrayList,这三个元素分别是”1, 2, 3”。




 

         主要代码:

         首先我们需要让我们的类继承于ListActivity

   1: pubic class ListActivityTest extends ListActivity
   2: {
   3: …..
   4: }

 



让我们直接看代码吧:

   1: public class ListActivityTest extends ListActivity
   2: {
   3:     /** Called when the activity is first created. */
   4:     @Override
   5:     public void onCreate(Bundle savedInstanceState) 
   6:     {
   7:         super.onCreate(savedInstanceState);
   8:         this.setTheme(android.R.style.Theme_Black);
   9:     //    setContentView(R.layout.main);
  10:         
  11:         List items = fillArray();
  12:         
  13:         ArrayAdapter adapter = new ArrayAdapter(
  14:                 this, android.R.layout.simple_expandable_list_item_1, items);
  15:         this.setListAdapter(adapter);
  16:     }
  17:     
  18:     private List fillArray()
  19:     {
  20:         List items = new ArrayList();
  21:         items.add("1");
  22:         items.add("2");
  23:         items.add("3");
  24:         return items;
  25:     }
  26: }

fillArray()返回了一个有三个String对象的List集合,我们在后边用来绑定ListView显示的内容。

然后我们需要在类中的onCreate()函数里边添加如下内容:

List items = this.getData();  // 获得待绑定的集合

ArrayAdapter adapter = new ArrayAdapter(

                this, android.R.layout.simple_expandable_list_item_1,

 items);

         this.setListAdapter(adapter);

其中:this代表当前的Context;android.R.layout.simple_expandable_list_item_1代表一个包含ListView的layout的ID,系统自带了几种,我们在这里选择的是系统自带的;items表示被绑定的数据源。

 

OK,简单的看完我们就可以看复杂一点的SimpleAdapter:

我们还是先来看一下效果:

 

先上代码:

   1: public class ListActivityTest extends ListActivity
   2: {
   3:     /** Called when the activity is first created. */
   4:     @Override
   5:     public void onCreate(Bundle savedInstanceState) 
   6:     {
   7:         super.onCreate(savedInstanceState);
   8:         
   9:         List> listContent = this.getData();
  10:         
  11:         SimpleAdapter adapter = new SimpleAdapter(this,
  12:                 listContent,
  13:                 android.R.layout.simple_list_item_2,
  14:                 new String[] {"Name", "Address"},
  15:                 new int[] {android.R.id.text1, android.R.id.text2});
  16:         
  17:         setListAdapter(adapter);
  18:     }
  19:     
  20:     private List> getData()
  21:     {
  22:         List> listContent = new ArrayList>();
  23:         
  24:         Hashtable table1 = new Hashtable();
  25:         table1.put("Name", "LiLi");
  26:         table1.put("Address", "China");
  27:         listContent.add(table1);
  28:         
  29:         Hashtable table2 = new Hashtable();
  30:         table2.put("Name", "NiuNiu");
  31:         table2.put("Address", "ChinaHeibei");
  32:         listContent.add(table2);
  33:         
  34:         return listContent;
  35:     }
  36: }

 

讲解:

SimpleAdapter可以实现比ArrayAdapter更加复杂的ListView界面,我们这个程序实现的是一项中有两行文字的ListView。

其中的getData()函数返回的是一个List,这个不懂的朋友请查阅相关的Java基础资料。在onCreate()函数中构造了一个SimpleAdapter(),我们看一下SimpleAdapter()的构造函数:

SimpleAdapter(Context context, List> data, int resource, String[] from, int[] to)

其中:

context : 当前的环境上下文,一般直接传递this就OK

Data:数据源,我们之前构造的listContent就是该ListView的数据源

resource:layout模板资源,我们可以传递Android自带的,当然也可以自定义,我们在这里为了简单传递的是Android自带的

from和to:这个我们需要联合起来看,这两个是平行数组,也就是数组的大小相同,根据我们的例子,我们需要在android.R.id.text1中显示数据源中索引为”name”的项,在android.R.id.text2种显示索引为”address”的项。




Android ListActivity与BaseAdapter应用实例教程 ListActivity是Android平台的一个特殊Activity类,用于展示一个列表项的集合。它继承自普通Activity,并自带一个ListView组件,开发者只需要专注于列表数据的提供和列表项的布局设计。ListActivity简化了列表相关的操作,使得开发者可以更加便捷地实现列表展示功能。在Android开发中,BaseAdapter是一个强大的工具,允许开发者将数据源和界面组件紧密地绑定在一起。要创建一个自定义适配器,首先需要继承BaseAdapter类。 阅读详情

相关推荐

Win10 Xbox游戏安装错误终极解决方案

Win10 Xbox游戏安装错误终极解决方案作为Windows 10系统深度用户,相信不少玩家都遇到过Xbox应用安装游戏失败的问题。这类问题轻则影响游戏体验,重则导致系统资源占用异常。本文将结合微软官方技术文档与实际维修案例,系统讲解Xbox游戏安装错误的深层原因与解决方案,帮助读者建立完整的故障处理思维体系。

mmoo_python的博客 2065

ListActivity---

ListActivity实际上是ListView和Activity的结合体。也就是说,一个ListActivity就是只包含一个ListView组件的Activity。在ListActivity类内部通过代码来创建ListView对象,因此,使用ListActivity并不需要使用XML布局文件来定义ListView组件。    如果在某些Activity中只包含一个ListView,使用Lis

yyb.ye专栏 1341

椭圆曲线点加的推导公式(一)

椭圆曲线点加推导过程

qq_32100199的博客 1516

安卓-ListActivity的使用

ListActivity可以理解为是ListView和Activity的结合。主要用来显示列表数据。显示数据需要设置适配器。Adapter常用的有ArrayAdapter,SimpleAdapter和CursorAdapter。ListActivity的使用步骤:1)继承自ListActivity,如:xxx extends ListActivity2)重写onCreate方法,在该方法中,需要做...

丿灬安之若死 4554

Unity中基于SkinMeshRenderer的骨骼动画换装系统优化实践

本文深入探讨了Unity中基于SkinnedMeshRenderer的骨骼动画换装系统优化实践。针对换装卡顿、显示错误等常见问题,文章剖析了SkinnedMeshRenderer的核心数据原理,对比了“数据替换”与“实体替换”两种主流方案的性能差异,并详细阐述了通过骨骼字典缓存、对象池管理、材质合并与Draw Call优化等实战技巧,构建高性能、高稳定性的生产级换装系统。

weixin_29008055的博客 314

永磁同步电机高效标定技术:从理论到实践的全面解析

本文全面解析永磁同步电机高效标定技术,从MTPA标定、弱磁区优化到初始角校准,详细介绍了理论原理和实战技巧。针对新能源汽车应用场景,提供标定系统集成要点和AI辅助优化方案,帮助工程师提升电机性能标定效率与精度。

weixin_28452161的博客 502

ListActivity类的用法

一、ListActivity类继承Activity类,默认绑定了一个ListView(列表视图)界面组件,并提供一些与列表视图、处理相关的操作。 一般首界面才用ListActivity。ListActivity中默认绑定ListView,其listView界面组件中的每一行(row)都可以被选择。我们可以借由设置一个onListItemClick方法,来得到用户正选择了哪一列的信息,并做后

iblade的博客 919

【SAP GUI 脚本 VBA】实战:构建Excel驱动的SAP自动化操作平台

本文详细介绍了如何利用VBA和SAP GUI脚本构建Excel驱动的SAP自动化操作平台,实现高频事务码的标准化封装和批量处理。通过实战案例展示物料主数据查询(MM03)、会计凭证修改(FB02)等场景的自动化实现,帮助用户提升10倍+操作效率,同时确保数据可追溯性和操作安全性。

weixin_33748818的博客 503

Android控件之ListActivity

1、ListActivity简介 ListActivity可以理解为ListView和Activity的结合,与前一篇的ListView和Activity组合实现的差别不大,主要是相对方便一些,但是有一下几点需要注意 1、ListActivity可以不调用setContentView(R.layout.main),它默认是ListView占满全屏。 2、如果想在屏幕中显示其他控件,如文本框和

u010327957的博客 669

系统安装(kuntaiR522 kvm安装)

鲲泰R522服务器kvm安装rocky

weixin_46396917的博客 1820

基于Gossip算法的柔性互联自治微网群分布式协调控制策略:复现代码与详细解释

摘要(149字): 本文基于Gossip算法实现了柔性互联微网群(MMG)的分布式协调控制策略仿真。研究构建了包含3个微网、10个分布式电源(DG)的MATLAB/Simulink模型,采用双层控制架构:上层通过柔性交流开关(FAS)实现微网间功率协调,下层利用Gossip算法完成DG间的边际成本一致性控制。重点开发了异步通信的Gossip算法核心函数,实现了分布式电源的自主边际成本估计与功率分配。仿真验证了系统在负荷变化和DG故障等工况下的协调控制能力,结果表明该策略能有效维持微网群的经济运行与功率平衡。

huanghm88的专栏 281

ListActivity简介

public class ListActivity extends Activityjava.lang.Object   ↳android.content.Context    ↳android.content.ContextWrapper     ↳android.view.ContextThemeWrapper      ↳android.app.Activity       ↳android.app.ListActivityKnown Direct Subclasses LauncherActi

1万+

Open3D 可视化(1)——简单可视化【2026最新版】

简单可视化,博客长期更新,本文最新更新时间为:2026年5月10日。

点云侠的博客 5684

android listActivity

ListActivity     ListActivity是一个专门显示ListView的Activity类,它内置了ListView对象,只要我们设置了数据源,就会自动地显示出来。   使用custom view for screen layout      虽然ListActivity内置了ListView对象,但我们依然可以使用custom view,通过在onCre

上善若水 1557

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

Android:ListActivity之学习

ListActivity是Activity的一个子类。 Class OverviewAn activity that displays a list of items by binding to a data source such as an array or Cursor, and exposes event handlers when the user selects an item. ListActivity hosts a ListView object that can be bound to

如果这是你所爱的,要坚持,并踏实。 5142

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

android中listactivity的使用

ListActivity的使用 ListActivity非常有用,本人学习了好长时间才稍有了解,首先谢谢各位网友的分享,从中学到了很多。现在把自己所学也发一下下。 在使用ListActivity时可以不设置setContentView(); private String []cities = {"Hangzhou","Xuzhou",&quo

three_trees的专栏 399

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

探索ListActivity

当我们想要实现带有图片的listView的时候我们可以通过listView的方式来实现,也可以通过listActivity的方式来实现,下面我为大家展示一下如何利用ListActivity的方式来实现: 首先我们要在我们的布局文件中,写出我们想在listview里面每一行所展示的东西,例如我们让我们的每一行的listview展示出一个图片一个主标题一个内容简介吧,那么我们便可以 开始我们的代码

linSir's blog 589

使用ListActivity

android.app 类 ListActivity java.lang.Object android.content.Context android.content.ContextWrapper android.view.ContextThemeWrapper android.app.Activity

zhxumao的博客 499

继承ListActivity实现ListView

继承ListActivity实现ListView 一、通过继承ListActivity实现ListView ListActivity直接继承了Activity类。以下代码使用Android系统提供的布局文件,所有不需要调用setContentView();就能显示出所需的界面。 Java代码如下所示: package my.list.namespace; import android.a

周宏1990 858

ListActivity 之 ListView 研究

ListActivity显示一个绑定到数组或游标这些数据源的一个列表,并且列表的每一项提供一个点击事件的管理方法,当用户点击其中的列表项的时候就能进行相应的处理。 ListActivity容纳了一个ListView对象,这个对象能够被绑定到不同的数据源,一般是一个数组或者存储了一组查询结果的游标。   屏幕布局 ListActivity的默认布局由一个位于屏幕中心的全屏列表

我的地盘 1036

Android ListActivity和实例分析

1 Activity和ListActivity            简单理解,在安卓中,每一个activity对应于一个显示界面,每一个activity都是独占整个屏幕的;ListActivity类继承Activity类,默认绑定了一个ListView(列表视图)界面组件,并提供一些与列表视图、处理相关的操作。            ListView界面组件中的每一行(row)都可以被选择。

xiaocong1314的专栏 2859
上一篇: Android深入浅出系列之Bluetooth-蓝牙操作(一)
下一篇: Android的onCreateOptionsMenu()创建菜单Menu详解
BeBeMo
博客等级 码龄14年 2粉丝 7原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值