实例三

来源:互联网 发布:python 设计模式 pdf 编辑:程序博客网 时间:2024/04/29 03:35

从前面的登录页面跳转进入添加账单页面.这个页面主要是用来登记收支记录的.

说白了就是往数据库录入明细.

 

表结构就是db.execSQL("CREATE TABLE bills ("
                 + "_ID INTEGER PRIMARY KEY," //id
                 + "fee integer,"                                     //费用

                 +"acctitemid integer,"                          //账目类型
                 + "userid integer,"                                //使用者
                 + "sdate TEXT,"                                 //日期
                 + "stime TEXT,"                                //时间
                 + "desc TEXT"                                  //备注
                 + ");");

 

可以看到主要是录入这些数据.首先是布置界面,我目前想到的用个tablelayout来布局

账目应该是一个ExpandableListActivity 2层的结构.需要从数据库里面读取.我在账目后面放了一个editview 只读没有光标的.也就是在这儿不可录入,在该editview的onclick事件里面我们打开账目选择界面。

关于ExpandableListActivity 大家可以参考android 里面apidemos 里面ExpandableList1,ExpandableList2,ExpandableList3

这里面对熟悉这个ui还是很有帮助的. 在ExpandableList2 里面就是从数据库进行读取的例子. 当然android里面那个我是没太

看明白因为他引用了import android.provider.Contacts.People; 联系人部分的框架,而我目前对数据库的操作和他不一样,我都是直接

sql访问.

但是你只要搞定2个cursor就ok了. Cursor groupCursor childCursor 其他都由SimpleCursorTreeAdapter帮你实现了.

下面我们来看看如何使用SimpleCursorTreeAdapter

//首先要实现groupcursor就是父节点游标,这个其实就是我的acctitem表的
//select * from accitem where pid is null 的结果
Cursor groupCursor = billdb.getParentNode();

        // Cache the ID column index
mGroupIdColumnIndex = groupCursor.getColumnIndexOrThrow("_ID");
        // Set up our adapter
mAdapter = new MyExpandableListAdapter(groupCursor, this,       android.R.layout.simple_expandable_list_item_1,
    android.R.layout.simple_expandable_list_item_1,
        new String[] { "NAME" }, // Name for group layouts
        new int[] { android.R.id.text1 }, 
        new String[] { "NAME" }, //
        new int[] { android.R.id.text1 });
setListAdapter(mAdapter);


//然后我要实现childCursor 
//其实就是select * from acctitem where id=pid 的结果

public class MyExpandableListAdapter extends SimpleCursorTreeAdapter {

public MyExpandableListAdapter(Cursor cursor, Context context,
                int groupLayout, int childLayout, String[] groupFrom,
                int[] groupTo, String[] childrenFrom, int[] childrenTo)
 {
            super(context, cursor, groupLayout, groupFrom, groupTo,
                    childLayout, childrenFrom, childrenTo);
  }

protected Cursor getChildrenCursor(Cursor groupCursor) {
   String pid = groupCursor.getLong(mGroupIdColumnIndex) + "";
   // Log.v("cola","pid="+pid);
   return billdb.getChildenNode(pid);
  }
}
//我们看看Billdbhelper里面的cursor
   public Cursor getParentNode(){
     return db.query("acctitem", new String[]{"_id", "name" }, "pid is null", null, null, null, "pid,_id");    
 
    }
   
    public Cursor getChildenNode(String pid){
     Log.v("cola","run getchildenNode");
     return db.query("acctitem", new String[]{"_id", "name" }, "pid="+pid, null, null, null, "_id");     
    }
只要这几步一个2级的tree list就可以出现了.

上面其实才是刚开始,后面我们需要使用一个自定义的Dialog 类似于一个inputBox 因为我们新增账目是需要输入账目的名称.

就是上面图4表现的.

虽然alertDialog提供了很多方法,可以选择list,treelist,radio, 可惜就是不能录入text.

这里我参考了api demos 里面的 DateWidgets1.java 和源代码里面DatePickerDialog.java .

我们可以从alertdialog 继承.然后添加一个Editview 最后把数据返回出来.只要把上面我说的2个java看清楚了后处理起来就简单了.

主要是一个回调函数的用法.下面看代码

//
public class Dialog_edit extends AlertDialog implements OnClickListener {
    private String text = "";
    private EditText edit;
    private OnDateSetListener mCallback; //定义回调函数

    private LinearLayout layout;

    public interface OnDateSetListener {  //回调接口

        void onDateSet(String text);
    }

    protected Dialog_edit(Context context, String title, String value,
            OnDateSetListener Callback) {
        super(context);
        mCallback = Callback;
        TextView label = new TextView(context);
        label.setText("hint");
        // setView(label);
        edit = new EditText(context);
        edit.setText(value);
        layout = new LinearLayout(context);
        layout.setOrientation(LinearLayout.VERTICAL);
        // LinearLayout.LayoutParams param =
        // new LinearLayout.LayoutParams(100, 40);
        // layout.addView(label, param);
        LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams(200,
                50);
        layout.addView(edit, param2);
       //添加edit
        setView(layout);
        setTitle(title);
        setButton("确定", this);
        setButton2("取消", (OnClickListener) null);

    }

    public void onClick(DialogInterface dialog, int which) {
        // Log.v("cola","U click which="+which);
        text = edit.getText().toString();
        Log.v("cola", "U click text=" + text);
        if (mCallback != null)
            mCallback.onDateSet(text);  //使用回调返回录入的数据

    }

}

这样我们就完成了自定义的dialog 我们可以使用它来新增和编辑账目. 对于账目的增删改就是sql的事情了

在这我又遇到一个问题就是我新增一个账目后如何来刷新界面,从而反映账目修改后的变化

在这我开始以为只要使用getExpandableListView().invalidate(); 就可以了,

因为我之前在ExpandableList1.java例子里面,使用它可以刷新界面.

在那个例子里面我修改了数组后调用该方法,界面就刷新了,而在这SimpleCursorTreeAdapter就行不通了,我想

应该只要刷新cursor应该就可以了,后来找到了notifyDataSetChanged 呵呵,果然可以了. 这样账目的录入和管理就搞定了.

 

下面给出目前最新的代码.

原创粉丝点击