通过布局文件来显示ListView内容并注册 ListView事件
1:layout/vlist.xml是我们的布局文件,在这里一定要对首节点加上
android:descendantFocusability="blocksDescendants" 这一属性,如果不加这一属性那么ListView就会获取不到点击事件。
<?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="horizontal"
android:descendantFocusability="blocksDescendants" >
<ImageView
android:id="@+id/imgNf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/titleNf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textIsSelectable="true"/> </LinearLayout>
2:activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"> <ListView
android:id="@+id/lvList"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView> </RelativeLayout>
3:MainActivity.java
public class MainActivity extends Activity {
private List<Map<String,Object>>list=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv=(ListView)findViewById(R.id.lvList);
lv.setAdapter(initAdapter());
//注册点击事件
lv.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String title=list.get(arg2).get("title").toString();
Toast.makeText(MainActivity.this, title, Toast.LENGTH_SHORT).show();
}
});
}
public SimpleAdapter initAdapter(){
SimpleAdapter simpleAdapter=new SimpleAdapter(
MainActivity.this,
getData(),
R.layout.vlist,
new String[]{"title","img"},
new int[]{R.id.titleNf,R.id.imgNf});
return simpleAdapter;
}
private List<Map<String,Object>>getData(){
list=new ArrayList<Map<String,Object>>();
Map<String,Object>map=new HashMap<String,Object>();
map.put("title", "This is a test 1");
map.put("img",R.drawable.ic_launcher);
list.add(map);
map=new HashMap<String,Object>();
map.put("title", "This is a test 2");
map.put("img",R.drawable.ic_launcher);
list.add(map);
return list;
}
}
4:效果

点击第二行数据后:

通过布局文件来显示ListView内容并注册 ListView事件的更多相关文章
- pycharm设置文件中显示模板内容
pycharm中设置自己的文件模板 File>>Settings>>Editor>>File and Code Templates 选择文件类型或者输入文件类型 ...
- ifrem上传文件后显示
ifrem上传文件后显示 1.上传文件按钮 <a class="btn btn-primary pull-right" id="data-upload&quo ...
- Android学习---ListView和Inflater的使用,将一个布局文件转化为一个对象
本文将介绍ListView和Inflater的使用,将接上一篇文章内容. 一.什么是ListView? 在android开发中ListView是比较常用的控件,ListView 控件可使用四种不同视图 ...
- 自定义ListView适配器Adapter引用布局文件的情况下实现点击列表项时背景颜色为灰色
listview控件设置适配器的时候,如果使用自定义的adapter,比如MyArrayAdapter extends ArrayAdapter<String> 如果listitem布局文 ...
- Android如何在一个线性布局里完美显示两个listview啊?
复写一个listView ,在你布局文件中使用此view: <ScrollView android:layout_width="fill_parent" android:la ...
- 安卓---achartengine图表----简单调用----使用view显示在自己的布局文件中----actionBar的简单设置
AChartEngine 是一个安卓系统上制作图表的框架,关于它的介绍大家可以百度,也可以参考下面这篇博客http://blog.csdn.net/lk_blog/article/details/76 ...
- 编程检查d:\test.txt文件是否存在,若在则显示该文件的名称和内容。
下面放两种方法 老师写的: import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundExcept ...
- 【转】 C# ListView实例:文件图标显示
[转] C# ListView实例:文件图标显示 说明:本例将目录中的文件显示在窗体的ListView控件中,并定义了多种视图浏览.通过调用Win32库函数实现图标数据的提取. 主程序: 大图标: 列 ...
- androidstudio新建项目中在布局文件中不显示title的方法
在androidstudio新建项目的时候,在布局文件里有时候会出现如下情况: 上面的标题栏非常碍眼,要想隐藏标题栏的话,可以在Manifest文件的theme标签里进行配置,自定义一个theme,加 ...
随机推荐
- B450配置
电脑型号 联想 B450 笔记本电脑 (扫描时间:2015?5?7?操作系统 Windows 7 旗舰版 32位 SP1 ( DirectX 11 ) 处理器 英特尔 酷睿2 双核 T9800 @ ...
- HDU_1010——小狗走迷宫DFS
Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...
- windows 自动安装
msiexec
- java基础知识(一)
基本特征:封装性,继承性,多态性 一些新特征: 静态导入:import static 包名 可变参数的函数:add(int -x) 增强版for循环: for(int x:xs) 自动拆箱: 基本类型 ...
- 关于Spring配置 (Cannot find class [org.apache.commons.dbcp.BasicDataSource] 问题)
myeclipse抛出异常 org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find /webapps/t ...
- javascript 中的location.reload
location.reload()是什么意思 location.reload() 括号内有一个参数 true/false , 为空和false的效果一样. 如果该方法没有规定参数,或者参数是 fals ...
- Linux下Weblogic域的创建过程
环境介绍:操作系统 :Redhat 5.5Weblogic :英文版 8.1.6 Weblogic安装目录 :/weblogic 一.域的建立执行下面语句进入weblogic的bin目录: cd /w ...
- 手游2dx面试笔记一
第一轮IQ测试:都来面试程序了,相信IQ再怎么也坑不到哪里去吧.要问什么样的题,几页纸呐, 如:1.找出不同类:羚羊.斑马.鲨鱼 2.在()里添一字使2边都能组词:木()料 3.中间值?:1,2,4, ...
- datagrid数据导出到excel文件给客户端下载的几种方法
方法一:导出到csv文件,存放在服务器端任一路径,然后给客户下载 优点: 1.可以进行身份认证后给客户下载,如果放到非web目录就没有对应的url,客户无法随时下载. 2.也是因为生成了文件,所以占用 ...
- js库编写的环境和准备工作
在编写一个js库之前需要准备许多基础知识. 本人在编写之前主要参考了visjs和百度的echarts这两个库的部分内容,这之中提取出几个要点如下: 1.如何整理代码结构,使模块与模块分开又不至于出现调 ...