我的listview
活动不会在我的列表视图sName
中显示特定行(UI
)。我可以向我sqlite
数据库添加项目,但在我的数据库更新后尝试让我的listview
刷新时,我的列表视图为空。
数据为added\updated\removed
时,我没有收到任何错误。我确实收到logcat错误,说明启动应用程序时光标或数据库未关闭。 “android.database.sqlite.DatabaseObjectNotClosedException:
应用程序未关闭此处打开的游标或数据库对象”请告知。 logcat和代码如下。
活性:
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);
}
}
修订活动:
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);
}
答案 0 :(得分:1)
在查询或编辑后,似乎忘了在光标或数据库上调用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();