我正在将数据保存到RealmObject
,然后将其添加到我的Realm
。离开活动并返回后,将检索RealmObject
,并为EditText
加载适当的数据。所有EditText
但是一个公司地址正在正确加载,我无法弄清楚为什么所有保存和加载信息都相同。有什么想法吗?
活动
public class NewLocation extends ActionBarActivity {
public EditText editCoName;
public EditText editCoAddress;
public EditText editCoContact;
public EditText editSqFt;
public EditText editTaxed;
public EditText editConcerns;
private Realm realm;
public CompanyInfo companyInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_location);
findViewById(R.id.button3).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SaveInfo();
Intent i = new Intent(NewLocation.this, RoomList.class);
startActivity(i);
}
});
editCoName = (EditText) findViewById(R.id.CoName);
editCoAddress = (EditText) findViewById(R.id.CoAddress);
editCoContact = (EditText) findViewById(R.id.CoContact);
editSqFt = (EditText) findViewById(R.id.SqFt);
editTaxed = (EditText) findViewById(R.id.Taxed);
editConcerns = (EditText) findViewById(R.id.Concerns);
//initialize realm and make CompanyInfo object if there is not already one
realm = Realm.getInstance(this);
CompanyInfo result = realm.where(CompanyInfo.class).findFirst();
if (result == null){
realm.beginTransaction();
companyInfo = realm.createObject(CompanyInfo.class);
companyInfo.setName(editCoName.getText().toString());
companyInfo.setAddress(editCoAddress.getText().toString());
companyInfo.setContact(editCoContact.getText().toString());
companyInfo.setTaxed(editTaxed.getText().toString());
companyInfo.setSqFt(editSqFt.getText().toString());
companyInfo.setConcerns(editConcerns.getText().toString());
realm.commitTransaction();
}
else {
editCoName.setText(result.getName());
editCoAddress.setText(result.getAddress());
editCoContact.setText(result.getContact());
editTaxed.setText(result.getTaxed());
editSqFt.setText(result.getSqFt());
editConcerns.setText(result.getConcerns());
}
}
@Override
protected void onResume() {
super.onResume();
LoadInfo();
}
@Override
protected void onPause() {
super.onPause();
SaveInfo();
}
@Override
protected void onDestroy() {
super.onDestroy();
realm.close();
}
public void SaveInfo() {
//save all info from the page
companyInfo = realm.where(CompanyInfo.class).findFirst();
realm.beginTransaction();
companyInfo.setName(editCoName.getText().toString());
companyInfo.setAddress(editCoAddress.getText().toString());
companyInfo.setContact(editCoContact.getText().toString());
companyInfo.setTaxed(editTaxed.getText().toString());
companyInfo.setSqFt(editSqFt.getText().toString());
companyInfo.setConcerns(editConcerns.getText().toString());
realm.copyToRealmOrUpdate(companyInfo);
realm.commitTransaction();
}
public void LoadInfo() {
//load info fom the CompanyInfo and put it into EditTexts
companyInfo = realm.where(CompanyInfo.class).findFirst();
if (companyInfo != null) {
editCoName.setText(companyInfo.getName());
editCoAddress.setText(companyInfo.getAddress());
editCoContact.setText(companyInfo.getContact());
editTaxed.setText(companyInfo.getTaxed());
editSqFt.setText(companyInfo.getSqFt());
editConcerns.setText(companyInfo.getConcerns());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_new_location, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch(item.getItemId())
{
case R.id.home:
startActivity(new Intent(getApplicationContext(), MainPage.class));
break;
}
return super.onOptionsItemSelected(item);
}
}
CompanyInfo类
public class CompanyInfo extends RealmObject{
@PrimaryKey
private String Name;
@Ignore
private String Address;
private String Contact;
private String sqFt;
private String taxed;
private String concerns;
private RealmList<Rooms> rooms = new RealmList<>();
public RealmList<Rooms> getRooms() {
return rooms;
}
public void setRooms(RealmList<Rooms> rooms) {
this.rooms = rooms;
}
public String getName() {
return Name;
}
public String getAddress() {
return Address;
}
public String getContact() {
return Contact;
}
public String getSqFt() {
return sqFt;
}
public String getTaxed() {
return taxed;
}
public String getConcerns() {
return concerns;
}
public void setName(String coName) {
this.Name = coName;
}
public void setAddress(String coAddress) {
this.Address = coAddress;
}
public void setContact(String coContact) {
this.Contact = coContact;
}
public void setSqFt(String sqFt) {
this.sqFt = sqFt;
}
public void setTaxed(String taxed) {
this.taxed = taxed;
}
public void setConcerns(String concerns) {
this.concerns = concerns;
}
}
答案 0 :(得分:1)
@Ignore
上有CompanyInfo#Adresse
注释,将其删除,Realm会保存地址。