java 令人敬畏的境界 - MainActivity(2)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 令人敬畏的境界 - MainActivity(2)相关的知识,希望对你有一定的参考价值。

package net.myanmarlinks.awesomerealm;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import net.myanmarlinks.awesomerealm.model.Author;
import net.myanmarlinks.awesomerealm.model.Category;
import net.myanmarlinks.awesomerealm.model.Post;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import io.realm.Realm;

public class MainActivity extends AppCompatActivity {

    private Realm realm;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        realm = Realm.getDefaultInstance();

        List<Category> categories = new ArrayList<>();
        List<Author> authors = new ArrayList<>();

        categories.add(new Category(getUniqueID(), "News"));
        categories.add(new Category(getUniqueID(), "IT"));
        categories.add(new Category(getUniqueID(), "Breaking News"));

        authors.add(new Author(getUniqueID(), "Aung Aung"));
        authors.add(new Author(getUniqueID(), "Baung Baung"));
        authors.add(new Author(getUniqueID(), "Hla Hla"));

        realm.beginTransaction();
        realm.copyToRealm(categories);
        realm.copyToRealmOrUpdate(authors);
        realm.commitTransaction();

        Category category = realm.where(Category.class).equalTo("id", categories.get(0).getId()).findFirst();
        Author author = realm.where(Author.class).equalTo("id", authors.get(0).getId()).findFirst();


        realm.beginTransaction();
        Post post = realm.createObject(Post.class, getUniqueID());
        post.setTitle("Hello World");
        post.setCategory(category);
        post.setAuthor(author);
        realm.commitTransaction();

        List<Post> posts = realm.where(Post.class).equalTo("author.id", author.getId()).findAll();
        Log.d("POSTS", posts.get(0).getCategory().getName());
    }

    private String getUniqueID() {
        return UUID.randomUUID().toString();
    }


}

以上是关于java 令人敬畏的境界 - MainActivity(2)的主要内容,如果未能解决你的问题,请参考以下文章

java 令人敬畏的境界 - 发布

java 令人敬畏的境界 - 作者

令人敬畏的jQuery步骤向导插件

text 令人敬畏的蟒蛇

text 令人敬畏的反应组件

markdown 令人敬畏的名单