This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<EditText | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:id="@ id/et_text" | |
android:layout_alignParentTop="true" | |
android:layout_alignParentLeft="true" | |
android:layout_alignParentStart="true" | |
android:layout_alignParentRight="true" | |
android:layout_alignParentEnd="true" /> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="horizontal" | |
android:layout_width="match_parent" | |
android:id="@+id/holder_container" | |
android:layout_centerVertical="true" | |
android:paddingBottom="10dp" | |
android:paddingTop="10dp" | |
android:layout_height="wrap_content"> | |
<LinearLayout |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Calendar; | |
import java.util.Date; | |
import java.util.UUID; | |
import io.realm.RealmModel; | |
import io.realm.annotations.PrimaryKey; | |
import io.realm.annotations.RealmClass; | |
@RealmClass | |
public class Note implements RealmModel { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity { | |
private Realm mRealm; | |
private RealmConfiguration mRealmConfig; | |
private EditText mText; | |
private RealmRecyclerView mNotes; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); | |
fab.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
if(mText.getText().length() > 0){ | |
insertNote(mText.getText().toString()); | |
mText.setText(""); | |
Snackbar.make(view, "Note added!", Snackbar.LENGTH_LONG) | |
.setAction("Action", null).show(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void insertNote(final String noteText) { | |
mRealm.executeTransaction(new Realm.Transaction() { | |
@Override | |
public void execute(Realm realm) { | |
Note note = new Note(); | |
note.setText(noteText); | |
mRealm.copyToRealm(note); | |
} | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NoteRecyclerViewAdapter extends RealmBasedRecyclerViewAdapter< | |
Note, NoteRecyclerViewAdapter.ViewHolder> { | |
public NoteRecyclerViewAdapter( | |
Context context, | |
RealmResults<Note> realmResults) { | |
super(context, realmResults, true, false); | |
} | |
public class ViewHolder extends RealmViewHolder { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
mRealmConfig = new RealmConfiguration | |
.Builder(this) | |
.build(); | |
Realm.setDefaultConfiguration(mRealmConfig); | |
mRealm = Realm.getDefaultInstance(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void loadNotes() { | |
RealmResults<Note> notes = mRealm.where(Note.class).findAllSorted("date", Sort.ASCENDING); | |
NoteRecyclerViewAdapter noteAdapter = new NoteRecyclerViewAdapter(getBaseContext(), notes); | |
mNotes.setAdapter(noteAdapter); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public NoteRecyclerViewAdapter( | |
Context context, | |
RealmResults<Note> realmResults) { | |
super(context, realmResults, true, true, true, "text"); | |
} |