Grabbing information from an SQLite Database: Filling in already filled fields and updating row
checked lots of other questions and websites, couldn't really find what I was looking for. I'm making a contact list for practice... what I'm trying to do is give the user the ability to update the contact's info. When they click on a contact, the fields should be filled in as they already are, not be blank with a Hint. Problem now is that the fields aren't populated, everything else works just fine but all the fields just have hints instead of the text the user filled in for the contact upon creation. Here is the code:
ContacList.class's code...
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
mDbAdapter = new TagDBAdapter(this);
mDbAdapter.open();
Cursor cursor = mCursor;
cursor.moveToPosition(position);
Intent intent = new Intent(this, AddEditContact.class);
intent.putExtra(TagDBAdapter.KEY_ROWID, id);
startActivityForResult(intent, ACTIVITY_EDIT);
}
Code for the AddEditContact.class
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.initLayout();
mDbAdapter = new TagDBAdapter(this);
mDbAdapter.open();
Bundle extras = getIntent().getExtras();
mRowId = extras.getLong(TagDBAdapter.KEY_ROWID);
if(mRowId != null) {
mContact = mDbAdapter.fetchContact(mRowId);
m_firstNameEditE.setText(mContact.getFirstName());
m_lastNameEditE.setText(mContact.getLastName());
m_phoneNumberEdit.setText(mContact.getPhoneNumber());
m_emailEditE.setText(mContact.getEmail());
m_homePhoneEditE.setText(mContact.getHomePhone());
m_workPhoneEditE.setText(mContact.getWorkPhone());
}
}
Fetch contact code
Here is the code for fetchContact()
public Contact fetchContact(long rowId) throws SQLException {
Contact contact = new Contact();
Cursor mCursor =
mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID, KEY_FIRSTNAME, KEY_LASTNAME, KEY_NAME, KEY_NUMBER,
KEY_EMAIL, KEY_HOMEPHONE, KEY_WORKPHONE}, KEY_ROWID + "=" + rowId, null, null, null, null, null);
if(mCursor != null) {
mCursor.moveToFirst();
}
while(mCursor.moveToNext()) {
contact.setFirstName(mCursor.getString(mCursor.getColumnIndexOrThrow(KEY_FIRSTNAME)));
contact.setLastName(mCursor.getString(mCursor.getColumnIndexOrThrow(KEY_LASTNAME)));
contact.setName(mCursor.getString(mCursor.getColumnIndexOrThrow(KEY_NAME)));
contact.setPhoneNumber(mCursor.getString(mCursor.getColumnIndexOrThrow(KEY_NUMBER)));
contact.setEmail(mCursor.getString(mCursor.getColumnIndexOrThrow(KEY_EMAIL)));
contact.setHomePhone(mCursor.getString(mCursor.getColumnIndexOrThrow(KEY_HOMEPHONE)));
contact.setWorkPhone(mCursor.getString(mCursor.getColumnIndexOrThrow(KEY_WORKPHONE)));
}
return contact;
}
So I'm getting the extras from the intent and getting the KEY_ROWID, and if it isn't null, setting the text of the EditTexts to the getters for the contact object I got from the fetchContact(long rowId) class, which returns a Contact object. Then I set all the EditTexts to their respective getters. But when in action, like I said, it just leaves all the fields blank, with just a Hint there.
Is there something I'm missing here? Thanks a lot.
One potential problem I thought of was that I have two intents which ask for a result from this activity, one 开发者_高级运维for ACTIVITY_CREATE and one for ACTIVITY_EDIT. Maybe its getting the wrong intent.
Have you tried debugging or at least putting some Log.d() in your code to print out the values?
Is your mDbAdapter written by you? Maybe there is something wrong with the fetchContact()?
精彩评论