天气预报--保存已查询城市,历史记录功能实现

保存功能,结合android我只能想到一个最适合的方法,数据库,在上次的查询城市中我也操作了sqlite,db.exec的那几条数据库语句要记住。做这个功能之前我首先想的如何将数据以什么样的形式表现出来,要出现的内容是什么,历史记录的表中要保存什么内容,最后我选择了用一个ListView来显示数据,显示城市名字,温度,天气图标,还有温馨提示,其中最重要的是ListView的操作。

ListView:

和上一个功能一样,要实现数据与控件的通信,同样要用到Adapter,一个完整的ListView需要两个XML文件,一个声明了Listview控件的XML和一个定义Listview中每个Item控件样式的XML文件,后者类似于Spinner的下拉菜单,我就不贴上XML文件了,贴上后者的XML内容:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="vertical"   
  7.     >  
  8.     <LinearLayout   
  9.         android:orientation="horizontal"   
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         >  
  13.           
  14.             <TextView  
  15.                 android:id="@+id/_area"  
  16.                 android:layout_width="wrap_content"  
  17.                 android:layout_height="wrap_content"  
  18.                 android:text="@string/hello_world"  
  19.                 android:textColor="#000000"  
  20.                 android:layout_marginRight="100dp"  
  21.              />  
  22.             <TextView   
  23.                 android:id="@+id/_temperature"  
  24.                 android:layout_width="wrap_content"  
  25.                 android:layout_height="wrap_content"   
  26.                 android:text="@string/hello_world"  
  27.                 android:textColor="#000000"  
  28.                 android:layout_marginBottom="5dp"  
  29.   
  30.              />  
  31.             <ImageView   
  32.                 android:id="@+id/_image"  
  33.                 android:layout_width="wrap_content"  
  34.                 android:layout_height="wrap_content"  
  35.                   android:layout_marginLeft="50dp"  
  36.                 />  
  37.      </LinearLayout>  
  38.      
  39.     <TextView   
  40.         android:id="@+id/_dex"  
  41.         android:layout_width="wrap_content"  
  42.         android:layout_height="wrap_content"   
  43.         android:text="@string/hello_world"  
  44.         android:textColor="#000000"/>  
  45. </LinearLayout>  
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    >
	<LinearLayout 
	    android:orientation="horizontal" 
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    >
	    
		    <TextView
		        android:id="@+id/_area"
		        android:layout_width="wrap_content"
		        android:layout_height="wrap_content"
		        android:text="@string/hello_world"
		        android:textColor="#000000"
		        android:layout_marginRight="100dp"
	         />
	        <TextView 
		        android:id="@+id/_temperature"
		        android:layout_width="wrap_content"
		        android:layout_height="wrap_content" 
		        android:text="@string/hello_world"
		        android:textColor="#000000"
		        android:layout_marginBottom="5dp"

		     />
	        <ImageView 
	            android:id="@+id/_image"
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content"
	              android:layout_marginLeft="50dp"
	            />
	 </LinearLayout>
   
    <TextView 
        android:id="@+id/_dex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="@string/hello_world"
        android:textColor="#000000"/>
</LinearLayout>

如何将图片和文字一起封装起来,我想到了List对象,用了ArrayList来实现貌似不太可能,因为有图片,查了一下类似的博客发现我面向对象的思想还不太成熟,List集合中的东西可以是任何对象,于是可以用Map<String,Object>来存放数据。Map类 :以按键/键值对的方式储存数据,和数组非常相似。左边的String为键名,右边的是键值,是一个Object对象,这样图片也能存放进去了:

  1. List<Map<String, Object>> listItemsList=new ArrayList<Map<String,Object>>();  
List<Map<String, Object>> listItemsList=new ArrayList<Map<String,Object>>();
接下来是Adapter的问题,我用的是SimpleAdapter,这是个简单的适配器,如果需要Listview做更复杂的工作需要重写BaseAdapter,这是一切Adapter的父类,重写它的getView方法。接下来就是SimpleAdapter的构造方法了:

public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
参数:context:SimpleAdapter关联的View的运行环境(Activity的上下文对象)

     data:一个Map组成的List。在列表中被个条目对应着列表中的一行,每一个Map中应该包含所有在from参数中指定的键。

     resource:一个定义列表项的布局文件的资源ID,它至少要包含那些在to中定义的id(也就是说这个布局文件是Listview每个Item的布局文件)

     from:一个将被添加到Map映射上的键名。

     to:将要绑定数据的视图Id,跟from参数一一对应。

