通过布局文件来显示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,加 ...
随机推荐
- head命令
head 与 tail 就像它的名字一样的浅显易懂,它是用来显示开头或结尾某个数量的文字区块,head 用来显示档案的开头至标准输出中,而 tail 想当然尔就是看档案的结尾. 1.命令格式: hea ...
- 【转】C语言文件操作解析(三)
原文网址:http://www.cnblogs.com/dolphin0520/archive/2011/10/07/2200454.html C语言文件操作解析(三) 在前面已经讨论了文件打开操作, ...
- 2015.9.11模拟赛 codevs 4159【hzwer的迷の数列】
题目描述 Description hzwer找了一个人畜无害的迷の数列…… 现在hzwer希望对这个数列进行一些操作,请你来回答hzwer的问题. 操作一:查询第i个数的大小 操作二:把第i个数的大小 ...
- POJ1657 Distance on chessboard
Distance on Chessboard Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25623 Accepted ...
- Geeks Interview Question: Ugly Numbers
Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence1, 2, 3, 4, 5, 6, 8, 9, ...
- [原创]Python入门到简单网站目录扫描器编写(上)
1.字符串,整型,浮点型.区别以及用法 |------字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串. 可以不严格的说,你可以认为引号包括的,都属于字符串 ...
- Gson 基础教程 —— 自定义类型适配器(TypeAdapter)
1,实现一个类型适配器(TypeAdapter) 自定义类型适配器需要实现两个接口: JsonSerializer<T> JsonDeserializer<T> 和两个方法: ...
- c#序列化反序列化工具(json,binary,xml)
using System; using System.Text; using System.IO; using System.Runtime.Serialization.Formatters.Bina ...
- 【Spring MVC系列】--(5)理解AOP
1.java反射 实用:JAVA反射机制及应用例子 http://407827531.iteye.com/blog/1173930 系统:java反射原理 http://china-jianchen. ...
- JavaScript异步编程 ( 一 )
1. 异步编程 Javascript语言的执行环境是"单线程"(single thread).所谓"单线程",就是指一次只能完成一件任务.如果有多个任务,就必须 ...