RLMApplication.java
public class RLMApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this)
.name(Realm.DEFAULT_REALM_NAME)
.schemaVersion(0)
.deleteRealmIfMigrationNeeded()
.build();
Realm.setDefaultConfiguration(realmConfiguration);
}
}
Contacts.java
public class Contacts extends RealmObject {
private String name;
private String email;
private String address;
private int age;
private long id;
private RealmList<Friends> friends;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public RealmList<Friends> getFriends() {
return friends;
}
public void setFriends(RealmList<Friends> friends) {
this.friends = friends;
}
}
Friends.java
public class Friends extends RealmObject {
private long fid;
private String name;
private String id;
public long getFid() {
return fid;
}
public void setFid(long fid) {
this.fid = fid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
TextView mTextViewAdd;
ListView mListView;
Realm mRealm;
CustomAdapter mCustomAdapter;
LayoutInflater mLayoutInflater;
RealmResults<Contacts> mContactses;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRealm = Realm.getDefaultInstance();
// mContactses = new RealmResults<Contacts>();
mTextViewAdd = (TextView) findViewById(R.id.txt_add);
mListView = (ListView) findViewById(R.id.list);
mLayoutInflater = getLayoutInflater();
setRealmData();
mTextViewAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View content = mLayoutInflater.inflate(R.layout.edit_item, null);
final EditText editTitle = (EditText) content.findViewById(R.id.title);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setView(content)
.setTitle("Add Contact")
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mRealm.beginTransaction();
Contacts contact = mRealm.createObject(Contacts.class);
contact.setId(System.currentTimeMillis());
contact.setName(editTitle.getText().toString());
contact.setEmail("Contact@hostname.com");
contact.setAddress("Contact's Address");
contact.setAge(20);
mRealm.commitTransaction();
mCustomAdapter.notifyDataSetChanged();
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
}
private void setRealmData() {
RealmQuery<Contacts> query = mRealm.where(Contacts.class);
mContactses = query.findAll();
mCustomAdapter = new CustomAdapter(MainActivity.this, mContactses, true);
mListView.setAdapter(mCustomAdapter);
}
public class CustomAdapter extends RealmBaseAdapter<Contacts> {
Context mActivity;
RealmResults<Contacts> list;
public CustomAdapter(Context context, RealmResults<Contacts> realmResults, boolean automaticUpdate) {
super(context, realmResults, automaticUpdate);
this.list = realmResults;
this.mActivity = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(final int i, View convertView, ViewGroup viewGroup) {
LayoutInflater mInflater = (LayoutInflater)
mActivity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.row_item, null);
TextView mTextViewName = (TextView) convertView.findViewById(R.id.txt_name);
Button mButtonEdit = (Button) convertView.findViewById(R.id.bt_edt);
Button mButtonDelete = (Button) convertView.findViewById(R.id.bt_del);
Button mButtonView= (Button) convertView.findViewById(R.id.bt_view);
mTextViewName.setText(list.get(i).getName());
mButtonView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
long cid=mContactses.get(i).getId();
Intent mIntent=new Intent(MainActivity.this,FriendActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Bundle mBundle=new Bundle();
mBundle.putString("id",String.valueOf(cid));
mIntent.putExtras(mBundle);
startActivity(mIntent);
}
});
mButtonEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View content = mLayoutInflater.inflate(R.layout.edit_item, null);
final EditText editTitle = (EditText) content.findViewById(R.id.title);
editTitle.setText(list.get(i).getName());
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setView(content)
.setTitle("Add Contact")
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
RealmResults<Contacts> results = mRealm.where(Contacts.class).findAll();
mRealm.beginTransaction();
results.get(i).setName(editTitle.getText().toString());
mRealm.commitTransaction();
notifyDataSetChanged();
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
mButtonDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mRealm.beginTransaction();
list.remove(i);
mRealm.commitTransaction();
mCustomAdapter.notifyDataSetChanged();
}
});
return convertView;
}
}
}
FriendActivity.java
public class FriendActivity extends AppCompatActivity {
TextView mTextViewAdd;
ListView mListView;
Realm mRealm;
CustomAdapter mCustomAdapter;
LayoutInflater mLayoutInflater;
RealmResults<Friends> mContactses;
Bundle mBundle;
long cid;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRealm = Realm.getDefaultInstance();
mBundle = getIntent().getExtras();
cid = Long.parseLong(mBundle.getString("id"));
mTextViewAdd = (TextView) findViewById(R.id.txt_add);
mListView = (ListView) findViewById(R.id.list);
mLayoutInflater = getLayoutInflater();
setRealmData();
mTextViewAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View content = mLayoutInflater.inflate(R.layout.edit_item, null);
final EditText editTitle = (EditText) content.findViewById(R.id.title);
AlertDialog.Builder builder = new AlertDialog.Builder(FriendActivity.this);
builder.setView(content)
.setTitle("Add Friend")
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mRealm.beginTransaction();
Friends contact = mRealm.createObject(Friends.class);
contact.setFid(System.currentTimeMillis());
contact.setId(String.valueOf(cid));
contact.setName(editTitle.getText().toString());
mRealm.commitTransaction();
mCustomAdapter.notifyDataSetChanged();
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
}
private void setRealmData() {
RealmQuery<Friends> query = mRealm.where(Friends.class);
query.contains("id", String.valueOf(cid));
mContactses = query.findAll();
mCustomAdapter = new CustomAdapter(FriendActivity.this, mContactses, true);
mListView.setAdapter(mCustomAdapter);
}
public class CustomAdapter extends RealmBaseAdapter<Friends> {
Context mActivity;
RealmResults<Friends> list;
public CustomAdapter(Context context, RealmResults<Friends> realmResults, boolean automaticUpdate) {
super(context, realmResults, automaticUpdate);
this.list = realmResults;
this.mActivity = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(final int i, View convertView, ViewGroup viewGroup) {
LayoutInflater mInflater = (LayoutInflater)
mActivity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.row_item, null);
TextView mTextViewName = (TextView) convertView.findViewById(R.id.txt_name);
Button mButtonEdit = (Button) convertView.findViewById(R.id.bt_edt);
Button mButtonDelete = (Button) convertView.findViewById(R.id.bt_del);
mTextViewName.setText(list.get(i).getName());
mButtonEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View content = mLayoutInflater.inflate(R.layout.edit_item, null);
final EditText editTitle = (EditText) content.findViewById(R.id.title);
editTitle.setText(list.get(i).getName());
AlertDialog.Builder builder = new AlertDialog.Builder(FriendActivity.this);
builder.setView(content)
.setTitle("Add Friend")
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
RealmResults<Friends> results = mRealm.where(Friends.class).findAll();
mRealm.beginTransaction();
results.get(i).setName(editTitle.getText().toString());
mRealm.commitTransaction();
notifyDataSetChanged();
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
mButtonDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mRealm.beginTransaction();
list.remove(i);
mRealm.commitTransaction();
mCustomAdapter.notifyDataSetChanged();
}
});
return convertView;
}
}
}
public class RLMApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this)
.name(Realm.DEFAULT_REALM_NAME)
.schemaVersion(0)
.deleteRealmIfMigrationNeeded()
.build();
Realm.setDefaultConfiguration(realmConfiguration);
}
}
Contacts.java
public class Contacts extends RealmObject {
private String name;
private String email;
private String address;
private int age;
private long id;
private RealmList<Friends> friends;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public RealmList<Friends> getFriends() {
return friends;
}
public void setFriends(RealmList<Friends> friends) {
this.friends = friends;
}
}
Friends.java
public class Friends extends RealmObject {
private long fid;
private String name;
private String id;
public long getFid() {
return fid;
}
public void setFid(long fid) {
this.fid = fid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
TextView mTextViewAdd;
ListView mListView;
Realm mRealm;
CustomAdapter mCustomAdapter;
LayoutInflater mLayoutInflater;
RealmResults<Contacts> mContactses;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRealm = Realm.getDefaultInstance();
// mContactses = new RealmResults<Contacts>();
mTextViewAdd = (TextView) findViewById(R.id.txt_add);
mListView = (ListView) findViewById(R.id.list);
mLayoutInflater = getLayoutInflater();
setRealmData();
mTextViewAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View content = mLayoutInflater.inflate(R.layout.edit_item, null);
final EditText editTitle = (EditText) content.findViewById(R.id.title);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setView(content)
.setTitle("Add Contact")
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mRealm.beginTransaction();
Contacts contact = mRealm.createObject(Contacts.class);
contact.setId(System.currentTimeMillis());
contact.setName(editTitle.getText().toString());
contact.setEmail("Contact@hostname.com");
contact.setAddress("Contact's Address");
contact.setAge(20);
mRealm.commitTransaction();
mCustomAdapter.notifyDataSetChanged();
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
}
private void setRealmData() {
RealmQuery<Contacts> query = mRealm.where(Contacts.class);
mContactses = query.findAll();
mCustomAdapter = new CustomAdapter(MainActivity.this, mContactses, true);
mListView.setAdapter(mCustomAdapter);
}
public class CustomAdapter extends RealmBaseAdapter<Contacts> {
Context mActivity;
RealmResults<Contacts> list;
public CustomAdapter(Context context, RealmResults<Contacts> realmResults, boolean automaticUpdate) {
super(context, realmResults, automaticUpdate);
this.list = realmResults;
this.mActivity = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(final int i, View convertView, ViewGroup viewGroup) {
LayoutInflater mInflater = (LayoutInflater)
mActivity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.row_item, null);
TextView mTextViewName = (TextView) convertView.findViewById(R.id.txt_name);
Button mButtonEdit = (Button) convertView.findViewById(R.id.bt_edt);
Button mButtonDelete = (Button) convertView.findViewById(R.id.bt_del);
Button mButtonView= (Button) convertView.findViewById(R.id.bt_view);
mTextViewName.setText(list.get(i).getName());
mButtonView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
long cid=mContactses.get(i).getId();
Intent mIntent=new Intent(MainActivity.this,FriendActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Bundle mBundle=new Bundle();
mBundle.putString("id",String.valueOf(cid));
mIntent.putExtras(mBundle);
startActivity(mIntent);
}
});
mButtonEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View content = mLayoutInflater.inflate(R.layout.edit_item, null);
final EditText editTitle = (EditText) content.findViewById(R.id.title);
editTitle.setText(list.get(i).getName());
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setView(content)
.setTitle("Add Contact")
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
RealmResults<Contacts> results = mRealm.where(Contacts.class).findAll();
mRealm.beginTransaction();
results.get(i).setName(editTitle.getText().toString());
mRealm.commitTransaction();
notifyDataSetChanged();
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
mButtonDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mRealm.beginTransaction();
list.remove(i);
mRealm.commitTransaction();
mCustomAdapter.notifyDataSetChanged();
}
});
return convertView;
}
}
}
FriendActivity.java
public class FriendActivity extends AppCompatActivity {
TextView mTextViewAdd;
ListView mListView;
Realm mRealm;
CustomAdapter mCustomAdapter;
LayoutInflater mLayoutInflater;
RealmResults<Friends> mContactses;
Bundle mBundle;
long cid;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRealm = Realm.getDefaultInstance();
mBundle = getIntent().getExtras();
cid = Long.parseLong(mBundle.getString("id"));
mTextViewAdd = (TextView) findViewById(R.id.txt_add);
mListView = (ListView) findViewById(R.id.list);
mLayoutInflater = getLayoutInflater();
setRealmData();
mTextViewAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View content = mLayoutInflater.inflate(R.layout.edit_item, null);
final EditText editTitle = (EditText) content.findViewById(R.id.title);
AlertDialog.Builder builder = new AlertDialog.Builder(FriendActivity.this);
builder.setView(content)
.setTitle("Add Friend")
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mRealm.beginTransaction();
Friends contact = mRealm.createObject(Friends.class);
contact.setFid(System.currentTimeMillis());
contact.setId(String.valueOf(cid));
contact.setName(editTitle.getText().toString());
mRealm.commitTransaction();
mCustomAdapter.notifyDataSetChanged();
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
}
private void setRealmData() {
RealmQuery<Friends> query = mRealm.where(Friends.class);
query.contains("id", String.valueOf(cid));
mContactses = query.findAll();
mCustomAdapter = new CustomAdapter(FriendActivity.this, mContactses, true);
mListView.setAdapter(mCustomAdapter);
}
public class CustomAdapter extends RealmBaseAdapter<Friends> {
Context mActivity;
RealmResults<Friends> list;
public CustomAdapter(Context context, RealmResults<Friends> realmResults, boolean automaticUpdate) {
super(context, realmResults, automaticUpdate);
this.list = realmResults;
this.mActivity = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(final int i, View convertView, ViewGroup viewGroup) {
LayoutInflater mInflater = (LayoutInflater)
mActivity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.row_item, null);
TextView mTextViewName = (TextView) convertView.findViewById(R.id.txt_name);
Button mButtonEdit = (Button) convertView.findViewById(R.id.bt_edt);
Button mButtonDelete = (Button) convertView.findViewById(R.id.bt_del);
mTextViewName.setText(list.get(i).getName());
mButtonEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View content = mLayoutInflater.inflate(R.layout.edit_item, null);
final EditText editTitle = (EditText) content.findViewById(R.id.title);
editTitle.setText(list.get(i).getName());
AlertDialog.Builder builder = new AlertDialog.Builder(FriendActivity.this);
builder.setView(content)
.setTitle("Add Friend")
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
RealmResults<Friends> results = mRealm.where(Friends.class).findAll();
mRealm.beginTransaction();
results.get(i).setName(editTitle.getText().toString());
mRealm.commitTransaction();
notifyDataSetChanged();
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
mButtonDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mRealm.beginTransaction();
list.remove(i);
mRealm.commitTransaction();
mCustomAdapter.notifyDataSetChanged();
}
});
return convertView;
}
}
}