如何从意图中获取数据并将其存储在sqlite数据库中?

时间:2020-08-07 02:23:09

标签: android sqlite

我是android开发的新手,所以我在我的项目中遇到了这个问题。

在第一个活动中,我首先验证电话号码,然后在验证该号码后(如果用户是新用户)将其发送到注册活动。在注册表单上,我不想输入电话字段,而我的想法是直接从第一个意图中获取电话号码,并将其存储在我创建用户表以存储注册后用户数据的数据库中表格和电话(在同一表格中)。

我已经创建了sqlite数据库助手类和一个用户模型类(构造函数,getter和setters)

如何在用户模型类对象中插入或传递phone字段,并将用户表与注册表单中的其他数据一起存储在数据库中? 我尝试获取电话意图,并在调用UserModel类时将其作为参数传递,但是它不起作用。还是我做错了什么。 请帮忙!预先感谢

UserModel Class

package com.example.notes;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class SignUpActivity extends AppCompatActivity {

    EditText st_name, sc_name, board, grade, location;
    Button schoolBtn;

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

        st_name = findViewById(R.id.studentId);
        sc_name = findViewById(R.id.schoolId);
        board = findViewById(R.id.boardId);
        grade = findViewById(R.id.gradeId);
        location = findViewById(R.id.locationId);
        schoolBtn = findViewById(R.id.schoolButton);

        schoolBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                final String phone = getIntent().getStringExtra("mob");

                UserModel userModel = new UserModel();

                try {

                    userModel = new UserModel(-1,
                            st_name.getText().toString(),
                            sc_name.getText().toString(),
                            phone,
                            board.getText().toString(),
                            grade.getText().toString(),
                            location.getText().toString());
                    Toast.makeText(SignUpActivity.this,
                            userModel.toString(),
                            Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                    Toast.makeText(SignUpActivity.this, "Error adding customer", Toast.LENGTH_SHORT).show();
                }

                DatabaseHelper databaseHelper = new DatabaseHelper(SignUpActivity.this);
                boolean success = databaseHelper.addUser(userModel);
                Toast.makeText(SignUpActivity.this,
                        "Success= " + success,
                        Toast.LENGTH_SHORT).show();

                Intent profile_intent = new Intent(SignUpActivity.this,
                        ProfileActivity.class);
                startActivity(profile_intent);
            }
        });
    }
}

DatabaseHelper class
package com.example.notes;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

public class DatabaseHelper extends SQLiteOpenHelper {

    public static final String COL_SCHOOL_TABLE = "SCHOOL_TABLE";
    public static final String COL_ID = "ID";
    public static final String COL_STUDENT_NAME = "STUDENT_NAME";
    public static final String COL_SCHOOL_NAME = "SCHOOL_NAME";
    public static final String COL_PHONE = "PHONE";
    public static final String COL_BOARD = "BOARD";
    public static final String COL_GRADE = "GRADE";
    public static final String COL_LOCATION = "LOCATION";

    public DatabaseHelper(@Nullable Context context) {
        super(context, "Users.db", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String executeStatement = " CREATE TABLE " + COL_SCHOOL_TABLE + " (" + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_STUDENT_NAME + " TEXT, " + COL_SCHOOL_NAME + " TEXT, " + COL_PHONE + " TEXT, " + COL_BOARD + " TEXT, " + COL_GRADE + " TEXT, " + COL_LOCATION + " TEXT)";
        db.execSQL(executeStatement);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
        db.execSQL(" DROP TABLE IF EXISTS " + COL_SCHOOL_TABLE);
        onCreate(db);
    }

    public boolean addUser(UserModel userModel){
            SQLiteDatabase db = this.getWritableDatabase();
            ContentValues cv = new ContentValues();

            cv.put(COL_STUDENT_NAME, userModel.getName());
            cv.put(COL_SCHOOL_NAME, userModel.getSchoolName());
            cv.put(COL_PHONE, userModel.getPhone());
            cv.put(COL_BOARD, userModel.getBoard());
            cv.put(COL_GRADE, userModel.getGrade());
            cv.put(COL_LOCATION, userModel.getLocation());

            long insert = db.insert(COL_SCHOOL_TABLE, null, cv);

            if (insert == -1)
                return false;
            else
                return true;
    }

    public boolean checkPhone(String mobile) {
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "SELECT PHONE FROM SCHOOL_TABLE";
        Cursor cur = db.rawQuery(query, null);
        cur.moveToFirst();
        if (mobile == cur.getString(0))
            return true;
        else
            return false;
    }
}

1 个答案:

答案 0 :(得分:0)

因此,根据您的问题,您希望将电话号码从第一个活动传递到注册活动,您可以在Intent的帮助下简单地传递它。

在验证后的第一个活动中,只需添加此内容

Intent i = new Intent(FirstActivity.this, SignupActivity.class);
i.putExtra("phone",phone.getText().toString().trim()); //add trim at back coz somtime it happen it takes the blank space also
startActivity(i);

在SignUp活动中,首先获取捆绑软件,然后检查捆绑软件是否为null或不是这样。

Bundle bundle = getIntent().getExtras();
if(bundle != null){
String phoneNumber = bundle.getString("phone");
}

检查来自先前活动的捆绑软件是否为空是一种很好的做法。如果我们不检查这种情况,并且如果bundle为null,则您的应用程序在加载此页面时不会崩溃。

作为参考,您可以使用此链接Pass value from one activity to another activity in android