大家好,我有listview,显示sqlite db中的项目列表。使用DialogBox我在DB中插入数据。插入成功完成。但是,如果我在DialogBox上输入数据并单击“确定”按钮,则插入数据但插入数据的数据不会显示在ListView中。
ListView没有刷新....我使用adapter.setNotifyOnChange(true);
。但这不会对listview产生任何影响。
我在logcat上收到警告
`09-16 15:01:10.215: INFO/dalvikvm(2066): Uncaught exception thrown by finalizer (will be discarded):
09-16 15:01:10.215: INFO/dalvikvm(2066): Ljava/lang/IllegalStateException;: Finalizing cursor android.database.sqlite.SQLiteCursor@44c4be28 on PAYMENT_TABLE that has not been deactivated or closed
09-16 15:01:10.215: INFO/dalvikvm(2066): at android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:596)
09-16 15:01:10.215: INFO/dalvikvm(2066): at dalvik.system.NativeStart.run(Native Method)`
但是我在OnDestory()
中关闭了光标这是我的代码。
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToWrite();
cursor = mySQLiteAdapter.queueAll();
cursor.moveToFirst();
while(!cursor.isAfterLast()) {
String mTitleRaw = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_PAYMENT_TYPE));
al.add(mTitleRaw);
cursor.moveToNext();
}
adapterChanged();
}
@SuppressWarnings("unchecked")
private void adapterChanged() {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, al);
adapter.setNotifyOnChange(true);
setListAdapter(adapter);
listContent = getListView();
listContent.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
//Object o = this.getListAdapter().getItem(position);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if (mySQLiteAdapter != null) {
mySQLiteAdapter.close();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add(0, MENU_ADD, 0, "Add New");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()) {
case MENU_ADD:
AlertMsg();
return true;
}
return super.onOptionsItemSelected(item);
}
private void updateList(){
cursor.requery();
}
public void AlertMsg() {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Payment Type");
alert.setMessage("Add field");
final EditText mTxt = new EditText(this);
alert.setView(mTxt);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
String mNewField = mTxt.getText().toString();
//Toast.makeText(getApplicationContext(), "Item " + mNewField + " Added", Toast.LENGTH_LONG).show();
mySQLiteAdapter.insert(mNewField);
updateList();
adapterChanged();
}
});
alert.show();
}
}
这是有效的方式......
任何人都可以提出一些建议......
感谢......