Source:
<code>protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); realm = Realm.getDefaultInstance(); <START> RealmResults<SmsDetectionString> msgItems = realm.allObjects(SmsDetectionString.class); <END> listViewAdv.setAdapter(new DetectionStringAdapter(this, msgItems, true)); listViewAdv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> a, View v, int position, long id) { SmsDetectionString detectionString = (SmsDetectionString) listViewAdv.getItemAtPosition(position); String string = detectionString.getDetectionString(); detectionString.removeFromRealm(); Toast.makeText(getApplicationContext(), getString(R.string.deleted) + ": " + string, Toast.LENGTH_SHORT).show(); return true; } }); insertButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (editAdvUserDet.getText().toString().contains("\"")) { Toast.makeText(AdvancedUserActivity.this, R.string.double_quote_will_cause_db_error, Toast.LENGTH_SHORT).show(); } else { realm.executeTransaction(new Realm.Transaction() { @Override public void execute(Realm realm) { SmsDetectionString detectionString = realm.createObject(SmsDetectionString.class); detectionString.setDetectionString(editAdvUserDet.getText().toString()); detectionString.setSmsType(spinner.getSelectedItem().toString()); Toast.makeText(AdvancedUserActivity.this, R.string.the_string_was_added_to_db, Toast.LENGTH_SHORT).show(); } }); } } }); }</code><technical_language>Note allObjects sorted, want allObjectsSorted where(SmsDetectionString.class).findAllSorted()</technical_language>

Target:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); realm = Realm.getDefaultInstance(); RealmResults<SmsDetectionString> msgItems = realm.allObjects(SmsDetectionString.class); listViewAdv.setAdapter(new DetectionStringAdapter(this, msgItems, true)); listViewAdv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> a, View v, int position, long id) { final SmsDetectionString detectionString = (SmsDetectionString) listViewAdv.getItemAtPosition(position); String string = detectionString.getDetectionString(); realm.executeTransaction(new Realm.Transaction() { @Override public void execute(Realm realm) { detectionString.removeFromRealm(); } }); Toast.makeText(getApplicationContext(), getString(R.string.deleted) + ": " + string, Toast.LENGTH_SHORT).show(); return true; } }); insertButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (editAdvUserDet.getText().toString().contains("\"")) { Toast.makeText(AdvancedUserActivity.this, R.string.double_quote_will_cause_db_error, Toast.LENGTH_SHORT).show(); } else { realm.executeTransaction(new Realm.Transaction() { @Override public void execute(Realm realm) { SmsDetectionString detectionString = realm.createObject(SmsDetectionString.class); detectionString.setDetectionString(editAdvUserDet.getText().toString()); detectionString.setSmsType(spinner.getSelectedItem().toString()); Toast.makeText(AdvancedUserActivity.this, R.string.the_string_was_added_to_db, Toast.LENGTH_SHORT).show(); } }); } } }); }