.Net程序员玩转Android开发---(13)ListView单击事件

大家都知道ListView用来显示数据列表,每一个列表都有列表项组成,如果我们单击选中一个列表,想获取列表中的详细信息或者打开一个新窗口把列表信息传递过去怎么办那?这一节我们演示一下ListView的单击事件,通过这节我们会对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="vertical" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

列表项布局文件

<?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="100dp"
    android:orientation="vertical"
     android:gravity="top"
    >

    <LinearLayout   android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
     >

<LinearLayout   android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
     >

     <TextView
       android:id="@+id/tvcode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="编码:" />

    <TextView
        android:id="@+id/tvname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="名称:" />

      <TextView
       android:id="@+id/tvprice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"

        android:text="价格:" />

       <TextView
       android:id="@+id/tvmodel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="规格:" />

</LinearLayout>
    </LinearLayout>
  <LinearLayout android:layout_width="fill_parent"   android:layout_height="2dp" android:background="#F0F0F0">
    </LinearLayout>
</LinearLayout>

后台代码

public class ListViewClickActivity extends Activity {

	private ListView lv;
	  SimpleAdapter adp;//定义适配器
	   private List<Map<String,Object>> mapList;//定义数据源  

	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listviewclicklay);
		lv=(ListView)findViewById(R.id.listView1);

		mapList=new ArrayList<Map<String,Object>>();
	    	for(int i=0;i<10;i++)
	    	{

	    		Map<String,Object> map=new HashMap<String,Object>();
	    		map.put("code","编码:1000"+i);
	    		map.put("name","名称:Ipad"+i);
	    		map.put("price","价格:"+i);
	    		map.put("model","单位:"+i);
	    		 mapList.add(map);
	    	}

	    	 adp=new SimpleAdapter(ListViewClickActivity.this, mapList,R.layout.listdetail, new String[]{"code","name","price","model"}, new int[]{R.id.tvcode,R.id.tvname,R.id.tvprice,R.id.tvmodel});
		      lv.setAdapter(adp);  

		      lv.setOnItemClickListener(new OnItemClickListener() {
		            @Override
		            public void onItemClick(AdapterView<?> arg0,View arg1, int arg2,
		                    long arg3) {   

		            	TextView   tname= (TextView)arg1.findViewById(R.id.tvname);//名称
		            	TextView   tmodel= (TextView)arg1.findViewById(R.id.tvmodel);//规格
		            	TextView   tprice= (TextView)arg1.findViewById(R.id.tvprice);//单价
		            	TextView   tcode= (TextView)arg1.findViewById(R.id.tvcode);//编码

		                Toast.makeText(getApplicationContext(),"当前商品 名称:"+tname.getText()+",编码:"+tcode.getText(),30).show();  

		            }
		        });  

	}

}
时间: 2024-08-10 12:45:50

.Net程序员玩转Android开发---(13)ListView单击事件的相关文章

.Net程序员玩转Android开发---(15)ListView滚动事件

Android中的ListView 可以上下滑动,并且上下活动可以分页加载数据,这一节我们看下ListView的滚动事件. ListView的滚动事件主要通过setOnScrollListener监听器来实现,主要包括两个方法onScroll和onScrollStateChanged方法,onScrollStateChanged主要用来监听滚动状态 在滑动过程中向ListView添加数据 ,ListView滚动的时候主要有三个状态,SCROLL_STATE_TOUCH_SCROLL   ,SCR

.Net程序员玩转Android开发---(16)ListView分页事件

ListView在加载是数据的时候,如果一次性把所有数据都加载出来,这样如果数据量大的话,效率低,性能差,通常情况下采取的措施是分页加载,只加载当前页数量的数据.这一节我们演示下ListView怎么分页加载数据.首先看下效果图 1. 创建加载进度栏 ListVIew每次加载的时候,会在底部有一个加载进度条栏,显示加载中,我们创建一个这样的布局文件,代码如下 <?xml version="1.0" encoding="utf-8"?> <Linear

.Net程序员玩转Android开发---(12)ListView显示数据

Android中显示数据有多种控件,这节我们来认识下ListView,ListView是Android中最常用的数据显示控件,可以显示简单数据源,也可以显示复杂数据源,我们在Android系统中常看到的列表项,基本都是ListView的功劳.ListView中显示数据,肯定要绑定数据源.数据源的绑定是通过Adapter来完成的,Android中有两种常用的适配器,ArrayAdapter(数组适配器)  SimpleAdapter(简单适配器),适配器的作用就是把复杂的数据源显示到istview

.Net程序员玩转Android开发---(1)环境搭建

对于没有接触过Android开发的人员来说,可能感觉Android开发比较困难,接下来的一段时间,我们将了解Android开发的具体细节,主要是面对.NET程序员,来看看.NET程序员怎样进行Android开发.  下面我们切入本节正题. 工欲善其事,必先利其器,下面我们准备Android开发的环境搭建,下面是开发的一些工具 1. JDK安装   jdk下载 http://www.oracle.com/technetwork/java/javase/downloads/index.html JD

.Net程序员玩转Android开发---(2)Hello World项目创建

对于程序员来说,刚开始接触到的第一个项目都是Hello World, 我们这里第一个项目也从Hello Word创建. 1. 项目创建 运行eclipse.exe文件,打开开发工具eclipse,选择项目存放位置,界面如下 打开eclipse后,选择 file-new--Android Applicaton Project创建Android项目,界面如下 步骤如下 2.

.Net程序员玩转Android开发---(11)页面跳转

在任何程序开发中,都会遇到页面之间跳转的情况,Android开发也不例外.这一节,我们来认识下Android项目中怎样进行页面跳转.页面跳转分为有参数和无参数页面跳转,已经接受另一个页面的返回值等.Android中页面跳转常用到的是Intent ,但是Intent不仅用做页面跳转,还可以做其他事情,例如拨打电话,发送短信,调用其他程序等.这节我们主要认识下怎样通过Intent进行页面跳转. 1.页面跳转 2.带参数页面跳转

.Net程序员玩转Android开发---(4)注册页面布局

上一篇我们介绍了登陆页面的布局,这一节我们看看注册页面的布局,实际上页面布局大同小异,来一起熟悉下基本控件的用法. 效果图: 1.添加注册页面 右键选中layout文件夹,添加注册页面.如下图 点击完成,页面添加完毕. 在页面中添加控件,XML代码如下 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com

.Net程序员玩转Android开发---(3)登陆页面布局

这一节我们来看看登陆页面怎样布局,对于刚接触到Android开发的童鞋来说,Android的布局感觉比较棘手,需要结合各种属性进行设置,接下来我们由点入面来 了解安卓中页面怎样布局,登陆页面很简单,两个文本框和一个按钮,页面效果如下:

.Net程序员玩转Android开发---(17)Handler用法

在android开发中,如果在一个线程中想更新主界面中控件显示的数据,直接给主界面控件赋值就会出现异常,android中为了安全起见,是不允许在线程中更新界面控件的数据,遇到这种情况,我们可以使用Handler.  Handler就是处理界面和线程间的消息传递,通信的组件.下面我们演示下handler处理详细的两种办法