Populate EditText with value from database

2019-07-15 10:09发布

问题:

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?

回答1:

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.



回答2:

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.



回答3:

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.