1

我的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);
}
4

1 回答 1

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();
于 2013-02-26T22:09:30.303 回答