How to populate UI from data in a database? - java

I currently have a UI for users to select reminders on Activity A. These reminder attributes are saved in a database, and the reminder is populated into a listview on Activity B. How would I go about selecting a reminder item on the listview on Activity B, grabbing this data from the database, and populating the UI of Activity A with the data that was previously selected.
Is there a standard to do something like this? Should I create the selections as a serialized object to be saved in the database and then use this data? Or should I just save each attribute as a field in the database and have each saved item as a field?
I am pretty new to this stuff, so I am not sure if I should go with a database, shared preference or content provider.

One approach is to use a Content Provider, which provides a consistent point of access to your database. With a ContentProvider, you can take advantage of the Loader framework (also available via the Support Library), CursorLoader, and CursorAdapter to keep your ListView in sync with your database.

Related

Sqlite or sharedpreference is suitable for my application

I have an android app with user login i need to store the login details in my app.
My app also has some user history of certain request made by him/her.
My app is a software service app so user details must be stored in app which is fetched from the database
Is Sqlite or sharedpreference suitable for my application?
Depends, If you only want to save sessions of user use SharedPrefrence or else if the data is more you can use sqlite
/**
* method to set the login status for the application
*
* #param context
* #param status
*/
public static void setLoginStatus(Context context, int status) {
sharedPreferences = context.getSharedPreferences(PREFERENCE_NAME, Activity.MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
editor.putInt(KEY_LOGIN_STATUS, status);
editor.apply();
}
NOTE :
About that its totally on your choice what you use to save the current
state of user , i will prefer using shared preferences. And for signup
and sign in use SQLite database. Shared preferences are commonly used
to perform light operations. You can search through data in SQLite as
you may have many users.
So use SQLite to register and login and then use shared preferences to
save tha current state of user.
Sqlite is always a better option to store the data there you can manage in proper format and you can encrypt as well.
I would suggest, you should use sharedPreferences for saving the login details and i suppose whatever request you are storing, contains more data, so save your requests in SQLite.
Here is some good details about theme :
Pros and Cons of SQLite and Shared Preferences
SQLite
Large amounts of same structured data should be stored in a SQLite
database as databases are designed for this kind of data. As the data
is structured and managed by the database, it can be queried to get a
sub set of the data which matches certain criteria using a query
language like SQL. This makes it possible to search in the data. Of
course managing and searching large sets of data influences the
performance so reading data from a database can be slower than reading
data from SharedPreferences.
SharedPreferences
SharedPreferences is a key/value store where you can save a data under
certain key. To read the data from the store you have to know the key
of the data. This makes reading the data very easy. But as easy as it
is to store a small amount of data as difficult it is to store and
read large structured data as you need to define key for every single
data, furthermore you cannot really search within the data except you
have a certain concept for naming the keys.
According to the Size or Format of your data you can choose one, for login information I suggest using SharedPreferences

How to bind SQLite data to textview in Android

I'm looking for the simplest way to bind a value in a SQLite column to a textview. I have about a hundred textviews across a number of activities.
I have a DBHelper class to define the table and columns and a SQLController class to define my methods to insert, get and update data. All of this is pretty much boilerplate code.
For each activity I open the database, set my cursor and get the data, then update it back to the database when the activity closes.
Is there a library I can use to bind the fields in my db to the textview directly? I currently use butterknife to bind the data objects to the views and am considering implementing android data binding, but I was wondering if there's a way to go directly from the database to the view without having to write all this boilerplate code?

Storing custom configuration in android

I am creating an android application and i need to manage how to display data of list view in some fragment .. My list view consists of multiple sections with a header on each section.. The data of these sections are loaded from a web service(web call for each section) and i need to manage which sections to display to the user and also sort the sections by priority so that the important sections are displayed at the top of the view.
My question is how can i make this configurable and loaded once in my application so that when getting the data i can construct the list view properly using an already loaded configuration, i thought of using SharedPreferences or maybe an XML file as a configuration storage.
Again, my interests are to:
Manage which sections to be viewed in my list view. (for all users and not per user)
Sort the sections by priorities.
Thanks in advance for the help.
I believe you should have a database and store the settings for each user there. Based on data stored in the database you can generate XML, if needed. This would be very useful, as if you open the application with another android phone, but log in with your user, your settings would be loaded as well.

How to save data between activity change and on app startup restore old data?

I have a tableLayout in which a user can dynamicaly add rows during run time. But when I change the activity and then switch back to it all data is lost.
In my class I have an arraylist which I fill with data in onCreate().
I am thinking of #Overriting the onSaveInstanceState() and I am thinking on adding to the savedInstanceState Bundle the arraylist but there is no putArrayList.
After that i will call it in onRestore by overriting it and with a for loop fill again the rows that with data from the ArrayList
Please give me an opinion how to do this?
There are multiple ways in fact.
Choose an other way of storing you data (parcelable)
Go for static, see Here
Save data to SQLite
Save data to SharedPrefs (e.g. Settings). Although this might not be the perfect way for ArrayLists.

Retrieve TextView attributes from SQLite db

I have 10 TextViews, their text and visibility is stored in an SQLite db. When an activity loads, I need to grab these values and apply them to the TextViews.
My db is currently structured as: _id, textviewID, text, visibility. What would be the best way to access these values and apply them to the correct TextView?
Take a look at this article, this post, and of course the Android Developer's topic on how to retirieve values from an sqlite db.
For these kind of configurations SharedPreferences are way more handy solution. If your design is not strict to use the local database for storing application preferences, you should consider it as an option.

Categories