什么是从数据库加载用户数据并将其传递给 Intent Extras 的正确方法

我正在进行的项目允许用户注册和登录,并且一切正常,直到我发现代码有问题。它不算作错误,因为编译器不认为它是错误。它只是一个错误或人们称之为的任何东西。所以这就是发生的事情。用户登录到他们的帐户,数据库将他们的数据传输到intent extras. 然后在接下来的活动中,用户名、硬币和宝石出现在页面顶部,以便用户知道他们还剩下多少硬币。出于测试目的,我添加了添加硬币和减少硬币按钮。尽管如此,代码仍然可以完美运行。但用户注销并重新登录后,coins恢复到原来的金额。我知道将值放在 coin 变量上引起的问题User.java班级。仍然在登录时,我将用户的硬币和宝石的默认值放在了额外的意图中。在用户登录时,我只是找不到如何将数据库中的值放入意图附加项的方法。所以这里是登录活动的代码


buttonLogin.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View view) {


            //Check user input is correct or not

            if (validate()) {


                //Get values from EditText fields

                String Email = editTextEmail.getText().toString();

                String Password = editTextPassword.getText().toString();

                User player1 = new User(null, null, Email, Password);

                //Authenticate user

                User currentUser = myDb.Authenticate(player1);


                //Check Authentication is successful or not

                if (currentUser != null) {

                    System.out.println("Success");


                    Bundle extras = new Bundle();

                    extras.putString("P_ID", currentUser.getId());

                    extras.putString("P_NAME", currentUser.getName());

                    extras.putInt("P_COINS", currentUser.getCoins());

                    extras.putInt("P_GEMS", currentUser.getGems());

                    Intent intent = new Intent(getApplicationContext(),HomeActivity.class);

                    intent.putExtras(extras);

                    startActivity(intent);

                    finish();

                } else {


                    //User Logged in Failed

                    System.out.println("Failed");

                }

            }

        }

    });



老实说,我的大脑缺乏逻辑。这就是我来这里的原因,看看我的代码是否有意义。如果你不明白我的意思,请问我哪些部分,请不要立即标记我的问题。我的英语真的很差,相信我。


一只名叫tom的猫
浏览 365回答 1
1回答

森林海

