简体   繁体   中英

Someone knows how to use card views with Realm?

I want to use card views in my app but I'm using Realm database, I really appreciate a brief explanation or an example. Thank you :)

Card View is User Interface that you use to show your content to the the user. For example, A card containing, the name and age of the user, image of the user etc. Whereas Realm is a mobile database, which stores the actual values of the user like name, age, path of the image and so on.

So, you use Card View to display the values and Realm to store the actual values.

More read up on Card View can be done here

and read up on Realm can be from here

public class User extends RealmObject {

    @PrimaryKey
    private String id;
    private String firstName;
    private String lastName;
    private int age;

public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

public String getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

In Your activity, You can store values to the Realm object like below,

User user = new User()
user.setFirstName("John")
user.setLastName("Kennedy")
user.setAge(40)

Realm realm = Realm.getInstance(this)
realm.executeTransaction {
                realm.copyToRealmOrUpdate(user)
            }
//Below is written in Kotlin language. You can find similar one in Java from the link given
val userJohn: User = realm.where(User::class.java)?.equalTo("firstName", "John").findFirst()

//Values of User John can be accessed like below
println(userJohn.firstName)
println(userJohn.lastName)
println(userJohn.age)

The above is Realm Example.

Below are some of the card view examples

http://code.tutsplus.com/tutorials/getting-started-with-recyclerview-and-cardview-on-android--cms-23465

http://javatechig.com/android/android-cardview-example

http://www.truiton.com/2015/03/android-cardview-example/

Because Realm makes the Objects for you, you simply use the getter and setter methods to populate the views on your cards. This would be done with the Adapter associated with your RecyclerView, or whatever List type View you are using.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM