Java Android Studio数据库连接

Java Android Studio数据库连接,java,android,database,Java,Android,Database,这是我得到的一张模型纸。我需要确保,我的代码是100%正确的。 A.创建一个名为“主页”的活动,并设计给定的布局 创建一个名为ProfileManagement的活动,并设计给定的布局。 创建名为EditProfile的活动并设计给定的布局。 (我做了所有这些布局部分) 最后一个名为“UserProfile”的类。创建默认构造函数 私有的通过实现创建一个名为“Users”的内部类 “BaseColumn”接口。在内部类中,定义所需的列 我需要一张桌子。 B实现一个名为addInfo()的方法来存

这是我得到的一张模型纸。我需要确保,我的代码是100%正确的。 A.创建一个名为“主页”的活动,并设计给定的布局 创建一个名为ProfileManagement的活动,并设计给定的布局。 创建名为EditProfile的活动并设计给定的布局。 (我做了所有这些布局部分)

最后一个名为“UserProfile”的类。创建默认构造函数 私有的通过实现创建一个名为“Users”的内部类 “BaseColumn”接口。在内部类中,定义所需的列 我需要一张桌子。 B实现一个名为addInfo()的方法来存储用户详细信息。 C实现名为updateInner()的方法来修改存储的用户 基于用户ID的详细信息。方法必须返回基于 关于成功或失败。D实现一个名为readAllin()的方法 检索数据库表中存储的所有用户详细信息。E 重载readAllin()方法以检索基于 在主键上。F实现一个名为deleteInfo()的方法来删除 特定用户

通过在数据库文件夹中创建另一个名为DBHelper的类 将类SQLiteOpenHelper扩展为其超类。实施 相关方法和施工人员

创建意图对象以启动。档案管理活动来自 “注册”按钮b。从“更新配置文件”按钮编辑配置文件活动 A.从中调用DbHandler类中实现的AddInner()方法 寄存器按钮的onClick事件。显示一条Toast消息,指示 成败

家庭活动

package com.bla.androidsqlitebasics.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.bla.androidsqlitebasics.database.DBHelper;
import com.live.sinhalacoder.androidsqlitebasics.R;

public class HomeActivity extends AppCompatActivity {

    EditText usernameEt, passwordEt;
    Button loginBt, registerBt;

    //to get access to database table
    DBHelper mHelper;

    //newly added user primary key
    long userId = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        usernameEt = findViewById(R.id.idUsernameEt);
        passwordEt = findViewById(R.id.idPasswordEt);

        loginBt = findViewById(R.id.idLoginBt);
        registerBt = findViewById(R.id.idRegisterBt);

        //initialize db helper when app create
        mHelper = new DBHelper(this);

        //if user clicked login button
        loginBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //login existing user
                login();
            }
        });

        //if user clicked register button
        registerBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //add new user to the database
                registerUser();
            }
        });
    }

    private void login() {
        String username = usernameEt.getText().toString();
        String password = passwordEt.getText().toString();

        //if username and pass does not match -1 will return from checkUser function and if not -1 logged in
        userId = mHelper.checkUser(username, password);
        if (userId != -1) {
            Intent intent = new Intent(HomeActivity.this, ProfileManagementActivity.class);
            intent.putExtra("userId", userId);
            startActivity(intent);
        } else {
            Toast.makeText(this, "Err.. Try again!", Toast.LENGTH_SHORT).show();
        }
    }

    public void registerUser() {
        String username = usernameEt.getText().toString();
        String password = passwordEt.getText().toString();

        userId = mHelper.addInfo(username, password);
        if (userId == -1) {
            Toast.makeText(this, "Err.. Try again!", Toast.LENGTH_SHORT).show();
        } else {
            //Toast.makeText(this, "Successfully registed!", Toast.LENGTH_SHORT).show();
            login();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHelper.close();
    }
}
编辑配置文件活动

a。从 单击搜索按钮的onClick事件以检索特定用户的配置文件 用户。B从中调用DbHandler类中实现的UpdateInner()方法 编辑按钮的onClick事件用于更新用户详细信息c。呼叫 在onClick的DbHandler类中实现的deleteinfor()方法 删除用户的删除按钮的事件