我建议只传递用户 ID(它永远不应该改变),然后总是从数据库中获取值硬币等,并且也只在数据库中更改它们(然后从数据库中的值重置显示的值)。然后,您将不会遇到尝试处理两组数据的问题,然后您将依赖真实数据,即数据库中的数据。工作示例以下是一些基本的代码。当它启动 MainActivity 时,在您登录时立即启动 LoginActivity,然后将您带到 HomeActivity。这将显示当前的用户 ID、用户名、硬币(最初为 0)和宝石。有 2 个按钮Add10Coins和Add10gems单击它们会将新值应用于显示更新值的数据库。如果您停止应用程序并重新运行,登录然后值将保持原样。明智地传递值,虽然 LoginActivity 设置了 3 个 Intent Extra 值,但 HomeActivity 只使用了一个(用户 ID 为 long),但根据显示的值,所有值都是可访问的。如果启动了另一个活动,那么您所要做的就是通过意图传递用户 ID。代码不是我所说的复杂,但我肯定会建议仔细阅读并尝试理解它。用户.java我添加了一些方法并添加了一些常量,现在是:-public class User {&nbsp; &nbsp; public static final int ADJUSTTYPE_ADD = 1;&nbsp; &nbsp; public static final int ADJUSTTYPE_REPLACE = 2;&nbsp; &nbsp; public static final int ADJUSTTYPE_MULTIPLY = 3;&nbsp; &nbsp; public static final int ADJUSTTYPE_DIVIDE = 4;&nbsp; &nbsp; String id;&nbsp; &nbsp; String userName;&nbsp; &nbsp; String email;&nbsp; &nbsp; String password;&nbsp; &nbsp; int coins;&nbsp; &nbsp; int gems;&nbsp; &nbsp; public User(String id, String userName, String email, String password) {&nbsp; &nbsp; &nbsp; &nbsp; this.id = id;&nbsp; &nbsp; &nbsp; &nbsp; this.email = email;&nbsp; &nbsp; &nbsp; &nbsp; //And so on. Don't mind this&nbsp; &nbsp; }&nbsp; &nbsp; public void setId(String id) {&nbsp; &nbsp; &nbsp; &nbsp; this.id = id;&nbsp; &nbsp; }&nbsp; &nbsp; public String getId() {&nbsp; &nbsp; &nbsp; &nbsp; return id;&nbsp; &nbsp; }&nbsp; &nbsp; public void setName(String userName) {&nbsp; &nbsp; &nbsp; &nbsp; this.userName = userName;&nbsp; &nbsp; }&nbsp; &nbsp; public String getName() {&nbsp; &nbsp; &nbsp; &nbsp; return this.userName;&nbsp; &nbsp; }&nbsp; &nbsp; public void setEmail(String email) {&nbsp; &nbsp; &nbsp; &nbsp; this.email = email;&nbsp; &nbsp; }&nbsp; &nbsp; public String getEmail() {&nbsp; &nbsp; &nbsp; &nbsp; return email;&nbsp; &nbsp; }&nbsp; &nbsp; public void setPassword(String password) {&nbsp; &nbsp; &nbsp; &nbsp; this.password = password;&nbsp; &nbsp; }&nbsp; &nbsp; public String getPassword() {&nbsp; &nbsp; &nbsp; &nbsp; return password;&nbsp; &nbsp; }&nbsp; &nbsp; public void setCoins(int coins) {&nbsp; &nbsp; &nbsp; &nbsp; this.coins = coins;&nbsp; &nbsp; }&nbsp; &nbsp; public int getCoins() {&nbsp; &nbsp; &nbsp; &nbsp; return this.coins;&nbsp; &nbsp; }&nbsp; &nbsp; public void setGems(int gems) {&nbsp; &nbsp; &nbsp; &nbsp; this.gems = gems;&nbsp; &nbsp; }&nbsp; &nbsp; public int getGems() {&nbsp; &nbsp; &nbsp; &nbsp; return this.gems;&nbsp; &nbsp; }&nbsp; &nbsp; public long getLongId() {&nbsp; &nbsp; &nbsp; &nbsp; long id;&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id = Long.valueOf(this.id);&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return -1;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return id;&nbsp; &nbsp; }}数据库助手.java这是基于不是对您的代码进行最严格的检查而从头开始编写的,它将在很大程度上受到我的样式/使用技术的影响,但不会影响到我申请实际开发的程度。其中是方法adjustCoinsAndOrGems这是用于更新数据库中以及返回的用户中的宝石或硬币的方法(以便返回用户的同步版本,而不是使用返回的使用(我个人更喜欢只要不是问题就访问数据库(例如显着影响性能)))public class DatabaseHelper extends SQLiteOpenHelper {&nbsp; &nbsp; public static final String DBNAME = "mygame.db";&nbsp; &nbsp; public static final int DBVERSION = 1;&nbsp; &nbsp; public static final String TBL_USER = "user";&nbsp; &nbsp; public static final String COL_USER_ID = BaseColumns._ID;&nbsp; &nbsp; public static final String COL_USER_NAME = "user_name";&nbsp; &nbsp; public static final String COL_USER_EMAIL = "user_email";&nbsp; &nbsp; public static final String COL_USER_PASWWORD = "user_password";&nbsp; &nbsp; public static final String COL_USER_COINS = "user_coins";&nbsp; &nbsp; public static final String COL_USER_GEMS = "user_gems";&nbsp; &nbsp; public static final String TBL_PLAYER = "player";&nbsp; &nbsp; public static final String COL_PLYAER_ID = BaseColumns._ID;&nbsp; &nbsp; public static final String COL_PLAYER_OWNINGUSER = "player_owninguser";&nbsp; &nbsp; public static final String COL_PLAYER_NAME = "player_name";&nbsp; &nbsp; //...... other columns&nbsp; &nbsp; SQLiteDatabase mDB;&nbsp; &nbsp; public DatabaseHelper(Context context) {&nbsp; &nbsp; &nbsp; &nbsp; super(context, DBNAME, null, DBVERSION);&nbsp; &nbsp; &nbsp; &nbsp; mDB = this.getWritableDatabase();&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onCreate(SQLiteDatabase db) {&nbsp; &nbsp; &nbsp; &nbsp; String crt_tbl_user = "CREATE TABLE IF NOT EXISTS " + TBL_USER + "(" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; COL_USER_ID + " INTEGER PRIMARY KEY," +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; COL_USER_NAME + " TEXT NOT NULL UNIQUE," +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; COL_USER_EMAIL + " TEXT NOT NULL UNIQUE," +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; COL_USER_PASWWORD + " TEXT NOT NULL," +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; COL_USER_COINS + " INTEGER," +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; COL_USER_GEMS + " INTEGER" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ")";&nbsp; &nbsp; &nbsp; &nbsp; String crt_tbl_player = "CREATE TABLE IF NOT EXISTS " + TBL_PLAYER + "(" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; COL_PLYAER_ID + " INTEGER PRIMARY KEY," +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; COL_PLAYER_NAME + " TEXT NOT NULL," +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; COL_PLAYER_OWNINGUSER + " INTEGER REFERENCES " + TBL_USER + "(" + COL_USER_ID + ")" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ")";&nbsp; &nbsp; &nbsp; &nbsp; db.execSQL(crt_tbl_user);&nbsp; &nbsp; &nbsp; &nbsp; db.execSQL(crt_tbl_player);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onUpgrade(SQLiteDatabase db, int i, int i1) {&nbsp; &nbsp; }&nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp; &nbsp; Note core add but not intended to be used directly&nbsp; &nbsp; &nbsp; &nbsp; Note this assumes that checks are done to ensure that name, email and password&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; have been provided&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private long addUser(Long id, String name, String email, String password, int coins, int gems) {&nbsp; &nbsp; &nbsp; &nbsp; ContentValues cv = new ContentValues();&nbsp; &nbsp; &nbsp; &nbsp; if (id > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cv.put(COL_USER_ID,id);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (name.length() > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cv.put(COL_USER_NAME,name);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (email.length() > 0 ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cv.put(COL_USER_EMAIL,email);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (password.length() > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cv.put(COL_USER_PASWWORD,password);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; cv.put(COL_USER_COINS,coins);&nbsp; &nbsp; &nbsp; &nbsp; cv.put(COL_USER_GEMS,gems);&nbsp; &nbsp; &nbsp; &nbsp; if (cv.size() < 1) return -1; //<<<<<<<<<< return if nothing to add&nbsp; &nbsp; &nbsp; &nbsp; return mDB.insert(TBL_USER,null,cv);&nbsp; &nbsp; }&nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp; &nbsp; For add with just name, email and password (normal usage)&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public long addUser(String name, String email, String password) {&nbsp; &nbsp; &nbsp; &nbsp; return this.addUser(-1L,name,email,password,0,0);&nbsp; &nbsp; }&nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp; &nbsp; For adding a user setting the coins and gems (special usage)&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public long addUserSettingCoinsAndGems(String name, String email, String password, int coins, int gems) {&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; this.addUser(-1L,name,email,password,coins,gems);&nbsp; &nbsp; }&nbsp; &nbsp; public User getUser(long id) {&nbsp; &nbsp; &nbsp; &nbsp; User rv = new User("-1","",",",""); // Invalid user&nbsp; &nbsp; &nbsp; &nbsp; String whereclause = COL_USER_ID + "=?";&nbsp; &nbsp; &nbsp; &nbsp; String[] whereargs = new String[]{String.valueOf(id)};&nbsp; &nbsp; &nbsp; &nbsp; Cursor csr = mDB.query(TBL_USER,null,whereclause,whereargs,null,null,null);&nbsp; &nbsp; &nbsp; &nbsp; if (csr.moveToFirst()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rv.setId(String.valueOf(id));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rv.setName(csr.getString(csr.getColumnIndex(COL_USER_NAME)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rv.setEmail(csr.getString(csr.getColumnIndex(COL_USER_EMAIL)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rv.setPassword(csr.getString(csr.getColumnIndex(COL_USER_PASWWORD)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rv.setCoins(csr.getInt(csr.getColumnIndex(COL_USER_COINS)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rv.setGems(csr.getInt(csr.getColumnIndex(COL_USER_GEMS)));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; csr.close();&nbsp; &nbsp; &nbsp; &nbsp; return rv;&nbsp; &nbsp; }&nbsp; &nbsp; public User getUser(String userid) {&nbsp; &nbsp; &nbsp; &nbsp; String whereclause = COL_USER_ID + "=?";&nbsp; &nbsp; &nbsp; &nbsp; User rv = new User("-1","",",",""); // Invalid user&nbsp; &nbsp; &nbsp; &nbsp; long id;&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id = Long.valueOf(userid);&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return rv;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; String[] whereargs = new String[]{String.valueOf(id)};&nbsp; &nbsp; &nbsp; &nbsp; Cursor csr = mDB.query(TBL_USER,null,whereclause,whereargs,null,null,null);&nbsp; &nbsp; &nbsp; &nbsp; if (csr.moveToFirst()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rv.setId(String.valueOf(id));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rv.setName(csr.getString(csr.getColumnIndex(COL_USER_NAME)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rv.setEmail(csr.getString(csr.getColumnIndex(COL_USER_EMAIL)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rv.setPassword(csr.getString(csr.getColumnIndex(COL_USER_PASWWORD)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rv.setCoins(csr.getInt(csr.getColumnIndex(COL_USER_COINS)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rv.setGems(csr.getInt(csr.getColumnIndex(COL_USER_GEMS)));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; csr.close();&nbsp; &nbsp; &nbsp; &nbsp; return rv;&nbsp; &nbsp; }&nbsp; &nbsp; public User getUser(String email, String password) {&nbsp; &nbsp; &nbsp; &nbsp; User rv = new User("-1","","","");&nbsp; &nbsp; &nbsp; &nbsp; String whereclause = COL_USER_EMAIL + "=? AND " + COL_USER_PASWWORD + "=?";&nbsp; &nbsp; &nbsp; &nbsp; String[] whereargs = new String[]{email,password};&nbsp; &nbsp; &nbsp; &nbsp; Cursor csr = mDB.query(TBL_USER,null,whereclause,whereargs,null,null,null);&nbsp; &nbsp; &nbsp; &nbsp; if (csr.moveToFirst()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rv.setId( String.valueOf(csr.getLong(csr.getColumnIndex(COL_USER_ID))));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rv.setName(csr.getString(csr.getColumnIndex(COL_USER_NAME)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rv.setEmail(csr.getString(csr.getColumnIndex(COL_USER_EMAIL)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rv.setPassword(csr.getString(csr.getColumnIndex(COL_USER_PASWWORD)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rv.setCoins(csr.getInt(csr.getColumnIndex(COL_USER_COINS)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rv.setGems(csr.getInt(csr.getColumnIndex(COL_USER_GEMS)));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; csr.close();&nbsp; &nbsp; &nbsp; &nbsp; return rv;&nbsp; &nbsp; }&nbsp; &nbsp; public User adjustCoinsAndOrGems(User u, int coins, int coin_adjustmode, int gems, int gem_adjustmode) {&nbsp; &nbsp; &nbsp; &nbsp; ContentValues cv = new ContentValues();&nbsp; &nbsp; &nbsp; &nbsp; User rv;&nbsp; &nbsp; &nbsp; &nbsp; User user_fromDB = getUser(u.getId());&nbsp; &nbsp; &nbsp; &nbsp; if (user_fromDB.id.equals("-1")) return u; // User not found so return&nbsp; &nbsp; &nbsp; &nbsp; switch (coin_adjustmode) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case User.ADJUSTTYPE_REPLACE:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cv.put(COL_USER_COINS,coins);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case User.ADJUSTTYPE_ADD:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (coins != 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cv.put(COL_USER_COINS,user_fromDB.getCoins() + coins);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case User.ADJUSTTYPE_MULTIPLY:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (coins > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cv.put(COL_USER_COINS,user_fromDB.getCoins() * coins);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case User.ADJUSTTYPE_DIVIDE:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (coins > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cv.put(COL_USER_COINS,user_fromDB.getCoins() / coins);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; switch (gem_adjustmode) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case User.ADJUSTTYPE_REPLACE:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cv.put(COL_USER_GEMS,gems);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case User.ADJUSTTYPE_ADD:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (gems != 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cv.put(COL_USER_GEMS,user_fromDB.getGems() + gems);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case User.ADJUSTTYPE_MULTIPLY:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (gems > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cv.put(COL_USER_GEMS,user_fromDB.getGems() * gems);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case User.ADJUSTTYPE_DIVIDE:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (gems > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cv.put(COL_USER_GEMS,user_fromDB.getGems() / gems);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (cv.size() < 1) return u;&nbsp; &nbsp; &nbsp; &nbsp; String whereclause = COL_USER_ID + "=?";&nbsp; &nbsp; &nbsp; &nbsp; String[] whereargs = new String[]{u.getId()};&nbsp; &nbsp; &nbsp; &nbsp; mDB.update(TBL_USER,cv,whereclause,whereargs);&nbsp; &nbsp; &nbsp; &nbsp; return getUser(user_fromDB.getId());&nbsp; &nbsp; }&nbsp; &nbsp; public boolean authenticateUser(String email, String password) {&nbsp; &nbsp; &nbsp; &nbsp; User u = getUser(email,password);&nbsp; &nbsp; &nbsp; &nbsp; return (u.getLongId() > 0);&nbsp; &nbsp; }}MainActivity.java启动 LoginActivity 并最终返回时什么都不做的非常简单的活动(因此您不妨杀死该应用程序)。public class MainActivity extends AppCompatActivity {&nbsp; &nbsp; TextView mMessage;&nbsp; &nbsp; DatabaseHelper mDB;&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.activity_main);&nbsp; &nbsp; &nbsp; &nbsp; mMessage = this.findViewById(R.id.message);&nbsp; &nbsp; &nbsp; &nbsp; mDB = new DatabaseHelper(this);&nbsp; &nbsp; &nbsp; &nbsp; addSomeTestingUsers();&nbsp; &nbsp; &nbsp; &nbsp; // Immediately start Login Activity&nbsp; &nbsp; &nbsp; &nbsp; Intent i = new Intent(MainActivity.this,LoginActivity.class);&nbsp; &nbsp; &nbsp; &nbsp; startActivity(i);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onResume() {&nbsp; &nbsp; &nbsp; &nbsp; super.onResume();&nbsp; &nbsp; &nbsp; &nbsp; mMessage.setText("Welcome back");&nbsp; &nbsp; }&nbsp; &nbsp; private void addSomeTestingUsers() {&nbsp; &nbsp; &nbsp; &nbsp; if (DatabaseUtils.queryNumEntries(mDB.getWritableDatabase(),DatabaseHelper.TBL_USER) > 0) return;&nbsp; &nbsp; &nbsp; &nbsp; mDB.addUser("Fred","fred@fredmal.com","password");&nbsp; &nbsp; &nbsp; &nbsp; mDB.addUser("Mary","mary@mary.email.com","password");&nbsp; &nbsp; }}登录活动这是非常简单的说明,就目前而言,您必须登录,并且 2 个用户的电子邮件和密码已编码在 MainActivity 中。正确供应后,HomeActivivty 将启动:-公共类 LoginActivity 扩展 AppCompatActivity {public static final String INTENTKEY_USERNAME = "IK_USERNAME";public static final String INTENTKEY_USERID = "IK_USERID";public static final String INTENTKEY_STRINGUSERID = "IK_USERIDSTRING";Button mloginbtn;EditText mEmail,mPassword;Context mContext;DatabaseHelper mDB;@Overrideprotected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; setContentView(R.layout.activity_login);&nbsp; &nbsp; mContext = this;&nbsp; &nbsp; mloginbtn = this.findViewById(R.id.loginbtn);&nbsp; &nbsp; mEmail = this.findViewById(R.id.email);&nbsp; &nbsp; mPassword = this.findViewById(R.id.password);&nbsp; &nbsp; mDB = new DatabaseHelper(this);&nbsp; &nbsp; mloginbtn.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View view) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; handleAuthentication();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}private void handleAuthentication() {&nbsp; &nbsp; if (mDB.authenticateUser(mEmail.getText().toString(),mPassword.getText().toString())) {&nbsp; &nbsp; &nbsp; &nbsp; User u = mDB.getUser(mEmail.getText().toString(),mPassword.getText().toString());&nbsp; &nbsp; &nbsp; &nbsp; Intent i = new Intent(mContext,HomeActivity.class);&nbsp; &nbsp; &nbsp; &nbsp; i.putExtra(INTENTKEY_USERNAME,u.getName());&nbsp; &nbsp; &nbsp; &nbsp; i.putExtra(INTENTKEY_USERID,u.getLongId());&nbsp; &nbsp; &nbsp; &nbsp; i.putExtra(INTENTKEY_STRINGUSERID,u.getId());&nbsp; &nbsp; &nbsp; &nbsp; startActivity(i);&nbsp; &nbsp; &nbsp; &nbsp; finish();&nbsp; &nbsp; }}家活动为简洁起见,这已用于显示硬币和宝石,它非常基本,并且依赖于 DatabaseHelper 中的方法来完成大部分工作。public class HomeActivity extends AppCompatActivity {&nbsp; &nbsp; TextView mUserameTextView, mUseridTextView, mCoinsTextView, mGemsTextView;&nbsp; &nbsp; Button mAdd10Coins, mAdd10Gems,mDone;&nbsp; &nbsp; User mUser;&nbsp; &nbsp; long mUserid;&nbsp; &nbsp; Context mContext;&nbsp; &nbsp; DatabaseHelper mDB;&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.activity_home);&nbsp; &nbsp; &nbsp; &nbsp; mContext = this;&nbsp; &nbsp; &nbsp; &nbsp; mDB = new DatabaseHelper(mContext);&nbsp; &nbsp; &nbsp; &nbsp; mUserameTextView = this.findViewById(R.id.username);&nbsp; &nbsp; &nbsp; &nbsp; mUseridTextView = this.findViewById(R.id.userid);&nbsp; &nbsp; &nbsp; &nbsp; mCoinsTextView = this.findViewById(R.id.coins);&nbsp; &nbsp; &nbsp; &nbsp; mGemsTextView = this.findViewById(R.id.gems);&nbsp; &nbsp; &nbsp; &nbsp; Intent i = this.getIntent();&nbsp; &nbsp; &nbsp; &nbsp; mUserid = i.getLongExtra(LoginActivity.INTENTKEY_USERID,-1);&nbsp; &nbsp; &nbsp; &nbsp; mUser = mDB.getUser(mUserid);&nbsp; &nbsp; &nbsp; &nbsp; refreshDisplay();&nbsp; &nbsp; &nbsp; &nbsp; initButtons();&nbsp; &nbsp; }&nbsp; &nbsp; private void initButtons() {&nbsp; &nbsp; &nbsp; &nbsp; mAdd10Coins = this.findViewById(R.id.add10coins);&nbsp; &nbsp; &nbsp; &nbsp; mAdd10Coins.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View view) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mDB.adjustCoinsAndOrGems(mUser,10,User.ADJUSTTYPE_ADD,0,User.ADJUSTTYPE_ADD);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mUser = mDB.getUser(mUserid);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; refreshDisplay();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; mAdd10Gems = this.findViewById(R.id.add10gems);&nbsp; &nbsp; &nbsp; &nbsp; mAdd10Gems.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View view) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mDB.adjustCoinsAndOrGems(mUser,0, User.ADJUSTTYPE_ADD,10,User.ADJUSTTYPE_ADD);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mUser = mDB.getUser(mUserid);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; refreshDisplay();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; mDone = this.findViewById(R.id.done);&nbsp; &nbsp; &nbsp; &nbsp; mDB = new DatabaseHelper(mContext);&nbsp; &nbsp; &nbsp; &nbsp; mDone.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View view) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; finish();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; private void refreshDisplay() {&nbsp; &nbsp; &nbsp; &nbsp; mUseridTextView.setText(mUser.getId());&nbsp; &nbsp; &nbsp; &nbsp; mUserameTextView.setText(mUser.getName());&nbsp; &nbsp; &nbsp; &nbsp; mCoinsTextView.setText(String.valueOf(mUser.getCoins()));&nbsp; &nbsp; &nbsp; &nbsp; mGemsTextView.setText(String.valueOf(mUser.getGems()));&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java