You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用CursorAdapter时ListView无法加载数据问题求助

Troubleshooting Your Empty ListView After Database Insert

Hey there, let's figure out why your ListView isn't displaying data after you insert it into the database. I've gone through your code and have a few key fixes and checks to share:

1. Your Cursor Isn't Refreshing When Returning to the List Page

Right now, you only load the database cursor once in onCreate() of MyActivitiesPage. When you insert data and jump back to this page, onCreate() doesn't run again—so you're still using the old, empty cursor.

Fix this by refreshing the cursor in onResume() (this method runs every time the activity comes back to the foreground):

@Override
protected void onResume() {
    super.onResume();
    // Clean up the old cursor to prevent leaks
    if (model != null && !model.isClosed()) {
        model.close();
    }
    // Fetch fresh data from the database
    model = helper.getall();
    // Update the adapter with the new cursor
    adapter.swapCursor(model);
}

2. Case Sensitivity in SQL Queries (Potential Hidden Issue)

I noticed in your dbHandler methods like getById() and updateDetails(), you're using _ID (uppercase) in WHERE clauses, but your table is defined with _id (lowercase). While SQLite is case-insensitive by default, this inconsistency can cause unexpected behavior on some devices.

Update those queries to match your table's column name exactly:

// Fix getById()
public Cursor getById(String id) {
    String[] args = {id};
    return getReadableDatabase().rawQuery("SELECT _id, acti, date, time, addr FROM dbTable WHERE _id = ?", args);
}

// Fix updateDetails()
public void updateDetails(String id, String acti, String date, String time, String addr) {
    ContentValues cv = new ContentValues();
    String[] args = {id};
    cv.put("acti", acti);
    cv.put("date", date);
    cv.put("time", time);
    cv.put("addr", addr);
    getWritableDatabase().update("dbTable", cv, "_id = ?", args);
}

3. Verify Data Actually Gets Inserted

Even though you see a "Added Successfully!" toast, it's worth double-checking if the data is really in the database. You can:

  • Use Android Studio's Device File Explorer to locate the handler.db file (under data/data/[your package name]/databases/), download it, and open it with a SQLite viewer like DB Browser for SQLite.
  • Add a quick query right after insertion to confirm the row exists.

4. Clean Up Resources to Avoid Leaks

Make sure you close your cursor and database helper when the activity is destroyed to prevent memory leaks:

@Override
protected void onDestroy() {
    super.onDestroy();
    if (model != null && !model.isClosed()) {
        model.close();
    }
    if (helper != null) {
        helper.close();
    }
}

Start with the first fix (refreshing the cursor in onResume())—that's almost certainly the main reason your ListView is empty. The other checks will help prevent future issues.

内容的提问来源于stack exchange,提问作者Ron Tan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 03:18:13