我有两个RealmObjects,CompanyInfo
和Rooms
,其中一个拥有另一个RealmList。在尝试添加到RealmList然后在提交事务后将其添加到RealmObject时,RealmList设置为0并添加到RealmObject作为大小0,因为当我填充Rooms
列表时没有显示起来。这是为什么?
CompanyInfo类
public class CompanyInfo extends RealmObject{
private String Name;
private String Address;
private String Contact;
private String sqFt;
private String taxed;
private String concerns;
private RealmList<Rooms> rooms;
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;
}
}
客房类
public class Rooms extends RealmObject{
private String Name;
private RealmList<FixtureInfo> fixtureInfos;
public RealmList<FixtureInfo> getFixtureInfos() {
return fixtureInfos;
}
public void setFixtureInfos(RealmList<FixtureInfo> fixtureInfos) {
this.fixtureInfos = fixtureInfos;
}
public void setName(String name) {
Name = name;
}
public String getName() {
return Name;
}
}
Java文件
public class RoomList extends ActionBarActivity {
public AlertDialog.Builder dialogBuilder;
public RealmList<Rooms> myRooms;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_room_list);
populateListView();
ListView list = (ListView) findViewById(R.id.RoomsList);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
default:
SaveInfo();
Intent i = new Intent(RoomList.this, RoomDescription.class);
TextView textItem = (TextView) view;
String txtString = textItem.getText().toString();
i.putExtra("txtString", txtString);
startActivity(i);
break;
}
}
});
Realm realm = Realm.getInstance(this);
CompanyInfo result = realm.where(CompanyInfo.class).findFirst();
if (result != null){
myRooms = result.getRooms();
}
}
@Override
protected void onResume() {
super.onResume();
LoadInfo();
}
@Override
protected void onPause() {
super.onPause();
SaveInfo();
}
private void setRoomName()
{
dialogBuilder = new AlertDialog.Builder(this);
final EditText txtInput = new EditText(this);
final Realm realm = Realm.getInstance(this);
dialogBuilder.setTitle("New Room");
dialogBuilder.setMessage("What is the room name?");
dialogBuilder.setView(txtInput);
dialogBuilder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
realm.beginTransaction();
String txtString = txtInput.getText().toString();
Rooms rooms = realm.createObject(Rooms.class);
rooms.setName(txtString);
myRooms.add(rooms);
realm.commitTransaction();
realm.close();
SaveInfo();
Intent i = new Intent(RoomList.this, RoomDescription.class);
i.putExtra("txtString", txtString);
Toast.makeText(getApplicationContext(), "Room has been added.", Toast.LENGTH_SHORT).show();
populateListView();
startActivity(i);
}
});
dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
SaveInfo();
}
});
AlertDialog dialogFixtureName = dialogBuilder.create();
dialogFixtureName.show();
}
private void populateListView()
{
if (myRooms != null) {
ArrayAdapter<Rooms> adapter = new ArrayAdapter<Rooms>(this, R.layout.rooms, myRooms);
ListView list = (ListView) findViewById(R.id.RoomsList);
list.setAdapter(adapter);
SaveInfo();
}
}
public void SaveInfo()
{
Realm realm = Realm.getInstance(this);
CompanyInfo companyInfo = realm.where(CompanyInfo.class).findFirst();
realm.beginTransaction();
companyInfo.setRooms(myRooms);
realm.commitTransaction();
realm.close();
}
public void LoadInfo()
{
Realm realm = Realm.getInstance(this);
CompanyInfo companyInfo = realm.where(CompanyInfo.class).findFirst();
if (companyInfo != null){
myRooms = companyInfo.getRooms();
}
realm.close();
}
@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_room_list, 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.add:
setRoomName();
break;
case R.id.home:
startActivity(new Intent(getApplicationContext(), MainPage.class));
break;
}
return super.onOptionsItemSelected(item);
}
}