我決定將Realm用於我的Android應用程序之一。這就是爲什麼我只是試圖用Realm製作一個簡單的應用程序來達到R目的。但是我很困惑,如果這是使用Realm的正確方法。我的項目正在運行並正確顯示數據。這裏是我的codes-這是在Android中使用Realm的正確方法嗎?
我創建了一個Java類RealmDB.java
:
package com.github.abdalimran.realmandroid;
import android.app.Application;
import android.content.Context;
import io.realm.Realm;
import io.realm.RealmConfiguration;
import io.realm.RealmResults;
public class RealmDB extends Application{
private Realm myRealm;
public RealmDB(Context context) {
myRealm = Realm.getInstance(new RealmConfiguration.Builder(context)
.name("myOtherRealm.realm")
.build());
}
public void addData(PersonDetailsModel p) {
myRealm.beginTransaction();
PersonDetailsModel person = myRealm.createObject(PersonDetailsModel.class);
person.setName(p.getName());
person.setAge(p.getAge());
person.setEmail(p.getEmail());
person.setAddress(p.getAddress());
myRealm.commitTransaction();
}
public RealmResults getAllData() {
RealmResults<PersonDetailsModel> query = myRealm.where(PersonDetailsModel.class).findAll();
return query;
}
public void closeRealm() {
myRealm.close();
}
}
現在我創建這個類的一個實例,並進行使用方法的操作。
這裏是我的領域對象類PersonDetailsModel.java
:
package com.github.abdalimran.realmandroid;
import io.realm.RealmObject;
import io.realm.annotations.PrimaryKey;
public class PersonDetailsModel extends RealmObject
{
@PrimaryKey
private String id;
private String name;
private String email;
private String address;
private int age;
public String getId() {
return id;
}
public void setId(String 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;
}
@Override
public String toString() {
return "ID: "+getId()+"\n"+
"Name: "+getName()+"\n"+
"Age: "+getAge()+"\n"+
"Email: "+getEmail()+"\n"+
"Address: "+getAddress()+"\n";
}
}
這是我實例化RealmDB類和執行操作的MainActivity.java
:
package com.github.abdalimran.realmandroid;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private ListView lvPersonNameList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvPersonNameList= (ListView) findViewById(R.id.lvPersonNameList);
RealmDB rdb=new RealmDB(this);
Log.i("list",""+rdb.getAllData());
if(rdb.getAllData()==null){
Toast.makeText(MainActivity.this, "No data found. Add person.", Toast.LENGTH_SHORT).show();
}
else {
ArrayAdapter<PersonDetailsModel> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1,
android.R.id.text1,
rdb.getAllData());
lvPersonNameList.setAdapter(adapter);
}
}
public void AddPerson(View view) {
Intent intent=new Intent(this,AddPersonActivity.class);
startActivity(intent);
}
}
這是我AddPersonActivity.java
類 -
package com.github.abdalimran.realmandroid;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import java.text.SimpleDateFormat;
import java.util.Date;
public class AddPersonActivity extends AppCompatActivity {
private EditText name;
private EditText age;
private EditText email;
private EditText address;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_person);
name= (EditText) findViewById(R.id.name);
age= (EditText) findViewById(R.id.age);
email= (EditText) findViewById(R.id.email);
address= (EditText) findViewById(R.id.address);
}
public void AddPerson(View view) {
Date dNow = new Date();
SimpleDateFormat ft = new SimpleDateFormat("yyMMddhhmmssMs");
String uniqueid = ft.format(dNow);
Log.i("uniqueid",uniqueid);
String sname=name.getText().toString();
String sage=age.getText().toString();
String semail=email.getText().toString();
String saddress=address.getText().toString();
PersonDetailsModel person=new PersonDetailsModel();
person.setId(uniqueid);
person.setName(sname);
person.setAge(Integer.parseInt(sage));
person.setEmail(semail);
person.setAddress(saddress);
RealmDB rdb=new RealmDB(getApplicationContext());
rdb.addData(person);
rdb.closeRealm();
}
}
是t他是否將Realm實施到Android項目中的正確方法?
主要是我很困惑與下面的代碼:
public void addData(PersonDetailsModel p) {
myRealm.beginTransaction();
PersonDetailsModel person = myRealm.createObject(PersonDetailsModel.class);
person.setName(p.getName());
person.setAge(p.getAge());
person.setEmail(p.getEmail());
person.setAddress(p.getAddress());
myRealm.commitTransaction();
}
在這裏,我有一個參數PersonDetailsModel p
。但是我使用myRealm.createObject(PersonDetailsModel.class);
創建領域對象,然後從p
獲取數據並將其設置爲person
。沒有辦法直接將參數p
設置爲createObject()
?
你只是在尋找一個是/否?問題是什麼?它工作嗎?還是你想讓別人審查它,並可能重構代碼? –
就我個人而言,我認爲你應該將'rdb.getAllData()'存儲到一個變量中,這樣你就不會持續查詢數據庫,但是沒有看到你有什麼錯誤 –
是的..這個工作正常。但我不確定我是否以正確的方式實施它。我希望有人審查我的方法,並建議如果我做錯了什麼。 –