Realm database in Loklak Wok Android for Persistent view

Loklak Wok Android provides suggestions for tweet searches. The suggestions are stored in local database to provide a persistent view, resulting in a better user experience. The local database used here is Realm database instead of sqlite3 which is supported by Android SDK. The proper way to use an sqlite3 database is to first create a contract where the schema of the database is defined, then a database helper class which extends from SQLiteOpenHelper class where the schema is created i.e. tables are created and finally write ContentProvider so that you don’t have to write long SQL queries every time a database operation needs to be performed. This is just a lot of hard work to do, as this includes a lot of steps, debugging is also difficult. A solution to this can be using an ORM that provides a simple API to use sqlite3, but the currently available ORMs lack in terms of performance, they are too slow. A reliable solution to this problem is realm database, which is faster than raw sqlite3 and has really simple API for database operations. This blog explains the use of realm database for storing tweet search suggestions. Adding Realm database to Android project In project level build.gradle buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.3.3' classpath "io.realm:realm-gradle-plugin:3.3.1" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } }   And at the top of app/build.gradle "apply plugin: 'realm-android'"  is added. Using Realm Database Let’s start with a simple example. We have a Student class that has only two attributes name and age. To create the model for the database, the Student class is simply extended to RealmObject. public class Student extends RealmObject { private String name; private int age; // A constructor needs to be explicitly defined, be it an empty constructor public Student(String name, int age) { this.name = name; this.age = age; } // getters and setters }   To push data to the database, Java objects are created, a transaction is initialized, then copyToRealm method is used to push the data and finally the transaction is committed. But before all this, the database is initialized and a Realm instance is obtained. Realm.init(context); // Database initialized Realm realm = Realm.getDefaultInstance(); // realm instance obtained Student student = new Student("Rahul Dravid", 22); // Simple java object created realm.beginTransaction() // initialization of transaction realm.copyToRealm(student); // pushed to database realm.commitTransaction(); // transaction committed   copyToRealm takes only a single parameter, the parameter can be an object or an Iterable. Off course, the passed parameter should extend RealmObject. A List of Student can be passed as a parameter to copyToRealm to push multiple data into the database. The above way of inserting data is synchronous. Realm also supports asynchronous transactions, you guessed it right, you don’t have to depend on AsyncTaskLoader. The same operation can be performed asynchronously as realm.executeTransaction(new Realm.Transaction() { @Override public void execute(Realm realm) { Student student = new Student("Rahul Dravid",…

Continue ReadingRealm database in Loklak Wok Android for Persistent view

Storing a Data List in Phimpme Android

In Phimpme Android, it is required to store all the available camera parameters like a list of ISO values, available camera resolution etc. so that it can be displayed to the user in the camera settings. In Phimpme, we have stored these list of data in SharedPreferences with some modifications. As we cannot store a list directly in SharedPreference, in this post I will be discussing how we achieved this in Phimpme Android application. To store the ArrayList you have to create a function that will convert the array into a string by using some symbol. Step - 1 First, Create a class say TinyDB which contains functions to store an array in sharedPreferences. public class TinyDB { private SharedPreferences preferences; public TinyDB(Context appContext) { preferences = PreferenceManager.getDefaultSharedPreferences(appContext); } } Step - 2 Create functions to convert the array into string and store in sharedPreferences. putListInt() method will convert the string ArrayList to String and store in sharedPreferences. Similarly, putListString() method will convert the integer ArrayList to string and store in sharedPreferences. public void putListInt(String key, ArrayList<Integer> intList) {   if (key == null) return;   if (intList==null) return;   Integer[] myIntList = intList.toArray(new Integer[intList.size()]);   preferences.edit().putString(key, TextUtils.join("‚‗‚", myIntList)).apply(); }    public void putListString(String key, ArrayList<String> stringList) {   if (key == null) return;   if (stringList ==null)return;   String[] myStringList = stringList.toArray(new String[stringList.size()]);   preferences.edit().putString(key, TextUtils.join("‚‗‚", myStringList)).apply(); }     Now create the object of TinyDB.class to call the above functions using tinyDb object. Now our data is saved in sharedPreference to get this data we have to create a getter for the ArrayList.   Step-3 Add two functions in TinyDB.class to get the string and integer ArrayList. public ArrayList<String> getListString(String key) {         return new ArrayList<String>(Arrays.asList(TextUtils.split(preferences. =getString(key, ""), "‚‗‚"))); } public ArrayList<Integer> getListInt(String key) {   String[] myList = TextUtils.split(preferences.getString(key, ""), "‚‗‚");   ArrayList<String> arrayToList = new ArrayList<String>(Arrays.asList(myList));   ArrayList<Integer> newList = new ArrayList<Integer>();   for (String item : arrayToList)       newList.add(Integer.parseInt(item));   return newList; } Now to get the saved integer and string ArrayList simply call this function by creating an instance of TinyDB.class. The below screenshot depicts how we have stored the list of camera resolutions in SharedPreference using TinyDB class. So this is how you can store the entire ArrayList in sharedPreferences. For more detail, you can see the TinyDb.class in our Phimpme project. Resources:   https://stackoverflow.com/questions/7057845/save-arraylist-to-sharedpreferences http://blog.nkdroidsolutions.com/arraylist-in-sharedpreferences/ http://findnerd.com/list/view/Save-ArrayList-of-Object-into-Shared-Preferences-in-Android/510?page=10&ppage=3 https://github.com/fossasia/phimpme-android/blob/development/app/src/main/java/org/fossasia/phimpme/opencamera/Camera/TinyDB.java    

Continue ReadingStoring a Data List in Phimpme Android