从数据库中检索数据?

时间:2018-03-10 12:43:40

标签: java android database sqlite android-sqlite

我有一个SQLite数据库,当用户创建个人资料并使用SetUpProfile活动中的联系人信息填充表单时填充该数据库,然后检索该数据并将其显示回UserProfile活动。< / p>

问题:如何在textviews中显示个人资料数据(姓名,电话,地址...)?

这是我到目前为止所做的:

个人资料类

public class Profile {
private int _id = 1;
private String _industryName;
private String _industryType;
private String _email;
private String _phone;
private String _address;
private String _packageType;

public Profile() {
    /** stays empty */
}

public Profile(int _id, String _industryName, String _industryType, String _email, String _phone, String _address, String _packageType) {
    this._id = _id;
    this._industryName = _industryName;
    this._industryType = _industryType;
    this._email = _email;
    this._phone = _phone;
    this._address = _address;
    this._packageType = _packageType;
}

public Profile(String _industryName, String _industryType, String _email, String _phone, String _address, String _packageType) {
    this._industryName = _industryName;
    this._industryType = _industryType;
    this._email = _email;
    this._phone = _phone;
    this._address = _address;
    this._packageType = _packageType;
}
   //getters and setters
}

这是处理程序 DBHandler类

addProfile()方法:

    public void addProfile(Profile profile) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_INDUSTRY_NAME, profile.get_industryName());
    values.put(KEY_INDUSTRY_TYPE, profile.get_industryType());
    values.put(KEY_EMAIL, profile.get_email());
    values.put(KEY_PHONE, profile.get_phone());
    values.put(KEY_ADDRESS, profile.get_address());
    values.put(KEY_PACKAGE_TYPE, profile.get_packageType());

    // Inserting Row
    db.insert(TABLE_PROFILE, null, values);
    db.close();
}

getProfile()方法

public Profile getProfile(int id) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE_PROFILE, new String[] {
            KEY_ID, KEY_INDUSTRY_NAME, KEY_INDUSTRY_TYPE, KEY_EMAIL, KEY_PHONE, KEY_ADDRESS, KEY_PACKAGE_TYPE }, KEY_ID + "=?",
            new String[] { String.valueOf(id) }, null, null, null, null);
    if (cursor != null)
        cursor.moveToFirst();

    Profile profile = new Profile(
            Integer.parseInt(cursor.getString(0)),
            cursor.getString(1),
            cursor.getString(2),
            cursor.getString(3),
            cursor.getString(4),
            cursor.getString(5),
            cursor.getString(6)
    );

    return profile;
    }

MainActivity类

public class UserProfile extends Activity {

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

    //SQLite databes is supposed to populate those textViews
    industry_type = (TextView) findViewById(R.id.b_industry);
    b_name = (TextView) findViewById(R.id.b_industry_name);
    b_email = (TextView) findViewById(R.id.mail);
    b_phone = (TextView) findViewById(R.id.phone);
    b_address = (TextView) findViewById(R.id.address);
    plan_type = (TextView) findViewById(R.id.p_title);

    edit_profile = (Button) findViewById(R.id.editProfile);

    industry_img = (ImageView) findViewById(R.id.thumbnail);
    plan_img = (ImageView) findViewById(R.id.plan_icon);


    edit_profile.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            EditProfile();
        }
    });
}

2 个答案:

答案 0 :(得分:1)

这是我使用的方法:

SQLiteDatabase db = null;
DBHandler dd = new DBHandler(getBaseContext());
dd.getWritableDatabase();
db= openOrCreateDatabase("profileManager.db", Context.MODE_PRIVATE, null);
Cursor cc = db.rawQuery("SELECT * FROM profile", null);
if(cc.getCount()>=1) {
    cc.moveToFirst();
    try {
        for(int i=0;i < cc.getCount();i++){
            bname = cc.getString(1);
            btype = cc.getString(2);
            bmail = cc.getString(3);
            bphone = cc.getString(4);
            baddress = cc.getString(5);
            bpackage = cc.getString(6);
        }
    }catch (Exception e){
        e.getMessage();
    }
}

答案 1 :(得分:0)

您需要创建配置文件类的私有变量的getter。例如,如下所示。

public String getIndustryName() {
  return _industryName;
}

然后,您需要从返回getProfile()对象的活动类中调用Profile方法。

Profile对象,您可以获取所有值并将其设置为相关TextView并显示。例如,如下所示。

b_name.setText(profile.getIndustryName());