关键字: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)
- 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)该方法就可以绑定了.
下面给出具体代码文件
Grid_bills.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 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的方法.
不知道有没有人帮我解决下这个问题.