档案管理活动

package com.bla.androidsqlitebasics.activity;

import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.bla.androidsqlitebasics.database.DBHelper;
import com.live.sinhalacoder.androidsqlitebasics.R;

public class ProfileManagementActivity extends AppCompatActivity {

    EditText usernameEt, passwordEt, dobEt;
    RadioGroup genderRadio;
    Button updateProfileBt;

    //to get access to database table
    DBHelper mHelper;

    long userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile_management);

        usernameEt = findViewById(R.id.idUsernameEt);
        passwordEt = findViewById(R.id.idPasswordEt);
        dobEt = findViewById(R.id.idBirthdayEt);

        genderRadio = findViewById(R.id.radioGroup);

        updateProfileBt = findViewById(R.id.idUpdateBt);

        //initialize db helper when app create
        mHelper = new DBHelper(this);

        //get userId that is coming from the home activity
        Intent intent = getIntent();
        if (intent != null) {
            userId = intent.getLongExtra("userId", -1);
        }

        //get logged in or registered user data from table and bind to editTexts
        Cursor cursor = mHelper.readAllInfor(userId);
        if (cursor.moveToFirst()) {
            usernameEt.setText(cursor.getString(1));
            passwordEt.setText(cursor.getString(2));
            dobEt.setText(cursor.getString(3));
            if (cursor.getString(4) != null) {
                if (cursor.getString(4).equals("Male")) {
                    genderRadio.check(R.id.idMaleRadio);
                } else {
                    genderRadio.check(R.id.idFemaleRadio);
                }
            }
        }
        //if user clicked edit button
        updateProfileBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //open update profile
                Intent intent = new Intent(ProfileManagementActivity.this, EditProfileActivity.class);
                intent.putExtra("userId", userId);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHelper.close();
    }
}
这些是我的布局文件。 活动之家


您的代码似乎工作正常。但我认为你的布局可能有错误。
请确保您的布局正确。

请检查您的意图对象和布局。

您在问什么?我需要知道,我的所有代码是正确的还是错误的。我的手机坏了。我不知道是我的密码问题还是电话问题这是你的密码。放心。我不想冒犯他人,但根据我的经验,我总是做错事,而不是手机或操作系统。你需要在应用程序崩溃时发布stacktraces。哦。非常感谢。我想这是我的电话问题(我重新启动手机,但它根本没有打开。现在无法检查logcat。希望有人尝试此代码并告诉我:)非常感谢您的时间IvanI喜欢此错误布局名称中的一个出现问题。再次感谢!
package com.bla.androidsqlitebasics.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.bla.androidsqlitebasics.database.DBHelper;
import com.live.sinhalacoder.androidsqlitebasics.R;

public class HomeActivity extends AppCompatActivity {

    EditText usernameEt, passwordEt;
    Button loginBt, registerBt;

    //to get access to database table
    DBHelper mHelper;

    //newly added user primary key
    long userId = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        usernameEt = findViewById(R.id.idUsernameEt);
        passwordEt = findViewById(R.id.idPasswordEt);

        loginBt = findViewById(R.id.idLoginBt);
        registerBt = findViewById(R.id.idRegisterBt);

        //initialize db helper when app create
        mHelper = new DBHelper(this);

        //if user clicked login button
        loginBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //login existing user
                login();
            }
        });

        //if user clicked register button
        registerBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //add new user to the database
                registerUser();
            }
        });
    }

    private void login() {
        String username = usernameEt.getText().toString();
        String password = passwordEt.getText().toString();

        //if username and pass does not match -1 will return from checkUser function and if not -1 logged in
        userId = mHelper.checkUser(username, password);
        if (userId != -1) {
            Intent intent = new Intent(HomeActivity.this, ProfileManagementActivity.class);
            intent.putExtra("userId", userId);
            startActivity(intent);
        } else {
            Toast.makeText(this, "Err.. Try again!", Toast.LENGTH_SHORT).show();
        }
    }

    public void registerUser() {
        String username = usernameEt.getText().toString();
        String password = passwordEt.getText().toString();

        userId = mHelper.addInfo(username, password);
        if (userId == -1) {
            Toast.makeText(this, "Err.. Try again!", Toast.LENGTH_SHORT).show();
        } else {
            //Toast.makeText(this, "Successfully registed!", Toast.LENGTH_SHORT).show();
            login();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHelper.close();
    }
}
package com.bla.androidsqlitebasics.activity;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.bla.androidsqlitebasics.database.DBHelper;
import com.live.sinhalacoder.androidsqlitebasics.R;

