Android 实例-个人理财工具 之五 账单明细显示A

关键字:android sdk 1.0 custom listview

 

前面我们已经实现了把每月的收支明细,录入到了表中,现在就是要实现把这些数据从sqlite的数据表中取出来展现.

上图就是最后的界面.

在设计该界面时我考虑过好几个方案.本来准备使用一个gridview 因为觉得名字很像我需要的东西.可是后来查了一些资料,并且做了点实验,发现和我想象的有些差距.于是采用了目前这种方式.使用listview .

 

这个界面布局实际上很简单,就是上面一个表头(linearlayout) 中间一个listview 下面是一个脚注(linearlayout)

 

如何实现listview其中内容?这个主要就是要理解Adapter的用法.

SimpleCursorAdapter (Context   context, int layout,  Cursor   c,  String[]   from, int[] to)

  1.       
  2.         String[] from= new  String[] { "rowid" , "name""fee" , "sdate" , "desc"  };
  3.          int [] to= new   int [] { R.id.item1, R.id.item2,R.id.item3,R.id.item4,R.id.item5 };
  4.         SimpleCursorAdapter mAdapter= new  SimpleCursorAdapter( this ,R.layout.grid_items, cur,from, to);
  5.         lv.setAdapter(mAdapter);

这里我们只需要准备好view的样式,和cursor 就可以了.

例如本例中的

R.layout.grid_items是

  1. <?xml version= "1.0"  encoding= "utf-8" ?>  
  2. <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"   
  3.     android:orientation= "horizontal"  android:layout_width= "fill_parent"   
  4.     android:layout_height= "fill_parent" >  
  5.         <TextView android:id= "@+id/item1"  android:layout_height= "fill_parent"   
  6.         android:layout_width= "wrap_content"  android:width= "20dip"
  7.         />  
  8.         <TextView android:id= "@+id/item2"  
  9.         android:layout_height= "fill_parent"   
  10.         android:text= "账目"  
  11.         android:width= "60dip"  android:layout_width= "wrap_content" />  
  12.         /> 
  13.         <TextView android:id= "@+id/item3"  
  14.         android:text= "费用(元)"  
  15.         android:textSize= "14dip"  android:width= "60dip"  android:layout_width= "wrap_content"
  16.         android:layout_height= "fill_parent"  android:textStyle= "bold|italic"
  17.         /> 
  18.         <TextView android:id= "@+id/item4"
  19.         android:layout_height= "fill_parent"           
  20.         android:text= "日期"
  21.         android:width= "80dip"
  22.         android:layout_width= "wrap_content"
  23.         /> 
  24.         <TextView android:id= "@+id/item5"  
  25.         android:layout_height= "fill_parent"           
  26.         android:text= "备注"  
  27.         android:width= "100dip"  android:layout_width= "wrap_content"
  28.         />   
  29.       
  30. </LinearLayout> 

在Adapter中的to 参数中,指定这些TextView 使用那些Cursor的值.

我的cursor 就是含有这些字段"rowid","name", "fee","sdate","desc" .

 

准备好这些,使用lv.setAdapter(mAdapter)该方法就可以绑定了.

 

下面给出具体代码文件

