There is a default SQLite database on Android. We can easily use the SQLite database in our app. But the FastMaster database from SQLite is being Realm. Realm how to use the app, so let’s see.
Full documentation of the use of Realm in Android can be found here. To use the Rails Android studio, we can add gradle to the file as dependencies compile ‘io.realm: realm-android: 0.84.1’
The railway version is being updated regularly. So when you use it, you can check the version from Latest Realm. After adding the dependencies to the buildgradle file in the Android Project App folder, we can now use the Rails in our project.
We can create a user class. Simple POJO [Plain Old Java Object] Class. Such as User.java. There are two variables in it. A name and an email For the two variables we have written the Getter & Setter method.
User.java:
import io.realm.RealmObject;
public class User extends RealmObject {
private String name;
private String email;
public user () {}
public String getName () {
return name;
}
public void setName {String name} {
this.name = name;
}
public String getEmail () {
return email;
}
public void setEmail {String email} {
this.email = email;
}
}
To use the railway, we have to make an instance of our railway. And in that instance, some configurations have to be passed. What is the name of our database, etc. To do this:
realmConfig = new RealmConfiguration.Builder (this) .name (“user.realm”). build ();
realm = realm.getInstance (realmConfig);
Here the name (“user.realm”) is the name of our database. We can use any name.
Keeping the data in the railway is a Transiction. Before writing any transaction, writeTransaction (); And to finish the transition, write is to commitTransaction ();
After starting the transaction, we can tell what we will keep the data in the railway. The railway will make an automatic table for us. For example, we can write the name of the user and the email, as such:
realm.where (User.class) .findAll (). clear ();
User user1 = realm.createObject (User.class);
user1.setName (“Elon Musk”);
user1.setEmail (“elonmuskoffic@tesla.com”);
In this way we can do as much transaction as we can.
We can easily get queries from the train. For example, if you want to take all the data in the user table:
1RealmResults results = realm.where (User.class) .findAll ();
Full MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import io.realm.Realm;
import io.realm.RealmConfiguration;
import io.realm.RealmResults;
public class MainActivity extends AppCompatActivity {
private Realm realm;
private RealmConfiguration realmConfig;
private TextView mTextView;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
mTextView = (TextView) findViewById (R.id.textView);
realmConfig = new RealmConfiguration.Builder (this) .name (“user.realm”). build ();
realm = realm.getInstance (realmConfig);
realm.beginTransaction ();
// Create objects and set data
realm.where (User.class) .findAll (). clear ();
User user1 = realm.createObject (User.class);
user1.setName (“Elon Musk”);
user1.setEmail (“elonmuskoffic@tesla.com”);
User user2 = realm.createObject (User.class);
user2.setName (“Bill Gates”);
user2.setEmail (“billg@microsoft.com”);
User country3 = realm.createObject (User.class);
country3.setName (“Jeff Bezos”);
country3.setEmail (“jeff@amazon.com”);
realm.commitTransaction ();
RealmResults results =
realm.where (User.class) .findAll ();
for (User user: results) {
mTextView.append (“Name:” + user.getName () + “\ n”);
mTextView.append (“Email:” + user.getEmail () + “\ n \ n”);
We have put some data in the railway database. After reading those data, we showed them in a text view. TextView added activity_main.xml:
<_ layout_width=”wrap_content” _=”android:_” layout_height=”wrap_content” id=”@ + id / textView”>
This is! Simple. Rails can be used easily for their projects. If there is a problem to understand, Google is there
Thank you so much
No comments:
Post a Comment