Android Open Source - RealmSample Item 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   www.j  ava2 s  .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.View;
import android.widget.Button;
import android.widget.TextView;

import io.realm.Realm;
import io.realm.RealmResults;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.schedulers.Schedulers;

public class ItemActivity extends Activity {

    public static final String EXTRA_ID = "ItemActivity.Id";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.setContentView(R.layout.activity_item);

        Intent data = this.getIntent();
        final String id = data.getStringExtra(EXTRA_ID);

        final Realm realm = Realm.getInstance(this);
        ListItem item = realm.where(ListItem.class).equalTo(ListItem.ID, id).findFirst();

        final TextView textView = (TextView) this.findViewById(R.id.text_item);
        textView.setText("Random " + item.getAttribute());

        final Button delete = (Button) this.findViewById(R.id.button_delete);
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Observable.create(
                        new Observable.OnSubscribe<Object>() {
                            @Override
                            public void call(Subscriber<? super Object> subscriber) {
                                final Realm realm = Realm.getInstance(ItemActivity.this);

                                realm.beginTransaction();

                                RealmResults<ListItem> results = realm.where(ListItem.class).equalTo(ListItem.ID, id).findAll();
                                results.clear();

                                realm.commitTransaction();

                                subscriber.onCompleted();
                            }
                        })
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .doOnCompleted(new Action0() {
                            @Override
                            public void call() {
                                ItemActivity.this.finish();
                            }
                        })
                        .subscribe();

                // AsyncTask version still has the same issue as Rx
//                new AsyncTask<Void, Void, Void>() {
//                    @Override
//                    protected Void doInBackground(Void... voids) {
//                        final Realm realm = Realm.getInstance(ItemActivity.this);
//                        realm.beginTransaction();
//                        RealmResults<ListItem> results = realm.where(ListItem.class).equalTo(ListItem.ID, id).findAll();
//                        results.clear();
//                        realm.commitTransaction();
//
//                        return null;
//                    }
//
//                    @Override
//                    protected void onPostExecute(Void aVoid) {
//                        ItemActivity.this.finish();
//                    }
//
//                }.execute();

                // deleting on main thread works fine
                // comment the observable code above and uncomment the lines bellow
//                realm.beginTransaction();
//                RealmResults<ListItem> results = realm.where(ListItem.class).equalTo(ListItem.ID, id).findAll();
//                results.clear();
//                realm.commitTransaction();
//
//                finish();
            }
        });
    }
}




Java Source Code List

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