Back to project page Abstract-Model.
The source code is released under:
Apache License
If you think the Android project Abstract-Model listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.logician.abstractModel.examples; /* w w w.jav a2 s.c o m*/ import java.util.List; import com.logician.abstractModel.AsyncModelFactory.ListCallback; import android.app.Activity; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; public class UsersActivity extends Activity implements ListCallback<User>{ private List<User> users; @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); //setContentView(R.layout.activity_main); initUsers(); showUsers(); } private void showUsers() { /* * Do whatever with the list of Users here. * Perhaps send them to a ListAdapter for display in a ListView. * */ } /* * You probably want to use an implementation of Loader instead, * preferably CursorLoader. * Then your ContentProvider can call getContext().getContentResolver().notifyChange() * to automatically update your views on a user operation. * */ private void initUsers() { SQLiteDatabase db = MyDatabaseOpenHelper.getInstance(this).getReadableDatabase(); Cursor cursor = db.query(new User().getTableName(), null, null, null, null, null, null); // That is seriously all you have to do! // (UsersActivity implements ListCallback<User>, so we just have to pass "this". User.ASYNC_FACTORY.listFromCursor(cursor, this); } private void setUsers(List<User> list){ users = list; // Update ListAdapters, etc } private void createUser(String username, String password){ User newUser = new User(); newUser.username = username; newUser.password = password; SQLiteDatabase db = MyDatabaseOpenHelper.getInstance(this).getWritableDatabase(); db.insert(newUser.getTableName(), null, newUser.toContentValues()); } private void login(String username, String password){ User user = new User(); SQLiteDatabase db = MyDatabaseOpenHelper.getInstance(this).getReadableDatabase(); Cursor cursor = db.query(user.getTableName(), null, User.USERNAME+"=?", new String[]{username}, null, null, null); if(cursor.moveToFirst()){ user.fromCursor(cursor); if(user.password == password) loginSuccess(user); } else{ failLogin(username, "Username does not exist!"); } } private void changePassword(User user, String newPassword){ user.password = newPassword; } private void loginSuccess(User user) { /* * The login was a success! Report it to the user, allow them to perform * restricted actions, etc. */ } private void failLogin(String username, String message){ /* * Show an error message, log the failed attempt, etc. */ } @Override public void onResult(List<User> models) { setUsers(models); } }