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范 ...
随机推荐
- 第20/24周 死锁(Deadlocking)
大家好,欢迎回到性能调优培训.今天讨论SQL Server里的死锁(Deadlocking),第5个月的培训就结束了.当2个查询彼此等待,没有查询可以继续它的工作就会发生死锁.第一步我会概括介绍下SQ ...
- 导出Excel文件(针对Dev)
文件类: using System;using System.Collections.Generic;using System.Linq;using System.Text;using System. ...
- lua编译出so文件
把lua编译出so文件 http://superuser.com/questions/557884/getting-error-recompile-with-fpic http://guiquanz. ...
- HDU 5091---Beam Cannon(线段树+扫描线)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5091 Problem Description Recently, the γ galaxies bro ...
- 框架Hibernate笔记系列 基础Session
标题:框架Hibernate笔记 资料地址: 1. www.icoolxue.com 孔浩 1.背景简介 Hibenate是JBoss公司的产品.它是数据持久化的框架.Usually,我们使用JDBC ...
- Linux命令速查手册,超详细Linux命令教程
一.常用命令速查 ls cd pwd cat more less tail head cp scp mv mkdir rmdir touch rm ps kill top free clear tre ...
- 形象化的spring 依赖注入原理
转. IoC就是Inversion of Control,控制反转.在Java开发中,IoC意味着将你设计好的类交给系统去控制,而不是在你的类内部控制.这称为控制反转. 下面我们以几个例子来说明什 ...
- 两个实用的方法从Base64字符串生成RSAPublicKey及RSAPrivatekey
public static RSAPublicKey getRSAPublidKeyBybase64(String base64s) { X509EncodedKeySpec keySpec = ne ...
- IOS 非常流畅的滑动tableView
为什么要写这篇文章呢?之前写过一篇,因为手机打字不是很方便,还有之前同事用6splus 定下午茶时候,我滑动列表时候竟然误以为是安卓系统的手机. tableview 流畅度可以用fps来测试,到6 ...
- C# 中几个小“陷阱”
每天写代码,偶尔就会有让你抓狂的时候:代码改了千百遍,蓦然回首,Bug就在灯火阑珊处……这里就列举一些容易犯错的几个小地方,以后遇到了其他的,再慢慢添加. 1. 获取程序当前运行路径 情景复现 ...