大家都知道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(); } }); } }
.Net程序员玩转Android开发---(13)ListView单击事件
原文地址:http://blog.csdn.net/zx13525079024/article/details/42081785