gpt4 book ai didi

安卓 ListView : Application did not close the cursor or database object

转载 作者:行者123 更新时间:2023-11-30 03:45:23 24 4
gpt4 key购买 nike

我的 listview Activity 不会在我的 ListView UI 中显示特定行 (sName)。我可以向我的 sqlite 数据库添加项目,但是在我的数据库更新时尝试让我的 listview 刷新后,我的 ListView 是空白的。

added\updated\removed 数据时我没有收到任何错误。我确实收到 logcat 错误,指出应用程序启动时游标或数据库未关闭。 “android.database.sqlite.DatabaseObjectNotClosedException: 应用程序没有关闭此处打开的游标或数据库对象”请指教。 logcat 和代码如下。

Activity :

   public class LoginList extends Activity implements OnClickListener,    OnItemClickListener {

private ListView loginList;
private Button webLogin;

private ListAdapter loginListAdapter;

private ArrayList<LoginDetails> loginArrayList;

List<String> arrayList = new ArrayList<String>();



@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

loginListAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, populateList());
setContentView(R.layout.login_listview);

arrayList = populateList();


loginList = (ListView)
findViewById(R.id.loginlist);
loginList.setOnItemClickListener(this);

webLogin = (Button)
findViewById(R.id.button3);
webLogin.setOnClickListener(this);


}

@Override
public void onClick (View v) {
Intent webLoginIntent = new Intent (this, LoginPlusActivity.class);
startActivity(webLoginIntent);

}

public List<String> populateList(){

ArrayList<LoginDetails> loginArrayList = new ArrayList<LoginDetails>();
List<String> webNameList = new ArrayList<String>();

dataStore openHelperClass = new dataStore (this);

SQLiteDatabase sqliteDatabase = openHelperClass.getReadableDatabase();

Cursor cursor = sqliteDatabase.query(dataStore.TABLE_NAME_INFOTABLE, null, null, null, null, null, dataStore.COLUMN_NAME_SITE, null);

startManagingCursor(cursor);


while (cursor.moveToNext()){
String sName = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_SITE));
String wUrl = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_ADDRESS));
String uName = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_USERNAME));
String pWord = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_PASSWORD));
String lNotes = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_NOTES));

LoginDetails lpDetails = new LoginDetails();

lpDetails.setsName(sName);
lpDetails.setwUrl(wUrl);
lpDetails.setuName(uName);
lpDetails.setpWord(pWord);
lpDetails.setlNotes(lNotes);

loginArrayList.add(lpDetails);
webNameList.add(sName);
}

cursor.close();
sqliteDatabase.close();
return webNameList;
}



@Override
protected void onResume() {
super.onResume();




arrayList.clear();

arrayList = populateList();

SimpleCursorAdapter loginListAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, null, null, null);
loginListAdapter.notifyDataSetChanged();

}

@Override
public void onItemClick(AdapterView<?> arg0 , View arg1, int arg2, long arg3) {
Toast.makeText(getApplicationContext(), "Selected ID :" + arg2, Toast.LENGTH_SHORT).show();

Intent updateDeleteLoginInfo = new Intent (this, UpdateDeleteLoginList.class);



LoginDetails clickedObject = loginArrayList.get(arg2);

Bundle loginBundle = new Bundle();
loginBundle.putString("clickedWebSite",clickedObject.getsName());
loginBundle.putString("clickedWebAddress",clickedObject.getwUrl());
loginBundle.putString("clickedUserName",clickedObject.getuName());
loginBundle.putString("clickedPassWord",clickedObject.getpWord());
loginBundle.putString("clickedNotes",clickedObject.getlNotes());

updateDeleteLoginInfo.putExtras(loginBundle);

startActivityForResult(updateDeleteLoginInfo, 0);
}
}

修改后的 Activity :

     public class LoginList extends FragmentActivity implements OnClickListener, OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor> {

private ListView loginList;
private Button webLogin;
private CursorAdapter adapter;
private static final int LOADER_ID = 0x02;

private ArrayList<LoginDetails> loginArrayList;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.login_listview);
getSupportLoaderManager().initLoader(LOADER_ID, null, this);

loginList = (ListView)
findViewById(R.id.loginlist);
loginList.setOnItemClickListener(this);

webLogin = (Button)
findViewById(R.id.button3);
webLogin.setOnClickListener(this);

loginArrayList = new ArrayList<LoginDetails>();
adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, null,
new String[] { dataStore.COLUMN_NAME_SITE}, new int[]{R.id.textView1});
loginList.setAdapter(adapter);
}

public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
return new CursorLoader(this, Uri.parse(null), null, null, null, null);
}
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
adapter.swapCursor(cursor);
}
public void onLoaderReset(Loader<Cursor> cursorLoader) {
adapter.swapCursor(null);
}

最佳答案

您好像忘记在查询或编辑后在游标或数据库上调用 close()。

SQLiteDatabase db;//your database
//do your work...
//If you query in your database:
Cursor c=db.rawQuery(...);//or something like that
//fetch data from cursor
c.close();
db.close();

关于安卓 ListView : Application did not close the cursor or database object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15097488/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com