我的SimpleAdapter:

  1. SimpleAdapter adapter=new SimpleAdapter(this, listItemsList, R.layout.listview,  
  2.                 new String[]{"_area","_temperature","_dex","_image"}, //Map中的键名称  
  3.                 new int[]{R.id._area,R.id._temperature,R.id._dex,R.id._image});//listview组件的资源ID  
SimpleAdapter adapter=new SimpleAdapter(this, listItemsList, R.layout.listview,
        		new String[]{"_area","_temperature","_dex","_image"}, //Map中的键名称
        		new int[]{R.id._area,R.id._temperature,R.id._dex,R.id._image});//listview组件的资源ID
知道了Adapter后,接下来是绑定数据的问题了,绑定的数据源是从一个SaveCity表中得到的:

  1. db.execSQL("CREATE TABLE IF NOT EXISTS SaveCity (_id integer primary key autoincrement,area text,cityid integer,"  
  2.                 + "date text,week text,temperature text,weather text,dex text,search text,tomorrow_weather text,after_tomorrow_weather text)");  
db.execSQL("CREATE TABLE IF NOT EXISTS SaveCity (_id integer primary key autoincrement,area text,cityid integer,"
				+ "date text,week text,temperature text,weather text,dex text,search text,tomorrow_weather text,after_tomorrow_weather text)");
可见我的Listview只要显示4项值,而这张表中保存了很多信息,这是为了获得更多的值,因为我要使我保存的所有城市都可以显示在MainInterface这个主界面上,主界面上所的控件所需要的信息我都保存在了这张表里,接下来我的List就要从这张表中取得我要的信息了,这里要运用一个Cursor游标类。

  1. cursor=db.rawQuery("select  * from SaveCity" , null);  
cursor=db.rawQuery("select  * from SaveCity" , null);
  1. while(cursor.moveToNext()){  
  2. <span style="white-space:pre">            </span>Map<String,Object> map=new HashMap<String, Object>();  
  3. <span style="white-space:pre">            </span>map.put("_area", cursor.getString(1));//数据库中第一列的内容显示在listview的左边  
  4. <span style="white-space:pre">            </span>map.put("_temperature", cursor.getString(5));//数据库中第一列的内容显示在listview的左边  
  5. <span style="white-space:pre">            </span>map.put("_dex", cursor.getString(7));  
  6. <span style="white-space:pre">            </span>if(cursor.getString(6).indexOf("晴") != -1){  
  7. <span style="white-space:pre">                </span>map.put("_image", R.drawable.ww0);  
  8. <span style="white-space:pre">            </span>}  
  9. <span style="white-space:pre">            </span>if(cursor.getString(6).indexOf("阴") != -1){  
  10. <span style="white-space:pre">                </span>map.put("_image", R.drawable.ww2);  
  11. <span style="white-space:pre">            </span>}  
  12. <span style="white-space:pre">            </span>if(cursor.getString(6).indexOf("云") != -1){  
  13. <span style="white-space:pre">                </span>map.put("_image", R.drawable.ww2);  
  14. <span style="white-space:pre">            </span>}<span style="white-space:pre">  </span>  
  15. <span style="white-space:pre">            </span>if(cursor.getString(6).indexOf("雨") != -1){  
  16. <span style="white-space:pre">                </span>map.put("_image", R.drawable.ww9);  
  17. <span style="white-space:pre">            </span>}<span style="white-space:pre">  </span>  
  18. <span style="white-space:pre">            </span>if(cursor.getString(6).indexOf("雪") != -1){  
  19. <span style="white-space:pre">                </span>map.put("_image", R.drawable.ww17);  
  20. <span style="white-space:pre">            </span>}<span style="white-space:pre">  </span>  
  21. <span style="white-space:pre">            </span>if(cursor.getString(6).indexOf("雷阵雨") != -1){  
  22. <span style="white-space:pre">                </span>map.put("_image", R.drawable.ww29);  
  23. <span style="white-space:pre">            </span>}  
  24. <span style="white-space:pre">            </span>listItemsList.add(map);  
  25. <span style="white-space:pre">        </span>}  
