Develop android applications using the android quick development framework afinal

Source: Internet
Author: User

First, we will introduce the afinal development framework:
1. afinal is a compact android Application Development Framework.
2. It contains an orm framework. You can add, delete, modify, and query android sqlite without configuring any files.
3. It contains an ioc framework that automatically binds android layout files and code controls, and supports event binding.
4. It contains a lightweight http framework that can complete http requests with only one line of code.

This article mainly introduces the ioc and orm functions of afinal:
The function we want to implement is very simple. We can add data to the database sqlite, then query all the data in the database to display on the listview, and then modify and delete the data in the listview. Complete the basic addition, deletion, modification, and query functions.

To put it bluntly, we should first create a mode class, which is very simple. The Code is as follows:
[Java]
Package com. yangfuhai. afinal;
 
Import net. tsz. afinal. annotation. sqlite. Id;
 
Public class User {
 
@ Id (column = "myId ")
Private int userId;

Private String name;
Private String email;


// Getter setter...


}

NOTE: If your object contains the id or _ id attribute, you do not need to configure @ Id (column = "myId ");
Next, let's take a look at the activity code:
[Java]
Package com. yangfuhai. afinal;
 
Import java. util. ArrayList;
Import java. util. List;
 
Import net. tsz. afinal. FinaActivity;
Import net. tsz. afinal. FinalDb;
Import net. tsz. afinal. annotation. view. ViewInject;
Import android. OS. Bundle;
Import android. text. TextUtils;
Import android. view. View;
Import android. view. ViewGroup;
Import android. widget. BaseAdapter;
Import android. widget. Button;
Import android. widget. EditText;
Import android. widget. ListView;
Import android. widget. TextView;
Import android. widget. Toast;
 
/**
* @ Title this is afinal demo
* @ Description
* @ Company explorer Network Studio (www.tsz.net)
* @ Author michael Young (www.YangFuhai.com)
* @ Version 1.0
* @ Created 2012-10-17
*/
Public class AfinalDemoActivity extends FinaActivity {

@ ViewInject (id = R. id. btn_add, click = "btnClick") Button BtnAdd;
@ ViewInject (id = R. id. btn_refresh, click = "btnClick") Button BtnRefresh;

@ ViewInject (id = R. id. listView, itemClick = "itemClick") ListView;

@ ViewInject (id = R. id. edit_name) EditText edit_name;
@ ViewInject (id = R. id. edit_email) EditText edit_email;

List <User> mListViewData = new ArrayList <User> ();

FinalDb;

Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Db = FinalDb. creat (this );
ListView. setAdapter (mListAdapter );
}

Public void btnClick (View v ){
If (v = BtnAdd ){
String name = edit_name.getText (). toString ();
String email = edit_email.getText (). toString ();

If (! TextUtils. isEmpty (name )&&! TextUtils. isEmpty (email )){
User user = new User ();
User. setEmail (email );
User. setName (name );

If (v. getTag ()! = Null ){
Int id = Integer. valueOf (v. getTag (). toString ());
User. setUserId (id );
Db. update (user );
ShowToast ("updated successfully ");

} Else {
Db. save (user );
ShowToast ("added successfully ");
}

Edit_name.setText ("");
Edit_email.setText ("");
BtnAdd. setTag (null );
BtnAdd. setText ("add data ");

}
} Else if (v = BtnRefresh ){
MListViewData. clear ();
MListViewData. addAll (db. findAll (User. class ));
MListAdapter. notifyDataSetChanged ();
}
}


Private View. OnClickListener itemButtonClick = new View. OnClickListener (){
Public void onClick (View v ){
Object obj = v. getTag ();
If (obj instanceof Integer ){
Int positon = Integer. valueOf (obj. toString ());
User user = mListViewData. get (positon );
If (v. getId () = R. id. item_btn_del) {// Delete the listview button

// Db. deleteById (User. class, user. getUserId (); // you can delete it in this way.
Db. deleteById (user );

ShowToast ("deleted successfully ");
BtnRefresh. Refreshing mclick (); // refresh the list

} Else if (v. getId () = R. id. item_btn_update) {// The update button of listview
Edit_email.setText (user. getEmail ());
Edit_name.setText (user. getName ());

BtnAdd. setText ("update data ");
BtnAdd. setTag (user. getUserId ());
}
}
}
};

Private void showToast (String strMsg ){
Toast. makeText (this, strMsg, 0). show ();
}



Private BaseAdapter mListAdapter = new BaseAdapter (){

Public View getView (int position, View convertView, ViewGroup parent ){
View item = View. inflate (AfinalDemoActivity. this, R. layout. listitem, null );

TextView TV _id = (TextView) item. findViewById (R. id. item_id );
TextView TV _name = (TextView) item. findViewById (R. id. item_name );
TextView TV _email = (TextView) item. findViewById (R. id. item_email );

Button btn_del = (Button) item. findViewById (R. id. item_btn_del );
Button btn_update = (Button) item. findViewById (R. id. item_btn_update );

User user = mListViewData. get (position );
TV _id.setText ("id:" + user. getUserId () + "");
TV _name.setText ("name:" + user. getName ());
TV _email.setText ("Email:" + user. getEmail ());

Btn_del.setTag (position );
Btn_update.setTag (position );
Btn_del.setOnClickListener (itemButtonClick );
Btn_update.setOnClickListener (itemButtonClick );

Return item;
}

Public long getItemId (int position ){
Return position;
}

Public Object getItem (int position ){
Return mListViewData. get (position );
}

Public int getCount (){
Return mListViewData. size ();
}
};



}

In the above column, we do not need findviewByid or setOnClickListener for the view, and complete annotation configuration. Makes the code more concise.
In database operations, we only need to instantiate the FinalDb object and use FinalDB to add, delete, and modify users.

There is a picture with the truth:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.