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

[代码] [Java]代码

001 package com.yangfuhai.afinal;
002  
003 import java.util.ArrayList;
004 import java.util.List;
005  
006 import net.tsz.afinal.FinaActivity;
007 import net.tsz.afinal.FinalDb;
008 import net.tsz.afinal.annotation.view.ViewInject;
009 import android.os.Bundle;
010 import android.text.TextUtils;
011 import android.view.View;
012 import android.view.ViewGroup;
013 import android.widget.BaseAdapter;
014 import android.widget.Button;
015 import android.widget.EditText;
016 import android.widget.ListView;
017 import android.widget.TextView;
018 import android.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 public class AfinalDemoActivity extends FinaActivity {
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 = new ArrayList<User>();
039      
040     FinalDb db;
041      
042     public void onCreate(Bundle savedInstanceState) {
043         super.onCreate(savedInstanceState);
044         setContentView(R.layout.main);
045         db = FinalDb.creat(this);
046         listView.setAdapter(mListAdapter);
047     }
048      
049     public void btnClick(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 = new User();
056                 user.setEmail(email);
057                 user.setName(name);
058                  
059                 if(v.getTag()!=null){
060                     int id = 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         }else if(v == BtnRefresh){
077             mListViewData.clear();
078             mListViewData.addAll(db.findAll(User.class));
079             mListAdapter.notifyDataSetChanged();
080         }
081     }
082      
083      
084     private View.OnClickListener itemButtonClick = new View.OnClickListener() {
085         public void onClick(View v) {
086             Object obj = v.getTag();
087             if(obj instanceof Integer){
088                 int positon = 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                 }else if(v.getId() == R.id.item_btn_update){//listview的更新按钮

你可能感兴趣的:(使用android快速开发框架afinal 开发android应用程序demo)