我试图在调用对话框后将数据保存到RealmList
。该对话框应该采用新对象的名称FixtureInfo
,将其添加到RealmList
,然后转到下一个活动。但是,在移动到下一个活动并按下后退按钮后,填充了ListView
的{{1}}不会显示刚刚创建的对象。它没有出现任何错误。有什么想法吗?
RealmList
中添加编辑作为beeeneder建议我这样做并且问题仍然存在。
第一项活动
RealmBaseAdapter
RealmBaseAdapter类
public class RoomDescription extends ActionBarActivity {
public AlertDialog.Builder dialogBuilder;
public RealmList<FixtureInfo> myFixtures = new RealmList<>();
public String RoomName;
public String FixtureName;
public RealmList<Rooms> myRooms = new RealmList<>();
private Realm realm;
public Rooms rooms;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_room_description);
//get room name
TextView textFixture = (TextView) findViewById(R.id.RoomName);
Bundle extras = getIntent().getExtras();
if (extras != null) {
RoomName = extras.getString("txtString");
textFixture.setText(RoomName);
}
//initiate realm instant to get CompanyInfo object, Room object, and populate myRoom and myFixtures
realm = Realm.getInstance(this);
rooms = realm.where(Rooms.class).equalTo("Name", RoomName).findFirst();
realm.beginTransaction();
CompanyInfo companyinfo = realm.where(CompanyInfo.class).findFirst();
myRooms = companyinfo.getRooms();
myFixtures = rooms.getFixtureInfos();
realm.commitTransaction();
populateListView();
}
@Override
protected void onResume() {
super.onResume();
LoadInfo();
}
@Override
protected void onPause() {
super.onPause();
SaveInfo();
}
@Override
protected void onDestroy() {
super.onDestroy();
realm.close();
}
private void setFixtureName()
{
//dialog to add fixture to room and set its name
dialogBuilder = new AlertDialog.Builder(this);
final EditText txtInput = new EditText(this);
dialogBuilder.setTitle("New Fixture");
dialogBuilder.setMessage("What is the fixture name?");
dialogBuilder.setView(txtInput);
dialogBuilder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//initiate realm instant
Realm realm = Realm.getInstance(getApplicationContext());
realm.beginTransaction();
//get fixture name and create FixtureInfo object
FixtureName = txtInput.getText().toString();
FixtureInfo fixtureInfo = realm.createObject(FixtureInfo.class);
fixtureInfo.setName(FixtureName);
fixtureInfo.setRoomName(RoomName);
myFixtures.add(fixtureInfo);
realm.commitTransaction();
//save changes
SaveInfo();
//start new activity
Intent i = new Intent(RoomDescription.this, FixtureDescription.class);
i.putExtra("textString", FixtureName);
i.putExtra("txtString", RoomName);
populateListView();
Toast.makeText(getApplicationContext(), "Fixture has been added.", Toast.LENGTH_SHORT).show();
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 setRoomName()
{
//same as setRoomName in RoomList.java
dialogBuilder = new AlertDialog.Builder(this);
final EditText txtInput = new EditText(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 realm = Realm.getInstance(getApplicationContext());
realm.beginTransaction();
String txtString = txtInput.getText().toString();
Rooms rooms = realm.createObject(Rooms.class);
rooms.setName(txtString);
myRooms.add(rooms);
realm.commitTransaction();
SaveInfo();
realm.close();
Intent i = new Intent(RoomDescription.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 removeFixture()
{
//remove fixture from room
dialogBuilder = new AlertDialog.Builder(this);
dialogBuilder.setTitle("Select Fixture to Remove");
dialogBuilder.setSingleChoiceItems(myFixtures.toArray(new String[myFixtures.size()]), -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//initiate realm instant
Realm realm = Realm.getInstance(getApplicationContext());
//remove fixture info from room
realm.beginTransaction();
myFixtures = rooms.getFixtureInfos();
myFixtures.remove(which);
//save change
SaveInfo();
populateListView();
Toast.makeText(getApplicationContext(), "Fixture has been removed.", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
AlertDialog dialogFixtureName = dialogBuilder.create();
dialogFixtureName.show();
}
private void populateListView()
{
//on click for the list of FixtureInfo connected to the room
ListView list = (ListView) findViewById(R.id.FixtureList);
RealmResults<FixtureInfo> results = realm.where(FixtureInfo.class).equalTo("RoomName", RoomName).findAll();
FixtureListAdapter adapter = new FixtureListAdapter(this, results, true);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
default:
Intent i = new Intent(RoomDescription.this, FixtureDescription.class);
TextView textItem = (TextView) view;
String FixtureName = textItem.getText().toString();
i.putExtra("textString", FixtureName);
i.putExtra("txtString", RoomName);
startActivity(i);
break;
}
}
});
}
public void SaveInfo()
{
//save info or update info
realm.beginTransaction();
rooms.setName(RoomName);
rooms.setFixtureInfos(myFixtures);
realm.copyToRealmOrUpdate(rooms);
realm.commitTransaction();
}
public void LoadInfo()
{
//load info from specific room
realm.beginTransaction();
myFixtures = rooms.getFixtureInfos();
realm.commitTransaction();
}
@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_description, 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.newFixture:
setFixtureName();
break;
case R.id.removeFixture:
removeFixture();
break;
case R.id.add:
setRoomName();
break;
case R.id.home:
startActivity(new Intent(getApplicationContext(), MainPage.class));
break;
case R.id.summary:
startActivity(new Intent(getApplicationContext(), Summary.class));
break;
}
return super.onOptionsItemSelected(item);
}
}
FixtureInfo类
public class FixtureListAdapter extends RealmBaseAdapter<FixtureInfo> implements ListAdapter {
private static class ViewHolder{
TextView FixtureName;
}
public FixtureListAdapter(Context context, RealmResults<FixtureInfo> realmResults, boolean automaticUpdate) {
super(context, realmResults, automaticUpdate);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(android.R.layout.simple_list_item_1,
parent, false);
viewHolder = new ViewHolder();
viewHolder.FixtureName = (TextView) convertView.findViewById(android.R.id.text1);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
FixtureInfo fixtureInfo = realmResults.get(position);
viewHolder.FixtureName.setText(fixtureInfo.getName());
return convertView;
}
}
答案 0 :(得分:0)
我建议你在onClick
中设置一个中断指针来检查你试图在那里添加的fixtureInfo。由于RoomName
是主键,因此如果您尝试更改FixtrueInfo
并使用其他RoomName
,我不会指望您会看到ListView
中的任何更改{1}}因为RealmResults
基于原始名称。
还有其他一些问题:
Realm.createObject
创建的,则无需再调用copyToRealmOrUpdate
我建议你在我们的例子http://github.com/realm/realm-java/tree/master/examples上再花几分钟时间。而且,realm-java背后的机制是我们使用Proxy对象来重载getter / setter。这并不困难。您可以在构建目录中找到FixtureInfoRealmProxy.java
。理解整个事情会很有趣也很有帮助,也许你也可以快速查看一下:)