使用android快速开发框架afinal 开发android应用程序demo

[代码][Java]代码

001 packagecom.yangfuhai.afinal;
002
003 importjava.util.ArrayList;
004 importjava.util.List;
005
006 importnet.tsz.afinal.FinaActivity;
007 importnet.tsz.afinal.FinalDb;
008 importnet.tsz.afinal.annotation.view.ViewInject;
009 importandroid.os.Bundle;
010 importandroid.text.TextUtils;
011 importandroid.view.View;
012 importandroid.view.ViewGroup;
013 importandroid.widget.BaseAdapter;
014 importandroid.widget.Button;
015 importandroid.widget.EditText;
016 importandroid.widget.ListView;
017 importandroid.widget.TextView;
018 importandroid.widget.Toast;
019
020 /**
021 * @title 这是 afinal demo
022 * @description afinal下载地址 http://code.google.com/p/afinal/
023 * @company 探索者网络工作室(www.tsz.net)
024 * @author michael Young (www.YangFuhai.com)
025 * @version 1.0
026 * @created 2012-10-17
027 */
028 publicclassAfinalDemoActivityextendsFinaActivity {
029
030 @ViewInject(id=R.id.btn_add,click="btnClick") Button BtnAdd;
031 @ViewInject(id=R.id.btn_refresh,click="btnClick") Button BtnRefresh;
032
033 @ViewInject(id=R.id.listView,itemClick="itemClick") ListView listView;
034
035 @ViewInject(id=R.id.edit_name) EditText edit_name;
036 @ViewInject(id=R.id.edit_email) EditText edit_email;
037
038 List<User> mListViewData =newArrayList<User>();
039
040 FinalDb db;
041
042 publicvoidonCreate(Bundle savedInstanceState) {
043 super.onCreate(savedInstanceState);
044 setContentView(R.layout.main);
045 db = FinalDb.creat(this);
046 listView.setAdapter(mListAdapter);
047 }
048
049 publicvoidbtnClick(View v){
050 if(v == BtnAdd){
051 String name = edit_name.getText().toString();
052 String email = edit_email.getText().toString();
053
054 if(!TextUtils.isEmpty(name) && !TextUtils.isEmpty(email)){
055 User user =newUser();
056 user.setEmail(email);
057 user.setName(name);
058
059 if(v.getTag()!=null){
060 intid = Integer.valueOf(v.getTag().toString());
061 user.setUserId(id);
062 db.update(user);
063 showToast("更新成功");
064
065 }else{
066 db.save(user);
067 showToast("添加成功");
068 }
069
070 edit_name.setText("");
071 edit_email.setText("");
072 BtnAdd.setTag(null);
073 BtnAdd.setText("添加数据");
074
075 }
076 }elseif(v == BtnRefresh){
077 mListViewData.clear();
078 mListViewData.addAll(db.findAll(User.class));
079 mListAdapter.notifyDataSetChanged();
080 }
081 }
082
083
084 privateView.OnClickListener itemButtonClick =newView.OnClickListener() {
085 publicvoidonClick(View v) {
086 Object obj = v.getTag();
087 if(objinstanceofInteger){
088 intpositon = Integer.valueOf(obj.toString());
089 User user = mListViewData.get(positon);
090 if(v.getId() == R.id.item_btn_del){//listview的删除按钮
091
092 // db.deleteById(User.class, user.getUserId());//也可以用这种方式删除
093 db.deleteById(user);
094
095 showToast("删除成功");
096 BtnRefresh.performClick();//刷新列表
097
098 }elseif(v.getId() == R.id.item_btn_update){//listview的更新按钮
099 edit_email.setText(user.getEmail());
100 edit_name.setText(user.getName());
101
102 BtnAdd.setText("更新数据");
103 BtnAdd.setTag(user.getUserId());
104 }
105 }
106 }
107 };
108
109 privatevoidshowToast(String strMsg){
110 Toast.makeText(this, strMsg,0).show();
111 }
112
113
114
115 privateBaseAdapter mListAdapter =newBaseAdapter() {
116
117 publicView getView(intposition, View convertView, ViewGroup parent) {
118 View item = View.inflate(AfinalDemoActivity.this, R.layout.listitem,null);
119
120 TextView tv_id = (TextView) item.findViewById(R.id.item_id);
121 TextView tv_name = (TextView) item.findViewById(R.id.item_name);
122 TextView tv_email = (TextView) item.findViewById(R.id.item_email);
123
124 Button btn_del = (Button) item.findViewById(R.id.item_btn_del);
125 Button btn_update = (Button) item.findViewById(R.id.item_btn_update);
126
127 User user= mListViewData.get(position);
128 tv_id.setText("id:"+user.getUserId()+"");
129 tv_name.setText("名字:"+user.getName());
130 tv_email.setText("邮箱:"+user.getEmail());
131
132 btn_del.setTag(position);
133 btn_update.setTag(position);
134 btn_del.setOnClickListener(itemButtonClick);
135 btn_update.setOnClickListener(itemButtonClick);
136
137 returnitem;
138 }
139
140 publiclonggetItemId(intposition) {
141 returnposition;
142 }
143
144 publicObject getItem(intposition) {
145 returnmListViewData.get(position);
146 }
147
148 publicintgetCount() {
149 returnmListViewData.size();
150 }
151 };
152
153
154
155 }

[代码][Java]代码

01 packagecom.yangfuhai.afinal;
02
03 importnet.tsz.afinal.annotation.sqlite.Id;
04
05 publicclassUser {
06
07 @Id(column="myId")
08 privateintuserId;
09
10 privateString name;
11 privateString email;
12
13
14
15 publicintgetUserId() {
16 returnuserId;
17 }
18 publicvoidsetUserId(intuserId) {
19 this.userId = userId;
20 }
21 publicString getName() {
22 returnname;
23 }
24 publicvoidsetName(String name) {
25 this.name = name;
26 }
27 publicString getEmail() {
28 returnemail;
29 }
30 publicvoidsetEmail(String email) {
31 this.email = email;
32 }
33
34
35
36 }

[文件]afinalDemo-0.0.2.zip~247KB下载(88)

[图片]QQ截图20121018125439.png

使用android快速开发框架afinal 开发android应用程序demo

你可能感兴趣的:(android)