public class EditProfileActivity extends AppCompatActivity {

    EditText usernameEt, passwordEt, dobEt;
    RadioGroup genderRadio;
    Button editBt, searchBt, deleteBt;

    //to get access to database table
    DBHelper mHelper;

    long userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_profile);

        usernameEt = findViewById(R.id.idUsernameEt);
        passwordEt = findViewById(R.id.idPasswordEt);
        dobEt = findViewById(R.id.idBirthdayEt);

        genderRadio = findViewById(R.id.radioGroup);

        editBt = findViewById(R.id.idEditBt);
        searchBt = findViewById(R.id.idSearchBt);
        deleteBt = findViewById(R.id.idDeleteBt);

        //initialize db helper when app create
        mHelper = new DBHelper(this);

        //TODO: get userId that is coming from the home activity to search using user Id(not sure this way or using search username)
        //If press update or delete without searching user id coming from the home activity will be deleted
        Intent intent = getIntent();
        if (intent != null) {
            userId = intent.getLongExtra("userId", -1);
        }

        //if user clicked edit button
        editBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String uname = usernameEt.getText().toString();
                String pass = passwordEt.getText().toString();
                String dob = dobEt.getText().toString();
                String gender = "";
                if (genderRadio.getCheckedRadioButtonId() == R.id.idMaleRadio) {
                    gender = "Male";
                } else if (genderRadio.getCheckedRadioButtonId() == R.id.idFemaleRadio) {
                    gender = "Female";
                }

                //edit logged in user
                if (mHelper.updateInfo(userId, uname, pass, dob, gender)) {
                    Toast.makeText(EditProfileActivity.this, "Updated!", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(EditProfileActivity.this, "Cannot update!", Toast.LENGTH_SHORT).show();
                }
            }
        });

        //if user clicked search button
        searchBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //get typed username to search
                String username = usernameEt.getText().toString();

                //get current user values to textFields from readInfo
                Cursor cursor = mHelper.readAllInfor();

                //TODO: I think this way is not the perfect since, we can directly search using the query
                while (cursor.moveToNext()) {
                    //if typed username equals with table value
                    if (username.equals(cursor.getString(1))) {
                        //get the user id to update and delete
                        userId = cursor.getLong(0);
                        //if there is any matching username with db user table get those values and place into textfields
                        passwordEt.setText(cursor.getString(2));
                        dobEt.setText(cursor.getString(3));

                        if (cursor.getString(4) != null) {
                            if (cursor.getString(4).equals("Male")) {
                                genderRadio.check(R.id.idMaleRadio);
                            } else if (cursor.getString(4).equals("Female"))
                                genderRadio.check(R.id.idFemaleRadio);
                        }
                    }
                }
                cursor.close();

                //dumb trick to display if user not exists
                if (passwordEt.getText().toString().equals("")) {
                    //if searched user not exists
                    Toast.makeText(EditProfileActivity.this, "No user found!", Toast.LENGTH_SHORT).show();
                }
            }
        });

        //if user clicked delete button
        deleteBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //delete user from table and if deleted count is greater than 0 display delete message
                if (mHelper.deleteInfo(userId)) {
                    Toast.makeText(EditProfileActivity.this, "Deleted!", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(EditProfileActivity.this, "User not in the table!", Toast.LENGTH_SHORT).show();
                }

                //clear out editText after deleted
                usernameEt.setText("");
                passwordEt.setText("");
                dobEt.setText("");
                genderRadio.clearCheck();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHelper.close();
    }
}
package com.bla.androidsqlitebasics.activity;

