Android 个人理财工具五:显示账单明细 上

所属分类: 软件编程 / Android 阅读数: 32
收藏 0 赞 0 分享

前面我们已经将每个月的收支明细存入到SQLite的数据表中,本文将实现从SQLite的数据表中取出这些数据显示为账单明细界面。

       下图是最终的效果图:

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

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

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

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

Java代码

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

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

       例如本例中的

       R.layout.grid_items是

XML/HTML代码

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:orientation="horizontal" android:layout_width="fill_parent" 
 android:layout_height="fill_parent"> 
 <TextView android:id="@+id/item1" android:layout_height="fill_parent" 
 android:layout_width="wrap_content" android:width="20dip" 
 /> 
 <TextView android:id="@+id/item2" 
 android:layout_height="fill_parent" 
 android:text="账目" 
 android:width="60dip" android:layout_width="wrap_content"/> 
 /> 
 <TextView android:id="@+id/item3" 
 android:text="费用(元)" 
 android:textSize="14dip" android:width="60dip" android:layout_width="wrap_content" 
 android:layout_height="fill_parent" android:textStyle="bold|italic" 
 /> 
 <TextView android:id="@+id/item4" 
 android:layout_height="fill_parent"  
 android:text="日期" 
 android:width="80dip" 
 android:layout_width="wrap_content" 
 /> 
 <TextView android:id="@+id/item5" 
 android:layout_height="fill_parent"  
 android:text="备注" 
 android:width="100dip" android:layout_width="wrap_content" 
 /> 
  
</LinearLayout> 

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

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

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

       下面给出具体代码文件:

       Grid_bills.java

Java代码

package com.cola.ui; 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 
import android.app.Activity; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.KeyEvent; 
import android.view.View; 
import android.widget.AbsoluteLayout; 
import android.widget.EditText; 
import android.widget.GridView; 
import android.widget.LinearLayout; 
import android.widget.ListView; 
import android.widget.SimpleCursorAdapter; 
import android.widget.TextView; 
public class Grid_bills extends Activity { 
 BilldbHelper billdb; 
 View sv; 
 EditText edit; 
 AbsoluteLayout alayout; 
 int a=10,b=10; 
 GridView grd; 
 
 TextView total; 
 
 protected GridView listHands = null ; 
 public void onCreate(Bundle icicle) { 
 super.onCreate(icicle); 
 setTitle("ColaBox-账单明细(2008-11月)"); 
 
 setContentView( R.layout.grid_bills) ; 
 billdb = new BilldbHelper(this); 
 Cursor cur=billdb.getBills(); 
 ListView lv=(ListView)findViewById(R.id.listview); 
 String[] from=new String[] {"rowid","name", "fee","sdate","desc" }; 
 int[] to=new int[] { R.id.item1, R.id.item2,R.id.item3,R.id.item4,R.id.item5 }; 
 SimpleCursorAdapter mAdapter=new SimpleCursorAdapter(this,R.layout.grid_items, cur,from, to); 
 lv.setAdapter(mAdapter); 
  
 //getBillsTotal 
 total=(TextView)findViewById(R.id.totalitem); 
 total.setText(billdb.getBillsTotal("2008-11")); 
 } 

