从数据库中检索数据?

托瓦尔德·奥拉夫森

我有一个 SQLite 数据库,当用户创建配置文件并在SetUpProfile活动中使用他的联系信息填写表单时填充该数据库,然后检索该数据并将其显示回UserProfile活动中。

问题:如何在 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;
    }

主活动类

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();
        }
    });
}
托瓦尔德·奥拉夫森

这是我使用的方法:

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();
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

java如何从数据库中检索数据

来自分类Dev

从Laravel中的数据库检索对象

来自分类Dev

如何从jsp文件中从数据库检索数据?

来自分类Dev

如何在QML中从数据库检索数据?

来自分类Dev

使用实体框架从数据库中检索数据

来自分类Dev

从数据库中检索多个图像

来自分类Dev

如何从数据库中检索数据?

来自分类Dev

从数据库表中检索数据以填充链接

来自分类Dev

尝试从数据库中的字符串检索数据

来自分类Dev

使用foreach从PHP中的数据库检索数据

来自分类Dev

从数据库中检索多个数据

来自分类Dev

从数据库中的对象检索列表

来自分类Dev

登录后从mysql数据库中检索数据

来自分类Dev

如何在Wordpress数据库中检索保存的数据

来自分类Dev

如何从sqlite数据库检索数据并显示在片段中

来自分类Dev

如何从数据库中检索样本?

来自分类Dev

如何使用django从mongodb数据库中检索数据?

来自分类Dev

java如何从数据库中检索数据

来自分类Dev

从Laravel中的数据库检索对象

来自分类Dev

如何从数据库中检索数据?

来自分类Dev

在表中安排从数据库中检索到的数据

来自分类Dev

从数据库中检索下拉选项

来自分类Dev

如何从数据库中检索标签?

来自分类Dev

在UWP中从数据库检索数据

来自分类Dev

无法从数据库中检索数据

来自分类Dev

从数据库中检索值

来自分类Dev

如何从数据库中检索资源?

来自分类Dev

从数据库中检索数据的问题

来自分类Dev

从数据库中检索单个 ROW