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);
}
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);
}
}
Aggregations