JJ Stamp
JJ Stamp

Reputation: 131

RealmObject not saving data

I am saving data to a RealmObject then adding it to my Realm. After leaving the activity and returning the RealmObject is retrieved and the EditText are loaded with the appropriate data. All the EditText but one, Company Address, are loading correctly and I can't figure out why when all save and loading information is the same. Any ideas?

Activity

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 class

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;
    }

}

Upvotes: 0

Views: 1208

Answers (1)

Hugo Gresse
Hugo Gresse

Reputation: 17919

You have an @Ignore annotation on CompanyInfo#Adresse, remove it and Realm will save the adress.

Upvotes: 1

Related Questions