Search in sources :

Example 1 with RealmSampleUserItem

use of com.mikepenz.fastadapter.app.items.RealmSampleUserItem in project FastAdapter by mikepenz.

the class RealmActivity method onCreate.

@Override
protected void onCreate(final Bundle savedInstanceState) {
    findViewById(android.R.id.content).setSystemUiVisibility(findViewById(android.R.id.content).getSystemUiVisibility() | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Handle Toolbar
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle(R.string.sample_realm_list);
    //style our ui
    new MaterializeBuilder().withActivity(this).build();
    //create our FastAdapter which will manage everything
    mFastItemAdapter = new FastItemAdapter<>();
    //configure our fastAdapter
    mFastItemAdapter.withOnClickListener(new FastAdapter.OnClickListener<RealmSampleUserItem>() {

        @Override
        public boolean onClick(View v, IAdapter<RealmSampleUserItem> adapter, RealmSampleUserItem item, int position) {
            Toast.makeText(v.getContext(), item.getName(), Toast.LENGTH_SHORT).show();
            return false;
        }
    });
    //get our recyclerView and do basic setup
    RecyclerView rv = (RecyclerView) findViewById(R.id.rv);
    rv.setLayoutManager(new LinearLayoutManager(this));
    rv.setItemAnimator(new AlphaInAnimator());
    rv.setAdapter(mFastItemAdapter);
    //Get a realm instance for this activity
    mRealm = Realm.getDefaultInstance();
    //Add a realm on change listener (donĀ“t forget to close this realm instance before adding this listener again)
    mRealm.where(RealmSampleUserItem.class).findAllAsync().addChangeListener(new RealmChangeListener<RealmResults<RealmSampleUserItem>>() {

        @Override
        public void onChange(RealmResults<RealmSampleUserItem> userItems) {
            //This will call twice
            //1.) from findAllAsync()
            //2.) from createData()
            mFastItemAdapter.setNewList(userItems);
        }
    });
    //fill with some sample data
    createData();
    //set the back arrow in the toolbar
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(false);
    //restore selections (this has to be done after the items were added
    mFastItemAdapter.withSavedInstanceState(savedInstanceState);
}
Also used : LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) MaterializeBuilder(com.mikepenz.materialize.MaterializeBuilder) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View) AlphaInAnimator(com.mikepenz.itemanimators.AlphaInAnimator) RealmSampleUserItem(com.mikepenz.fastadapter.app.items.RealmSampleUserItem) RecyclerView(android.support.v7.widget.RecyclerView) FastAdapter(com.mikepenz.fastadapter.FastAdapter) Toolbar(android.support.v7.widget.Toolbar) RealmResults(io.realm.RealmResults)

Example 2 with RealmSampleUserItem

use of com.mikepenz.fastadapter.app.items.RealmSampleUserItem in project FastAdapter by mikepenz.

the class RealmActivity method onOptionsItemSelected.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    //handle the click on the back arrow click
    switch(item.getItemId()) {
        case android.R.id.home:
            onBackPressed();
            return true;
        case R.id.item_add:
            mRealm.where(RealmSampleUserItem.class).findAllAsync().addChangeListener(new RealmChangeListener<RealmResults<RealmSampleUserItem>>() {

                @Override
                public void onChange(RealmResults<RealmSampleUserItem> userItems) {
                    //Remove the change listener
                    userItems.removeChangeListener(this);
                    //Store the primary key to get access from a other thread
                    final long newPrimaryKey = userItems.last().getIdentifier() + 1;
                    mRealm.executeTransactionAsync(new Realm.Transaction() {

                        @Override
                        public void execute(Realm realm) {
                            RealmSampleUserItem newUser = realm.createObject(RealmSampleUserItem.class);
                            newUser.withName("Sample Realm Element " + newPrimaryKey).withIdentifier(newPrimaryKey);
                        }
                    });
                }
            });
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
Also used : RealmSampleUserItem(com.mikepenz.fastadapter.app.items.RealmSampleUserItem) Realm(io.realm.Realm) RealmResults(io.realm.RealmResults)

Aggregations

RealmSampleUserItem (com.mikepenz.fastadapter.app.items.RealmSampleUserItem)2 RealmResults (io.realm.RealmResults)2 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 RecyclerView (android.support.v7.widget.RecyclerView)1 Toolbar (android.support.v7.widget.Toolbar)1 View (android.view.View)1 FastAdapter (com.mikepenz.fastadapter.FastAdapter)1 AlphaInAnimator (com.mikepenz.itemanimators.AlphaInAnimator)1 MaterializeBuilder (com.mikepenz.materialize.MaterializeBuilder)1 Realm (io.realm.Realm)1