while(cursor.moveToNext()){
<span style="white-space:pre">			</span>Map<String,Object> map=new HashMap<String, Object>();
<span style="white-space:pre">			</span>map.put("_area", cursor.getString(1));//数据库中第一列的内容显示在listview的左边
<span style="white-space:pre">			</span>map.put("_temperature", cursor.getString(5));//数据库中第一列的内容显示在listview的左边
<span style="white-space:pre">			</span>map.put("_dex", cursor.getString(7));
<span style="white-space:pre">			</span>if(cursor.getString(6).indexOf("晴") != -1){
<span style="white-space:pre">				</span>map.put("_image", R.drawable.ww0);
<span style="white-space:pre">			</span>}
<span style="white-space:pre">			</span>if(cursor.getString(6).indexOf("阴") != -1){
<span style="white-space:pre">				</span>map.put("_image", R.drawable.ww2);
<span style="white-space:pre">			</span>}
<span style="white-space:pre">			</span>if(cursor.getString(6).indexOf("云") != -1){
<span style="white-space:pre">				</span>map.put("_image", R.drawable.ww2);
<span style="white-space:pre">			</span>}<span style="white-space:pre">	</span>
<span style="white-space:pre">			</span>if(cursor.getString(6).indexOf("雨") != -1){
<span style="white-space:pre">				</span>map.put("_image", R.drawable.ww9);
<span style="white-space:pre">			</span>}<span style="white-space:pre">	</span>
<span style="white-space:pre">			</span>if(cursor.getString(6).indexOf("雪") != -1){
<span style="white-space:pre">				</span>map.put("_image", R.drawable.ww17);
<span style="white-space:pre">			</span>}<span style="white-space:pre">	</span>
<span style="white-space:pre">			</span>if(cursor.getString(6).indexOf("雷阵雨") != -1){
<span style="white-space:pre">				</span>map.put("_image", R.drawable.ww29);
<span style="white-space:pre">			</span>}
<span style="white-space:pre">			</span>listItemsList.add(map);
<span style="white-space:pre">		</span>}

让cursor游标从第一行走到最后一行,将每一行保存的内容封装在Map对象中,最后add进ArrayList中。

最后让Listview绑定这个adapter:

  1. listView.setAdapter(adapter);  
listView.setAdapter(adapter);
这样listview的显示数据就完成了,最后需要在Listview增加操作,最简单的就是点击每个Item后会有更新或者删除的功能,类似于Button,listview也有一个setOnItemClickListener方法来监听Item按下事件。

  1. AlertDialog.Builder builder = new AlertDialog.Builder(History.this);  
  2.                 builder.setMessage(areaChoose.getText().toString())  
  3.                     .setCancelable(true)  
  4.                     .setPositiveButton(R.string.update, new DialogInterface.OnClickListener(){  
  5.                          public void onClick(DialogInterface dialog, int id) {  
  6.                              /*********更新操作******************/  
  7.                                
  8.                          }  
  9.                     })  
  10.                     .setNegativeButton(R.string.delete, new DialogInterface.OnClickListener(){  
  11.                         public void onClick(DialogInterface dialog, int id){  
  12.                             /**********删除操作*********/  
  13.                               
  14.                     })  
  15.                     .show();  
  16.                 AlertDialog alert = builder.create();  
AlertDialog.Builder builder = new AlertDialog.Builder(History.this);
				builder.setMessage(areaChoose.getText().toString())
					.setCancelable(true)
					.setPositiveButton(R.string.update, new DialogInterface.OnClickListener(){
						 public void onClick(DialogInterface dialog, int id) {
							 /*********更新操作******************/
							 
						 }
					})
					.setNegativeButton(R.string.delete, new DialogInterface.OnClickListener(){
						public void onClick(DialogInterface dialog, int id){
							/**********删除操作*********/
							
					})
					.show();
				AlertDialog alert = builder.create();
这里我选择了让他按下后弹出一个对话框,对话框的标题是选择的城市,两个按钮,更新或者删除,使用了AlertDialog.Builder来实现。,简单说一下这个对象的各种方法:

setMessage(String title);这是一个对话框的标题,String类型

setCancelable(boolean);定义了当这个对话框出现后,可不可以按手机上的返回键退出这个对话框。

setPositiveButton():这定义了Yes按钮上的文字和一个监听器,将按下后要执行的代码写在onClick方法里就好

setNegativeButton();这个是定义了No按钮。

show();跟Toast最后的show()一个性质,如果不show出来的话我们是看不见的。

最后一定要creat()出来。

除了这几个之外还有:

setIcon :为对话框设置图标

setView : 给对话框设置自定义样式
setItems :设置对话框要显示的一个list,一般用于显示几个命令时

setMultiChoiceItems :用来设置对话框显示一系列的复选框
setNeutralButton    :普通按钮

这样,历史记录的功能就Clear了。





   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值