MovieBazar.Info

Latest News & Reviews

Realm Mobile Database For Android Application Database . A Better Choice For Developers .

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

1
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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:

1
2
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

1
2
3
4
realm.where (User.class) .findAll (). clear ();
User user1 = realm.createObject (User.class);
user1.setName (“Elon Musk”);
user1.setEmail (“[email protected]”);

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:

1
RealmResults results = realm.where (User.class) .findAll ();
Full MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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 (“[email protected]”);

User user2 = realm.createObject (User.class);
user2.setName (“Bill Gates”);
user2.setEmail (“[email protected]”);

User country3 = realm.createObject (User.class);
country3.setName (“Jeff Bezos”);
country3.setEmail (“[email protected]”);

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

<_ 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

MovieBazar.Info © 2018