Thorvald
Thorvald

Reputation: 3563

Retrieving data from a database?

I have an SQLite database that is populated when the user create a profile and fill a form with his contact informations in SetUpProfile activity and then retrieve that data and show it back on UserProfile activity.

Question: How to display the profile data (name, phone, address...) in textviews ?

This is what I've done so far:

Profile class

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
}

This is the handler DBHandler class

addProfile() method:

    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() method

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 class

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

Upvotes: 0

Views: 57

Answers (2)

Thorvald
Thorvald

Reputation: 3563

This is the approach I used :

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

Upvotes: 1

Priyank Patel
Priyank Patel

Reputation: 12372

You need to create getters of your private variables of profile class. For example as below.

public String getIndustryName() {
  return _industryName;
}

Then you need to call getProfile() method from your activity class which return Profile object.

From Profile object, you can get all value and set it into relevent TextView and display. For example as below.

b_name.setText(profile.getIndustryName());

Upvotes: 0

Related Questions