Java db4o无法跟踪Android上的对象
我正在制作我的第一个android应用程序,并尝试使用db4o数据库 一般来说,它的结构是这样的:您有一个具有某种继承性的“Person”类来区分“boss”、“employee”、“intern”,。。。但我想这在这里并不重要。 您还有“任务”类。“Task”对象除其他属性外,还有一个带有Persons的ArrayList。任务可以不分配人员而存在,反之亦然 我遇到的问题是db4o似乎无法跟踪我的对象。因此,当我尝试将从数据库中加载的带有查询的人员附加到任务时,该人员会被复制。 现在,我已经解决了这个问题,对Person对象执行QBE,并将返回的对象添加到任务中。但这似乎非常错误 所以在某个地方,对我的对象的引用丢失了。。。 我一直保持数据库连接打开,只是在存储区关闭它 这是一个示例操作: 我有一个activity,其中我为任务填充了一些属性,然后我使用ActivityForResult,在这个activity中,我通过列表视图中的意图选择并返回Person对象。然后回到我想链接并保存它们的第一个活动 我在意图中传递的对象是可序列化的,而不是可打包的,我不知道除了“性能影响”之外,这是否很重要 我愿意发布一些代码,但我不知道代码的哪一部分有助于识别问题 我认为代码是相关的: 添加任务活动: 这是onCreateJava db4o无法跟踪Android上的对象,java,android,db4o,Java,Android,Db4o,我正在制作我的第一个android应用程序,并尝试使用db4o数据库 一般来说,它的结构是这样的:您有一个具有某种继承性的“Person”类来区分“boss”、“employee”、“intern”,。。。但我想这在这里并不重要。 您还有“任务”类。“Task”对象除其他属性外,还有一个带有Persons的ArrayList。任务可以不分配人员而存在,反之亦然 我遇到的问题是db4o似乎无法跟踪我的对象。因此,当我尝试将从数据库中加载的带有查询的人员附加到任务时,该人员会被复制。 现在,我已经解
Db4oHelper db4oHelper = new Db4oHelper(context);
db=db4oHelper.db();
(...)
Button AddButton = (Button) findViewById(R.id.Addbutton);
AddButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent pickUserIntent = new Intent(context,PickUser.class);
startActivityForResult(pickUserIntent,1);
}
});
以及:
listResultToArray的helper类只是一个简单的for循环迭代,它将数据库返回的ObjectSet项放入listview所需的ArrayList中
PickUser活动:
请张贴相关代码。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
ArrayList<Person> personList_local = (ArrayList<Person>)
data.getSerializableExtra("PERSONS");
ArrayList<Person> personList_db=new ArrayList<Person>();
for (Person p:personList_local){
personList_db.add((Person)helper.listResultToArray(db.queryByExample(po)).get(0));
}
Task t = new Task("Description",personList_db);
db.store(t);
db.commit();
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
persons_sel=new ArrayList<Person>();
Db4oHelper db4oHelper = new Db4oHelper(context);
db=db4oHelper.db();
persons_all=helper.listResultToArray(db.query(Person.class));
list = (ListView) findViewById(android.R.id.list);
this.adapter = new PersonAdapter(this, R.layout.row_checkbox, persons_all);
setListAdapter(this.adapter);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Person p = (Person) parent.getItemAtPosition(position);
CheckBox check = (CheckBox) view.findViewById(R.id.check);
if (!person_sel.remove(p)) {
person_sel.add(p);
check.setChecked(true);
}else{
check.setChecked(false);
}
}
});
Button SubmitButton = (Button) findViewById(R.id.SubmitButton);
SubmitButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (persons_sel.size()>0){
Intent returnIntent = new Intent();
returnIntent.putExtra("PERSONS", persons_sel);
setResult(RESULT_OK,returnIntent);
finish();
}else{
Toast.makeText(getApplicationContext(),
"You must select at least something!",
Toast.LENGTH_SHORT).show();
}
}
});
}
private class PersonAdapter extends ArrayAdapter<Person> {
private ArrayList<Person> items;
public PersonAdapter(Context context, int textViewResourceId,
ArrayList<Person> items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row_checkbox, null);
}
Person p = items.get(position);
if (p != null) {
TextView tt = (TextView) v.findViewById(R.id.toptext);
if (tt != null) {
tt.setText(p.toString());
}
}
return v;
}
}