Android 从数据库填充 Spinner 获取旧条目
在我的应用程序开始时,我显示一个对话框。 在此对话框中我有 2 个微调器。如果我更改第一个微调器的条目,应用程序将加载 JSON 文件并将其解析到数据库中。然后,微调器会填充来自保存 JSON 文件的数据库的 SimpleCursorAdapter。问题是,当我更改第一个微调器时,它总是加载上次更改微调器时保存的数据库。
这是 onItemSelected 方法中的我的代码:
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
dialogs.dismiss();
}
};
Thread checkUpdate = new Thread() {
public void run() {
klassenListeAktualisieren((new Long(txtBerufID)).toString());
handler.sendEmptyMessage(0);
}
};
checkUpdate.start();
dbHelperKlasse = new KlassenlisteDbAdapter(myContext);
dbHelperKlasse.open();
Cursor cursor_Names = dbHelperKlasse.fetchAllOfThem();
startManagingCursor(cursor_Names);
String[] columns = new String[] { dbHelperKlasse.KEY_TITLE };
int[] to = new int[] { android.R.id.text1 };
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(myContext, android.R.layout.simple_spinner_item,cursor_Names, columns, to);
mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
klassenSpinner.setAdapter(mAdapter);
如果您需要了解其他信息(代码等),请告诉我。
At the start of my App i show a Dialog.
In this dialog I have 2 spinners. If I change the entry of the first spinner the app loads a JSON file and parses it into a database. Then the spinner gets filled with a SimpleCursorAdapter from the database where the JSON file was saved. The problem is that when I change the first spinner it always loads the database which was saved the last time the spinner was changed.
Here's my code from the onItemSelected method:
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
dialogs.dismiss();
}
};
Thread checkUpdate = new Thread() {
public void run() {
klassenListeAktualisieren((new Long(txtBerufID)).toString());
handler.sendEmptyMessage(0);
}
};
checkUpdate.start();
dbHelperKlasse = new KlassenlisteDbAdapter(myContext);
dbHelperKlasse.open();
Cursor cursor_Names = dbHelperKlasse.fetchAllOfThem();
startManagingCursor(cursor_Names);
String[] columns = new String[] { dbHelperKlasse.KEY_TITLE };
int[] to = new int[] { android.R.id.text1 };
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(myContext, android.R.layout.simple_spinner_item,cursor_Names, columns, to);
mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
klassenSpinner.setAdapter(mAdapter);
Let me know if you need to know anything else (Code etc.).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于遇到同样问题的每个人,解决方案如下:
创建一个新方法,在其中放入对数据库的 fetch 调用并填充微调器。
然后,在
Handler
的handleMessage()
方法(我关闭了Dialog
)中,调用此方法。因此,旋转器在他写入数据库之后被填充,而不是在此过程中(然后显示数据库中的“旧”数据)。For everybody who faces the same problem here's the solution:
Create a new method where you put in your fetch call to the database and where you populate your spinner.
Afterwards, in the
handleMessage()
method of yourHandler
(where I closed myDialog
), call this method. So the spinner gets filled after he wrote into the database and not during this process (and then showing the "old" data from the database).