首页 > 其他 > 详细

父页面与子页面间相互传值

时间:2014-03-11 14:35:41      阅读:530      评论:0      收藏:0      [点我收藏+]

先简单的介绍一下ListActivity

ListActivity是一个专门显示ListView的Activity类,它内置了ListView对象,只要我们设置了数据源,就会自动地显示出来。ListActivity和普通的Activity没有太大的差别,不同就是对显示ListView做了许多优化,方面显示而已。

我们知道

列表的显示需要三个元素:

1.ListVeiw 用来展示列表的View。

2.适配器 用来把数据映射到ListView上的中介。

3.数据    具体的将被映射的字符串,图片,或者基本组件。

根据列表的适配器类型,列表分为三种,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter

simpleAdapter的扩展性最好,可以定义各种各样的布局出来,可以放上ImageView(图片),还可以放上Button(按钮),CheckBox(复选框)等等。

下面的代码都直接继承了ListActivity,采用simpleAdapter

先上两个图:

bubuko.com,布布扣

bubuko.com,布布扣

 

上代码

MoreActivity.java
______________________________________

  1. import java.util.ArrayList;  
  2. import java.util.HashMap;  
  3. import java.util.List;  
  4. import java.util.Map;  
  5. import android.app.AlertDialog;  
  6. import android.app.ListActivity;  
  7. import android.content.DialogInterface;  
  8. import android.os.Bundle;  
  9. import android.view.View;  
  10. import android.widget.ListView;  
  11. import android.widget.SimpleAdapter;  
  12.    
  13. public class MoreActivity extends ListActivity {  
  14.    
  15. private String discount_info;  
  16. private String recommend_shop;  
  17. private String about_us;  
  18.    
  19. @Override  
  20. public void onCreate(Bundle savedInstanceState) {  
  21. super.onCreate(savedInstanceState);  
  22.    
  23. SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.more,  
  24. new String[]{"title","info","img"},  
  25. new int[]{R.id.title,R.id.info,R.id.img});  
  26. setListAdapter(adapter);  
  27.    
  28. }  
  29.    
  30. private List<HashMap> getData() {  
  31. List<HashMap> list = new ArrayList<HashMap>();  
  32.    
  33. Map map = new HashMap();  
  34.    
  35. discount_info=this.getString(R.string.discount_info);//相应字符串在String.xml中设置  
  36. recommend_shop=this.getString(R.string.recommend_shop);  
  37. about_us=this.getString(R.string.about_us);  
  38.    
  39. map.put("title", discount_info);  
  40. map.put("info""android 1");  
  41. map.put("img", R.drawable.i1);  
  42. list.add(map);  
  43.    
  44. map = new HashMap();  
  45. map.put("title", recommend_shop);  
  46. map.put("info""android 2");  
  47. map.put("img", R.drawable.i2);  
  48. list.add(map);  
  49.    
  50. map = new HashMap();  
  51. map.put("title", about_us);  
  52. map.put("info""android 3");  
  53. map.put("img", R.drawable.i3);  
  54. list.add(map);  
  55.    
  56. return list;  
  57. }  
  58.    
  59. @Override  
  60. protected void onListItemClick(ListView l, View v, int position, long id) {  
  61. // TODO Auto-generated method stub  
  62. super.onListItemClick(l, v, position, id);  
  63. if(0==id)  
  64. {  
  65. showInfo();//点击第一项执行函数,其它id可以自己设置执行函数  
  66. }  
  67.    
  68. }  
  69.    
  70. //定义启动Dialog  
  71.    
  72. public void showInfo(){  
  73. new AlertDialog.Builder(this)  
  74. .setTitle("title")  
  75. .setMessage("bla..bla")  
  76. .setPositiveButton("OK"new DialogInterface.OnClickListener() {  
  77. @Override  
  78. public void onClick(DialogInterface dialog, int which) {  
  79. }  
  80. })  
  81. .show();  
  82.    
  83. }  
  84. }  


more.xml

______________________________________________

 

  1. <?xml version=”1.0″ encoding=”utf-8″?>  
  2. <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”  
  3.  android:orientation=”horizontal”  
  4.  android:layout_width=”fill_parent”  
  5.  android:layout_height=”fill_parent”>  
  6.  <ImageView  
  7.   android:id=”@+id/img”  
  8.   android:layout_width=”wrap_content”  
  9.   android:layout_height=”wrap_content”  
  10.   android:layout_margin=”5px”/>  
  11.    
  12.  <LinearLayout  
  13.   android:orientation=”vertical”  
  14.   android:layout_width=”wrap_content”  
  15.   android:layout_height=”wrap_content”>  
  16.    
  17.   <TextView  
  18.    android:id=”@+id/title”  
  19.    android:layout_width=”wrap_content”  
  20.    android:layout_height=”wrap_content”  
  21.    android:textColor=”#000000″  
  22.    android:textSize=”22px” />  
  23.   <TextView  
  24.    android:id=”@+id/info”  
  25.    android:layout_width=”wrap_content”  
  26.    android:layout_height=”wrap_content”  
  27.    android:textColor=”#000000″  
  28.    android:textSize=”13px” />  
  29.    
  30.  </LinearLayout>  
  31. </LinearLayout>  

 

 

参考文章:

http://www.cnblogs.com/allin/archive/2010/05/11/1732200.html

父页面与子页面间相互传值,布布扣,bubuko.com

父页面与子页面间相互传值

原文:http://www.cnblogs.com/thinksasa/p/3582152.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!