Android Open Source - RealmSample Main Activity






From Project

Back to project page RealmSample.

License

The source code is released under:

Apache License

If you think the Android project RealmSample listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright 2014 Eduardo Barrenechea/*from w  ww .j ava  2s. com*/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ca.barrenechea.realmsample;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.Random;
import java.util.UUID;

import io.realm.Realm;
import io.realm.RealmChangeListener;
import io.realm.RealmResults;
import rx.Observable;
import rx.Subscriber;
import rx.schedulers.Schedulers;


public class MainActivity extends Activity implements RealmChangeListener {

    private boolean mSortOrder = RealmResults.SORT_ORDER_ASCENDING;
    private ItemAdapter mAdapter;

    private static final Random mRandom = new Random(System.currentTimeMillis());

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

        mAdapter = new ItemAdapter(this);

        final ListView list = (ListView) this.findViewById(R.id.list);
        list.setAdapter(mAdapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                final ListItem item = mAdapter.getItem(i);
                final String id = item.getId();

                Intent intent = new Intent(MainActivity.this, ItemActivity.class);
                intent.putExtra(ItemActivity.EXTRA_ID, id);

                startActivity(intent);

                // deleting in context with Rx works fine
                // comment the startActivity line and uncomment the lines bellow
//                Observable.create(
//                        new Observable.OnSubscribe<Object>() {
//                            @Override
//                            public void call(Subscriber<? super Object> subscriber) {
//                                final Realm realm = Realm.getInstance(MainActivity.this);
//
//                                realm.beginTransaction();
//                                realm.where(ListItem.class).equalTo(ListItem.ID, id).findAll().clear();
//                                realm.commitTransaction();
//
//                                subscriber.onCompleted();
//                            }
//                        })
//                        .subscribeOn(Schedulers.io())
//                        .subscribe();

                // deleting in context with AsyncTask also works fine
                // comment the startActivity line above and uncomment the lines bellow
//                new AsyncTask<Void, Void, Void>() {
//                    @Override
//                    protected Void doInBackground(Void... voids) {
//                        final Realm realm = Realm.getInstance(MainActivity.this);
//
//                        realm.beginTransaction();
//                        realm.where(ListItem.class).equalTo(ListItem.ID, id).findAll().clear();
//                        realm.commitTransaction();
//
//                        return null;
//                    }
//                }.execute();
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_new:
                Observable.create(
                        new Observable.OnSubscribe<Object>() {
                            @Override
                            public void call(Subscriber<? super Object> subscriber) {
                                final Realm realm = Realm.getInstance(MainActivity.this);
                                realm.beginTransaction();

                                ListItem item = realm.createObject(ListItem.class);
                                item.setId(UUID.randomUUID().toString());
                                item.setAttribute(mRandom.nextInt(10000));

                                realm.commitTransaction();
                            }
                        })
                        .subscribeOn(Schedulers.io())
                        .subscribe();
                return true;

            case R.id.action_sort_asc:
                item.setChecked(true);
                mSortOrder = RealmResults.SORT_ORDER_ASCENDING;
                mAdapter.sort(ListItem.ATTRIBUTE, mSortOrder);
                return true;

            case R.id.action_sort_desc:
                item.setChecked(true);
                mSortOrder = RealmResults.SORT_ORDER_DECENDING;
                mAdapter.sort(ListItem.ATTRIBUTE, mSortOrder);
                return true;

            case R.id.action_refresh:
                loadList(mSortOrder);
                return true;

            case R.id.action_clear:
                Observable.create(
                        new Observable.OnSubscribe<Object>() {
                            @Override
                            public void call(Subscriber<? super Object> subscriber) {
                                final Realm realm = Realm.getInstance(MainActivity.this);

                                realm.beginTransaction();
                                realm.where(ListItem.class).findAll().clear();
                                realm.commitTransaction();
                            }
                        })
                        .subscribeOn(Schedulers.io())
                        .subscribe();
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        Realm.getInstance(this).addChangeListener(this);

        loadList(RealmResults.SORT_ORDER_ASCENDING);
    }

    @Override
    protected void onPause() {
        super.onPause();

        Realm.getInstance(this).removeChangeListener(this);
    }

    private void loadList(boolean order) {
        final Realm realm = Realm.getInstance(MainActivity.this);
        final RealmResults<ListItem> listItems = realm.where(ListItem.class)
                .findAll()
                .sort(ListItem.ATTRIBUTE, order);
        mAdapter.setData(listItems);
    }

    @Override
    public void onChange() {
        mAdapter.notifyDataSetChanged();
    }
}




Java Source Code List

ca.barrenechea.realmsample.ItemActivity.java
ca.barrenechea.realmsample.ItemAdapter.java
ca.barrenechea.realmsample.ListItem.java
ca.barrenechea.realmsample.MainActivity.java