Populate EditText with value from database

2019-07-15 09:20发布

In my Android application I have a TextView with an EditText. I want to prepopulate the EditText with the last value entered, which is stored in the SQLite database?

3条回答
forever°为你锁心
2楼-- · 2019-07-15 10:03

I have done this By using Spinner inside EditText.

Take EditText and Spinner inside a RelativeLayout retrieve data from DB and assign those values to the spinner.

By using OnItemSelectedListener to the spinner u can assign the selected values to ur EdiText.

查看更多
狗以群分
3楼-- · 2019-07-15 10:09

This should not be so hard I think:

public void onCreate(Bundle savedState) {
    EditText editText = (EditText) findViewById(R.id.my_edittext);
    String value = // Query your 'last value'.
    editText.setText(value);
}

If you want to learn more about SQLite database storage, read this article in Android document.

查看更多
放荡不羁爱自由
4楼-- · 2019-07-15 10:22

Assign an edittext to it's resource ID via getViewById(R.id.xxxx)

then do a SQLite query for the highest PID...

then pull theString you're looking for out of the result cursor...

assign that via editTextId.setText(theString);

be sure to enclose it in a try/catch and assign the edittext to some arbitrary default value if the db exec fails due to no entries in the table.

查看更多
登录 后发表回答