Android简单的ListViewDemo及每个控件的点击事件
ListView是什么?
ListView是一个 数据控件,可以展示从数据库中读取的数据。是.net3.5的新控件。
它比gridview更灵活,而且支持多种模板,支持分页。
文章地址 http://www.cnblogs.com/mmyblogs/p/6058322.html(转载请保留)
接下来我就为大家演示一个简单的利用SimpleAdapter实现ListView及其点击事件的例子。
main.xml主页面布局
<?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="match_parent"
android:orientation="vertical"
android:gravity="center"> <ListView
android:id="@+id/lv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
list_item.xml每个item的排布
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:orientation="horizontal"> <LinearLayout
android:layout_width="60dp"
android:layout_height="60dp"> <ImageView
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/> </LinearLayout> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="60dp"
android:orientation="vertical"
android:gravity="center"> <TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"/> <TextView
android:id="@+id/context"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15sp"/> </LinearLayout> </LinearLayout>
MainActivity.java核心代码
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import android.widget.SimpleAdapter;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.Adapter;
import android.widget.Toast; public class MainActivity extends Activity
{ @Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main); List<Map<String,Object>>datalist=new ArrayList<Map<String,Object>>();
ListView lv=(ListView) findViewById(R.id.lv); Map<String,Object> map1=new HashMap<String,Object>();
map1.put("image",R.mipmap.ic_launcher);
map1.put("title","AIDE资源");
map1.put("context","AIDE 资源 分享");
Map<String,Object> map2=new HashMap<String,Object>();
map2.put("image",R.mipmap.ic_launcher);
map2.put("title","了解AIDE");
map2.put("context","AIDE 了解 分享");
Map<String,Object> map3=new HashMap<String,Object>();
map3.put("image",R.mipmap.ic_launcher);
map3.put("title","玩转AIDE");
map3.put("context","AIDE 玩转 分享");
Map<String,Object> map4=new HashMap<String,Object>();
map4.put("image",R.mipmap.ic_launcher);
map4.put("title","控件之TextView");
map4.put("context","AIDE TextView 分享"); datalist.add(map1);
datalist.add(map2);
datalist.add(map3);
datalist.add(map4); lv.setAdapter(new SimpleAdapter(this,datalist,R.layout.list_item,new String[]{"image","title","context"},new int[]{R.id.image,R.id.title,R.id.context}));
lv.setOnItemClickListener(new OnItemClickListener(){
//list点击事件
@Override
public void onItemClick(AdapterView<?> p1, View p2, int p3, long p4)
{
// TODO: Implement this method
switch(p3){
case 0://第一个item
Toast.makeText(MainActivity.this,"AIDE 资源 分享",Toast.LENGTH_SHORT).show();
break;
case 1://第二个item
Toast.makeText(MainActivity.this,"AIDE 了解 分享",Toast.LENGTH_SHORT).show();
break;
case 2://第三个item
Toast.makeText(MainActivity.this,"AIDE 玩转 分享",Toast.LENGTH_SHORT).show();
break;
}
} }); }
}
以上就是全部代码,下面是效果图



