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

Android Studio中ListView无法显示书签列表项问题求助

Fixing Your Bookmark ListView Not Displaying URLs

Hey there, let's figure out why your bookmarks aren't showing up in the ListView. After looking through your code, I found a couple of key issues that are causing this problem, plus some optimizations to make your code more robust.

1. Critical Cursor Traversal Bug in databasetostring()

The biggest issue is how you're iterating over the Cursor in your database query method. Right now, your code skips the first record entirely and fails to read data correctly if there's only one bookmark. Here's why:

  • cursor.moveToFirst() moves to the first row of results
  • You immediately call cursor.moveToNext(), which jumps to the second row (if it exists)
  • If there's only one bookmark, cursor.moveToNext() returns false, and you never enter the loop at all.

Fix for databasetostring():

Rewrite the method to properly iterate through all rows, and use getReadableDatabase() instead of getWritableDatabase() since we're only reading data:

public List<String> databasetostring() {
    SQLiteDatabase db = getReadableDatabase();
    String query = "SELECT * FROM " + TABLE_NAME;
    List<String> dbstring = new ArrayList<>();
    Cursor cursor = db.rawQuery(query, null);

    // Correctly iterate through all rows
    if (cursor.moveToFirst()) {
        do {
            String url = cursor.getString(cursor.getColumnIndex(COLUMN_NAME));
            if (url != null) {
                dbstring.add(url);
            }
        } while (cursor.moveToNext());
    }

    // Always close cursors and databases to avoid memory leaks
    cursor.close();
    db.close();
    return dbstring;
}

2. Unnecessary List Size Check in Bookmarks Activity

In your bookmarks Activity, you only set the Adapter if books.size() > 0. This means even after fixing the cursor issue, you might miss setting the Adapter accidentally. Remove the size check to simplify the code and ensure the ListView always has an Adapter:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.bookmarks);

    mydbhandlerbook bookmark = new mydbhandlerbook(this, null, null, 1);
    final List<String> books = bookmark.databasetostring();

    // Set the adapter regardless of list size (shows empty state if no bookmarks)
    ArrayAdapter<String> myadapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, books);
    ListView mylist = findViewById(R.id.bookmark_list);
    mylist.setAdapter(myadapter);

    mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String url = books.get(position);
            // Update this intent to launch your main WebView activity
            Intent intent = new Intent(bookmarks.this, MainActivity.class);
            intent.putExtra("bookmark_url", url);
            startActivity(intent);
        }
    });
}

3. Other Minor Improvements

  • Remove ImageView from websites class: You can't store ImageView objects in a SQLite database—this field is unnecessary and should be deleted to avoid confusion.
  • Fix SQL Injection Risk in deleteurl(): Your current delete method uses unsafe string concatenation. Use parameterized queries instead:
    public void deleteurl(String url) {
        SQLiteDatabase db = getWritableDatabase();
        db.delete(TABLE_NAME, COLUMN_NAME + " = ?", new String[]{url});
        db.close();
    }
    
  • Verify Bookmark Insert Code Runs: Double-check that the code where you add yandex, google, etc., is actually executing (e.g., place it in MainActivity's onCreate method or an onClick listener for your add bookmark button).

After making these changes, your bookmarks should correctly appear in the ListView when you open the bookmarks Activity.

内容的提问来源于stack exchange,提问作者Isuru Thiwanka

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 08:47:43