       grid_item.xml

XML/HTML代码

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
 android:orientation="vertical" 
 android:layout_height="fill_parent" android:layout_width="fill_parent"> 
<LinearLayout 
android:id="@+id/LinearLayout01" 
xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_height="fill_parent" android:layout_width="fill_parent"> 
 <LinearLayout android:id="@+id/layouthead" 
 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"> 
 <TextView android:id="@+id/item1" android:layout_height="fill_parent" 
 android:layout_width="wrap_content" android:width="20dip" 
 /> 
 <TextView android:id="@+id/item2" 
 android:layout_height="fill_parent" 
 android:text="账目" 
 android:textStyle="bold" android:width="60dip" android:layout_width="wrap_content"/> 
 /> 
 <TextView android:id="@+id/item3" 
 android:text="费用(元)" 
 android:textSize="14dip" android:textStyle="bold" android:width="60dip" android:layout_width="wrap_content" 
 android:layout_height="fill_parent"/> 
 <TextView android:id="@+id/item4" 
 android:layout_height="fill_parent"  
 android:text="日期" 
 android:textSize="14dip" android:textStyle="bold" android:width="80dip" android:layout_width="wrap_content" 
 /> 
 <TextView android:id="@+id/item5" 
 android:layout_height="fill_parent"  
 android:text="备注" 
 android:textSize="14dip" android:textStyle="bold" android:width="100dip" android:layout_width="wrap_content" 
 /> 
 </LinearLayout> 
 <View android:layout_width="fill_parent" android:layout_height="1dip" android:background="?android:attr/listDivider"/> 
 <LinearLayout android:id="@+id/layout" android:layout_width="wrap_content" android:layout_height="fill_parent" android:minHeight="372dip"> 
 
 <ListView android:id="@+id/listview" android:layout_height="fill_parent" android:layout_width="fill_parent"></ListView> 
</LinearLayout> 
 <LinearLayout android:id="@+id/layoutfoot" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" android:background="#ffCded8b"> 
  
 <TextView android:id="@+id/totalitem" 
 android:layout_height="fill_parent" 
 android:text="当月收入:2009.33 支出:3000.87 小计:-1000.9" 
 android:textStyle="bold" android:layout_width="fill_parent" /> 
 /> 
  
 </LinearLayout> 
 </LinearLayout> 
</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的方法。

              系列文章:

                       Android 个人理财工具六:显示账单明细 下

                       Android 个人理财工具五:显示账单明细 上

                       Android 个人理财工具四:添加账单页面 下

                       Android 个人理财工具三:添加账单页面 上

                       Android 个人理财工具二:使用SQLite实现启动时初始化数据

                       Android 个人理财工具一:项目概述与启动界面的实现

           以上就是关于显示账单明细的功能实现,后续继续添加相关功能,谢谢大家对本站的支持!

更多精彩内容其他人还在看

Android设计登录界面、找回密码、注册功能

这篇文章主要为大家详细介绍了Android设计登录界面的方法,Android实现找回密码、注册功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android通过手势实现答题器翻页效果

这篇文章主要为大家详细介绍了Android通过手势实现答题器翻页效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android采用双缓冲技术实现画板

这篇文章主要为大家详细介绍了Android采用双缓冲技术实现画板的相关资料,思路清晰,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android开发之毛玻璃效果实例代码

这篇文章主要给大家分享android开发之毛玻璃效果的实例代码,非常具有参考借鉴价值,感兴趣的朋友一起学习吧
收藏 0 赞 0 分享

Android实现桌面悬浮窗、蒙板效果实例代码

这篇文章主要介绍了Android实现桌面悬浮窗、蒙板效果实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

深入解读Android的Volley库的功能结构

这篇文章主要介绍了Android的Volley开发框架的功能结构,Volley是Android开发中网络部分的一大利器,包含很多HTTP协议通信的相关操作,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发中使用Volley库发送HTTP请求的实例教程

这篇文章主要介绍了Android开发中使用Volley库发送HTTP请求的实例教程,包括创建Volley单例的基本知识与取消Request请求的技巧等,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿QQ聊天撒花特效 很真实

本文写的这个特效,是关于聊天的,你肯定遇到过,就是你跟人家聊天的时候,比如发送应(么么哒),然后屏幕上全部就是表情了,今天我们就是做这个,撒花的特效,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android的HTTP操作库Volley的基本使用教程

这篇文章主要介绍了Android的HTTP操作库Volley的基本使用教程,包括JSON请求与图片加载等用法的实例,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿水波纹流量球进度条控制器

这篇文章主要介绍了Android仿水波纹流量球进度条控制器的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多