前面我们已经实现了把每月的收支明细,录入到了表中,现在就是要实现把这些数据从sqlite的数据表中取出来展现.
上图就是最后的界面.
在设计该界面时我考虑过好几个方案.本来准备使用一个gridview 因为觉得名字很像我需要的东西.可是后来查了一些资料,并且做了点实验,发现和我想象的有些差距.于是采用了目前这种方式.使用listview .
这个界面布局实际上很简单,就是上面一个表头(linearlayout) 中间一个listview 下面是一个脚注(linearlayout)
如何实现listview其中内容?这个主要就是要理解Adapter的用法.
SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to)
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 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)该方法就可以绑定了.
这次我在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的方法.