|
| 1 | +package me.karishnu.simplenotes; |
| 2 | + |
| 3 | +import android.content.Intent; |
| 4 | +import android.os.Bundle; |
| 5 | +import android.support.design.widget.FloatingActionButton; |
| 6 | +import android.support.design.widget.Snackbar; |
| 7 | +import android.support.v7.app.AppCompatActivity; |
| 8 | +import android.support.v7.widget.Toolbar; |
| 9 | +import android.view.View; |
| 10 | +import android.view.Menu; |
| 11 | +import android.view.MenuItem; |
| 12 | + |
| 13 | +import co.moonmonkeylabs.realmrecyclerview.RealmRecyclerView; |
| 14 | +import io.realm.Realm; |
| 15 | +import io.realm.RealmResults; |
| 16 | +import io.realm.Sort; |
| 17 | + |
| 18 | +public class MainActivity extends AppCompatActivity { |
| 19 | + |
| 20 | + private RealmRecyclerView realmRecyclerView; |
| 21 | + private NoteAdapter noteAdapter; |
| 22 | + private Realm realm; |
| 23 | + |
| 24 | + @Override |
| 25 | + protected void onCreate(Bundle savedInstanceState) { |
| 26 | + super.onCreate(savedInstanceState); |
| 27 | + setContentView(R.layout.activity_main); |
| 28 | + Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); |
| 29 | + setSupportActionBar(toolbar); |
| 30 | + |
| 31 | + Realm.init(this); |
| 32 | + |
| 33 | + realmRecyclerView = (RealmRecyclerView) findViewById(R.id.rv); |
| 34 | + |
| 35 | + realm = Realm.getDefaultInstance(); |
| 36 | + |
| 37 | + RealmResults<Note> notes = realm.where(Note.class).findAllSorted("id", Sort.DESCENDING); |
| 38 | + |
| 39 | + noteAdapter = new NoteAdapter(this, notes); |
| 40 | + realmRecyclerView.setAdapter(noteAdapter); |
| 41 | + |
| 42 | + FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); |
| 43 | + fab.setOnClickListener(new View.OnClickListener() { |
| 44 | + @Override |
| 45 | + public void onClick(View view) { |
| 46 | + Intent intent = new Intent(MainActivity.this, CreateNoteActivity.class); |
| 47 | + startActivity(intent); |
| 48 | + } |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public boolean onCreateOptionsMenu(Menu menu) { |
| 54 | + // Inflate the menu; this adds items to the action bar if it is present. |
| 55 | + getMenuInflater().inflate(R.menu.menu_main, menu); |
| 56 | + return true; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public boolean onOptionsItemSelected(MenuItem item) { |
| 61 | + // Handle action bar item clicks here. The action bar will |
| 62 | + // automatically handle clicks on the Home/Up button, so long |
| 63 | + // as you specify a parent activity in AndroidManifest.xml. |
| 64 | + int id = item.getItemId(); |
| 65 | + |
| 66 | + //noinspection SimplifiableIfStatement |
| 67 | + if (id == R.id.action_settings) { |
| 68 | + return true; |
| 69 | + } |
| 70 | + |
| 71 | + return super.onOptionsItemSelected(item); |
| 72 | + } |
| 73 | +} |
0 commit comments