android ListView添加事件并获取选中项的值
2015-11-16 08:44
615 查看
android ListView添加事件并获取选中项的值,ListView是一个经常用到的控件,ListView里面的每个子项Item可以使一个字符串,也可以是一个组合控件。
main.xml代码如下:点击(此处)折叠或打开
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="@+id/myListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
list_item.xml代码如下:
点击(此处)折叠或打开
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/itemTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22dip"
android:paddingRight="12dip"
/>
<TextView
android:id="@+id/itemContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22dip"
/>
</LinearLayout>
activity MyListView.java代码如下:
package listview.pack;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MyListView extends Activity {
/** Called when the activity is first created. */
//声明ListView对象
ListView myListView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//生成ListView对象
myListView=(ListView)findViewById(R.id.myListView);
//创建ArrayList对象 并添加数据
ArrayList<HashMap<String,String>> myArrayList=new ArrayList<HashMap<String,String>>();
for(int i=0;i<10;i++){
HashMap<String, String> map = new HashMap<String, String>();
map.put("itemTitle", "This Is Title "+i);
map.put("itemContent", "This Is Content "+i);
myArrayList.add(map);
}
//生成SimpleAdapter适配器对象
SimpleAdapter mySimpleAdapter=new SimpleAdapter(this,
myArrayList,//数据源
R.layout.list_items,//ListView内部数据展示形式的布局文件listitem.xml
new String[]{"itemTitle","itemContent"},//HashMap中的两个key值 itemTitle和itemContent
new int[]{R.id.itemTitle,R.id.itemContent});/*布局文件listitem.xml中组件的id
布局文件的各组件分别映射到HashMap的各元素上,完成适配*/
myListView.setAdapter(mySimpleAdapter);
//添加点击事件
myListView.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
//获得选中项的HashMap对象
HashMap<String,String> map=(HashMap<String,String>)myListView.getItemAtPosition(arg2);
String title=map.get("itemTitle");
String content=map.get("itemContent");
Toast.makeText(getApplicationContext(),
"你选择了第"+arg2+"个Item,itemTitle的值是:"+title+"itemContent的值是:"+content,
Toast.LENGTH_SHORT).show();
}
});
}
}
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(2902) | 评论(0) | 转发(0) |
1
上一篇:sqllite数据库查询方法中需要注意的 模糊查询问题
下一篇:JAVASE中JFileChooser的使用小结 打开文件并显示
相关热门文章
欢迎androidbegin在ChinaUnix...
如何修改android aapt源码实现...
Android常用布局介绍
lesson4-Qt窗口
Activity
Android之开发环境搭建
Android自定义View的实现...
AndroidManifest.xml配置文件...
查看Android应用包名package和...
Android相对布局+圆角按钮+Sha...
linux dhcp peizhi roc
关于Unix文件的软链接
求教这个命令什么意思,我是新...
sed -e "/grep/d" 是什么意思...
谁能够帮我解决LINUX 2.6 10...
给主人留下些什么吧!~~
评论热议
相关文章推荐
- Android Studio查看SHA1和MD5(附带keystore创建)
- Android中的颜色设置 8位的十六进制
- android获取设备唯一标识符
- Android实战源码--围住神经猫
- Android动画效果translate、scale、alpha、rotate详解
- Android using Accelerometer
- Notification相关和一些关键点收集
- Android笔记(五十九)Android总结:四大组件——Service篇
- Android项目开发二
- 深入理解Android消息处理系统——Looper、Handler、Thread
- Android项目开发第二周
- Android Studio常用快捷键
- Android实践项目汇报(二)
- android开发步步为营之83:android图片处理技术之二(通过Matrix移动缩放旋转错切)
- Android开发之带CheckBox的ListView的点击事件的处理
- Android开发之ListView开发中view对象的复用问题2
- Android之锁屏广播事件的使用
- android之相机开发
- RecyclerView加载不同view实现效果--IT蓝豹
- AndroidEventBus