import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.bla.androidsqlitebasics.database.DBHelper;
import com.live.sinhalacoder.androidsqlitebasics.R;

public class ProfileManagementActivity extends AppCompatActivity {

    EditText usernameEt, passwordEt, dobEt;
    RadioGroup genderRadio;
    Button updateProfileBt;

    //to get access to database table
    DBHelper mHelper;

    long userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile_management);

        usernameEt = findViewById(R.id.idUsernameEt);
        passwordEt = findViewById(R.id.idPasswordEt);
        dobEt = findViewById(R.id.idBirthdayEt);

        genderRadio = findViewById(R.id.radioGroup);

        updateProfileBt = findViewById(R.id.idUpdateBt);

        //initialize db helper when app create
        mHelper = new DBHelper(this);

        //get userId that is coming from the home activity
        Intent intent = getIntent();
        if (intent != null) {
            userId = intent.getLongExtra("userId", -1);
        }

        //get logged in or registered user data from table and bind to editTexts
        Cursor cursor = mHelper.readAllInfor(userId);
        if (cursor.moveToFirst()) {
            usernameEt.setText(cursor.getString(1));
            passwordEt.setText(cursor.getString(2));
            dobEt.setText(cursor.getString(3));
            if (cursor.getString(4) != null) {
                if (cursor.getString(4).equals("Male")) {
                    genderRadio.check(R.id.idMaleRadio);
                } else {
                    genderRadio.check(R.id.idFemaleRadio);
                }
            }
        }
        //if user clicked edit button
        updateProfileBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //open update profile
                Intent intent = new Intent(ProfileManagementActivity.this, EditProfileActivity.class);
                intent.putExtra("userId", userId);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHelper.close();
    }
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginHorizontal="@dimen/layout_margin_horizontal"
    tools:context="com.bla.androidsqlitebasics.activity.HomeActivity">

    <TextView
        android:id="@+id/usernameLabelTv"
        android:layout_width="@dimen/label_width"
        android:layout_height="@dimen/label_height"
        android:layout_marginTop="@dimen/username_label_margin_top"
        android:text="@string/username_tv"
        android:textSize="@dimen/labels_size"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/idUsernameEt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/username_et_margin_start"
        android:layout_marginTop="@dimen/username_et_margin_top"
        android:ems="10"
        android:hint="@string/username_tv"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.384"
        app:layout_constraintStart_toEndOf="@id/usernameLabelTv"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/passwordLabelTv"
        android:layout_width="@dimen/label_width"
        android:layout_height="@dimen/label_height"
        android:layout_marginTop="@dimen/password_label_margin_top"
        android:text="@string/password_label_tv"
        android:textSize="@dimen/labels_size"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/usernameLabelTv" />

    <EditText
        android:id="@+id/idPasswordEt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/username_et_margin_start"
        android:layout_marginTop="@dimen/password_et_margin_top"
        android:ems="10"
        android:hint="@string/password_label_tv"
        android:inputType="textPassword"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.384"
        app:layout_constraintStart_toEndOf="@id/passwordLabelTv"
        app:layout_constraintTop_toBottomOf="@id/idUsernameEt" />

    <Button
        android:id="@+id/idLoginBt"
        android:layout_width="@dimen/bt_width"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/log_bt_margin_top"
        android:text="@string/login_bt_label"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/passwordLabelTv"
        app:layout_constraintVertical_bias="0.023" />

    <Button
        android:id="@+id/idRegisterBt"
        android:layout_width="@dimen/bt_width"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/username_et_margin_start"
        android:layout_marginTop="@dimen/reg_bt_margin_top"
        android:text="@string/register_bt_label"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.395"
        app:layout_constraintStart_toEndOf="@id/idLoginBt"
        app:layout_constraintTop_toBottomOf="@id/idPasswordEt"
        app:layout_constraintVertical_bias="0.028" />
    </android.support.constraint.ConstraintLayout>