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及每个控件的点击事件的更多相关文章

  1. Android 使用代码主动去调用控件的点击事件(模拟人手去触摸控件)

    使用代码主动去调用控件的点击事件(模拟人手去触摸控件) //View 可以是LinearLayout,Button,TextView View.performClick();

  2. Android监听Button和ImageButton控件的点击事件

    一.onClick事件 Button和ImageButton都有一个onClick事件,通过自身的.setOnClickListener(OnClickListener)方法添加点击事件 所有的控件都 ...

  3. android中RecyclerView控件实现点击事件

    RecyclerView控件实现点击事件跟ListView控件不同,并没有提供类似setOnItemClickListener()这样的注册监听器方法,而是需要自己给子项具体的注册点击事件. 本文的例 ...

  4. 【MAUI】为 Label、Image 等控件添加点击事件

    一.前言 已经习惯了 WPF.WinForm 中"万物皆可点击"的方式. 但是在 MAUI 中却不行了. 在 MAUI 中,点击.双击的效果,是需要通过"手势识别器&qu ...

  5. Android技能杂谈——如何优雅的处理控件的点击事件

    首先分享一个最佳实践: 大家开发复杂一些的Android应用的时候常常会写一个BaseActivity或者BaseFragment,里面写上一些基本而公用的方法,比如监听网络状况.弹出对话框.设置ac ...

  6. Button控件的点击事件

    Java的代码 主要重点: findViewById(); OnClickListener() package com.example.admin.myapplication; import andr ...

  7. 继承UIView的子控件添加点击事件

    UITapGestureRecognizer*tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:selfaction:@select ...

  8. listview适配器中的控件的点击事件并传值

    @Override public View getView(final int position, View convertView, ViewGroup parent) { // TODO Auto ...

  9. ios 给移动的控件添加点击事件

    前言: 给一个UIView做移动动画,虽然看起来frame在持续改变,但是它的frame已经是最终值了. 也就是说表面看到的动画都是假象,它的真实位置已经是固定的了.所以只有点击在他的真实frame范 ...

随机推荐

  1. C#基础整理参数

    基本概念 把数据传入方法中,可以使方法有多个返回值. 参数的传递 值参数,通过将实参的值复制到形参的方式传递数据.值参数的实参可以是变量或者是表达式

  2. 交换排序---快速排序算法(Javascript版)

    快速排序是对冒泡排序的一种改进.通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行 ...

  3. MySQL 复制介绍及搭建

    MySQL复制介绍 MySQL复制就是一台MySQL服务器(slave)从另一台MySQL服务器(master)进行日志的复制然后再解析日志并应用到自身,类似Oracle中的Data Guard. M ...

  4. WinForm 简单蒙版实现控件遮盖

    在Web上面要实现一个遮罩层或者说是蒙版吧,有了DIV那不算什么难事,只要给div定好位置和大小,把颜色的Alpha值设一下就有透明的效果.不过在Winform中实现起来就没那么简单了事.尝试过用一个 ...

  5. vs2012运行项目报未能加载文件或程序集“System.Web.Mvc, Version=4.0.0.1,Culture=neutral”问题和解决方法

    原先本地项目版本(4.0.0.1)高于服务器版本(4.0.0.0),本地项目改成服务器版本4.0.0.0时,发布后的项目报这个错误

  6. jquery简单原则器(匹配索引为指定值的元素)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. hibernate简单注释(一.1)

    **************************************************************************************************** ...

  8. python mysql Connect Pool mysql连接池 (201

     easy_install mysql-connector-python >>>import mysql.connector as conner >>> conn ...

  9. How to return dictionary keys as a list in Python 3.3

    http://btmiller.com/2015/04/13/get-list-of-keys-from-dictionary-in-python-2-and-3.html Get a List of ...

  10. Loadrunner中web_find和web_reg_find函数的使用与区别

    总结一下Loadrunner中的检查点函数,主要介绍两个函数:web_find()和web_reg_find():这两个函数均用于内容的查找,但两者也有本质的区别,具体介绍如下:一.web_find( ...