Android简单的ListViewDemo及每个控件的点击事件的更多相关文章
- Android 使用代码主动去调用控件的点击事件(模拟人手去触摸控件)
使用代码主动去调用控件的点击事件(模拟人手去触摸控件) //View 可以是LinearLayout,Button,TextView View.performClick();
- Android监听Button和ImageButton控件的点击事件
一.onClick事件 Button和ImageButton都有一个onClick事件,通过自身的.setOnClickListener(OnClickListener)方法添加点击事件 所有的控件都 ...
- android中RecyclerView控件实现点击事件
RecyclerView控件实现点击事件跟ListView控件不同,并没有提供类似setOnItemClickListener()这样的注册监听器方法,而是需要自己给子项具体的注册点击事件. 本文的例 ...
- 【MAUI】为 Label、Image 等控件添加点击事件
一.前言 已经习惯了 WPF.WinForm 中"万物皆可点击"的方式. 但是在 MAUI 中却不行了. 在 MAUI 中,点击.双击的效果,是需要通过"手势识别器&qu ...
- Android技能杂谈——如何优雅的处理控件的点击事件
首先分享一个最佳实践: 大家开发复杂一些的Android应用的时候常常会写一个BaseActivity或者BaseFragment,里面写上一些基本而公用的方法,比如监听网络状况.弹出对话框.设置ac ...
- Button控件的点击事件
Java的代码 主要重点: findViewById(); OnClickListener() package com.example.admin.myapplication; import andr ...
- 继承UIView的子控件添加点击事件
UITapGestureRecognizer*tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:selfaction:@select ...
- listview适配器中的控件的点击事件并传值
@Override public View getView(final int position, View convertView, ViewGroup parent) { // TODO Auto ...
- ios 给移动的控件添加点击事件
前言: 给一个UIView做移动动画,虽然看起来frame在持续改变,但是它的frame已经是最终值了. 也就是说表面看到的动画都是假象,它的真实位置已经是固定的了.所以只有点击在他的真实frame范 ...
随机推荐
- msvc2013编译qt5.6源码
1.回顾 说起到qt的编译,真是领人痛心啊,不仅编译选项繁多,而且编译时间比较久,总是能使想编译qt源码的人望而却步,呵呵...我就是其中一个,不知道从什么时候开始就想着把qt的源码编译一下,也尝试过 ...
- JS魔法堂:再识ASCII实体、符号实体和字符实体
一.前言 相信大家都熟悉通过字符实体 来实现多个连续空格的输入吧!本文打算对三类HTML实体及JS相关操作作进一步的整理和小结,若有纰漏请大家指正,谢谢. 二.初识HTML实 ...
- 博客已经迁移至http://www.xuechenlei.com
博客园是个非常好的公共平台,这里总有我想找的东西,本着人生在于折腾的精神和一些个性化的定制,将博客内容迁移到www.xuechenlei.com,时而同步分享到这里,欢迎访问关注:
- Socket.Available 属性
获取已经从网络接收且可供读取的数据量. 命名空间: System.Net.Sockets程序集: System(System.dll 中) 从网络接收的.可供读取的数据的字节数. 异常 Ex ...
- [Asp.net 5] Configuration-新一代的配置文件(接口定义与基础实现)
关于配置文件的目录:[Asp.net 5] Configuration-新一代的配置文件 本系列文章讲的是asp.net 5(Asp.net VNext)中的配置文件部分,工程下载地址为:https: ...
- Handler "BlockViewHandler" has a bad module "ManagedPipelineHandler" in its module list
当你的ASP.NET MVC项跑在IIS时,出现如标题Handler "BlockViewHandler" has a bad module "ManagedPipeli ...
- ASP.NET里的Session详细解释
Session模型简介 Session是什么呢?简单来说就是服务器给客户端的一个编号.当一台WWW服务器运行时,可能有若干个用户浏览正在运正在这台服务器上的网站.当每个用户首次与这台WWW服务器建立连 ...
- Java继承知识总结
Java中的继承作为Java面向对象三大特性之一,在知识点上而言,还是很多的.现做出如下总结: 1. 继承的概念: 继承在本职上是特殊——一般的关系,即常说的is-a关系.子类继承父类,表明子类是一种 ...
- 修正 ColorPanel 选色缓慢问题
问题:TColorPanel 在运行时,选取颜色都会重绘,造成选色缓慢. 适用:Delphi XE5 修正:找出 FMX.Colors.pas 档案,并复制到自己的 Project 路径里,找到 TC ...
- WebView的使用及添加进度条
实现的效果比较简单类似于微信打开网页,头部有个进度条显示加载进度 下载地址:http://download.csdn.net/detail/qq_29774291/9666941 1.在安卓端加载一个 ...