Grid_bills.java

  1. package com.cola.ui;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import android.app.Activity;
  7. import android.database.Cursor;
  8. import android.os.Bundle;
  9. import android.util.Log;
  10. import android.view.KeyEvent;
  11. import android.view.View;
  12. import android.widget.AbsoluteLayout;
  13. import android.widget.EditText;
  14. import android.widget.GridView;
  15. import android.widget.LinearLayout;
  16. import android.widget.ListView;
  17. import android.widget.SimpleCursorAdapter;
  18. import android.widget.TextView;
  19. public   class  Grid_bills extends Activity {    
  20.     BilldbHelper billdb;
  21.     View sv;
  22.     EditText edit;
  23.     AbsoluteLayout alayout;
  24.      int  a=10,b=10;
  25.     GridView grd;
  26.     
  27.     TextView total;
  28.     
  29.      protected  GridView listHands =  null  ;
  30.      public   void  onCreate(Bundle icicle) {
  31.         super.onCreate(icicle);
  32.         setTitle( "ColaBox-账单明细(2008-11月)" );     
  33.      
  34.         setContentView( R.layout.grid_bills) ; 
  35.         billdb =  new  BilldbHelper( this );
  36.         Cursor cur=billdb.getBills();
  37.         ListView lv=(ListView)findViewById(R.id.listview);
  38.         String[] from= new  String[] { "rowid" , "name""fee" , "sdate" , "desc"  };
  39.          int [] to= new   int [] { R.id.item1, R.id.item2,R.id.item3,R.id.item4,R.id.item5 };
  40.         SimpleCursorAdapter mAdapter= new  SimpleCursorAdapter( this ,R.layout.grid_items, cur,from, to);
  41.         lv.setAdapter(mAdapter);
  42.         
  43.          //getBillsTotal
  44.         total=(TextView)findViewById(R.id.totalitem);
  45.         total.setText(billdb.getBillsTotal( "2008-11" ));
  46.     }    

grid_item.xml

  1. <?xml version= "1.0"  encoding= "utf-8" ?>  
  2. <ScrollView xmlns:android= "http://schemas.android.com/apk/res/android"
  3.     android:orientation= "vertical"
  4.     android:layout_height= "fill_parent"  android:layout_width= "fill_parent" >
  5. <LinearLayout 
  6. android:id= "@+id/LinearLayout01"  
  7. xmlns:android= "http://schemas.android.com/apk/res/android"  android:orientation= "vertical"  android:layout_height= "fill_parent"  android:layout_width= "fill_parent" >
  8.     <LinearLayout android:id= "@+id/layouthead"  
  9.     android:background= "#ffCded8b"  android:layout_height= "fill_parent"  android:layout_width= "fill_parent"  android:focusable= "true"  android:clickable= "true"  android:focusableInTouchMode= "true"  android:keepScreenOn= "true" >    
  10.         <TextView android:id= "@+id/item1"  android:layout_height= "fill_parent"   
  11.         android:layout_width= "wrap_content"  android:width= "20dip"
  12.         />  
  13.         <TextView android:id= "@+id/item2"  
  14.         android:layout_height= "fill_parent"   
  15.         android:text= "账目"  
  16.         android:textStyle= "bold"  android:width= "60dip"  android:layout_width= "wrap_content" />  
  17.         /> 
  18.         <TextView android:id= "@+id/item3"  
  19.         android:text= "费用(元)"  
  20.         android:textSize= "14dip"  android:textStyle= "bold"  android:width= "60dip"  android:layout_width= "wrap_content"
  21.         android:layout_height= "fill_parent" /> 
  22.         <TextView android:id= "@+id/item4"
  23.         android:layout_height= "fill_parent"           
  24.         android:text= "日期"
  25.         android:textSize= "14dip"  android:textStyle= "bold"  android:width= "80dip"  android:layout_width= "wrap_content"
  26.         /> 
  27.         <TextView android:id= "@+id/item5"  
  28.         android:layout_height= "fill_parent"           
  29.         android:text= "备注"  
  30.         android:textSize= "14dip"  android:textStyle= "bold"  android:width= "100dip"  android:layout_width= "wrap_content"
  31.         />    
  32.     </LinearLayout>
  33.     <View  android:layout_width= "fill_parent"  android:layout_height= "1dip"  android:background= "?android:attr/listDivider" />
  34.     <LinearLayout android:id= "@+id/layout"  android:layout_width= "wrap_content"  android:layout_height= "fill_parent"  android:minHeight= "372dip" >
  35.     
  36.     <ListView android:id= "@+id/listview"  android:layout_height= "fill_parent"  android:layout_width= "fill_parent" ></ListView>
  37. </LinearLayout>
  38.     <LinearLayout android:id= "@+id/layoutfoot"  
  39.     android:layout_width= "fill_parent"  
  40.     android:layout_height= "wrap_content"  android:background= "#ffCded8b" >    
  41.          
  42.         <TextView android:id= "@+id/totalitem"  
  43.         android:layout_height= "fill_parent"   
  44.         android:text= "当月收入:2009.33 支出:3000.87 小计:-1000.9"  
  45.         android:textStyle= "bold"   android:layout_width= "fill_parent"  />  
  46.         /> 
  47.            
  48.     </LinearLayout>
  49.         </LinearLayout>
  50. </ScrollView>

 

这次我在sqlite的sql上面遇到点麻烦,目前还没搞定,就是我保存在数据库中的费用是int型,分为单位.我从数据库中

取出来是 select fee/100 from bills ;但是显示的却是取整后的数值.

不知道正确语法应该是什么样子,后面我想拼成字符显示应该可以,我就试了 select fee/100||'' from bills;

这样就可以在listview上面输出小数. 可是我发现999999.99/100 输出却是1000000. 我在adb shell里面查询还是999999.99 到了listview时就变成了1000000 我估计可能是Adapter 里面的字符取出来用了getString的方法.

不知道有没有人帮我解决下这个问题.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值