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

SQLite Cursor获取列数据及歌曲库ListView点击传参至Activity问题

嘿,这个需求在Android开发里太常见啦,我给你梳理清楚怎么一步步实现,尽量贴合你现有的代码结构~

实现步骤

1. 在ListView点击事件中传递核心数据

首先给你的ListView设置点击监听器,当用户点击某一项时,优先传递歌曲ID(这比直接传所有数据更高效,也更易维护)。如果你的ListView用的是CursorAdapter,点击事件里的long id参数直接就是数据库里的行ID,非常方便;如果是自定义Adapter,就从你的数据源(比如List<Song>)里取出当前项的ID。

代码示例:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // 这里的id就是数据库中对应歌曲的行ID(CursorAdapter场景)
        long songId = id;
        
        // 跳转到目标Activity
        Intent intent = new Intent(YourMainActivity.this, SongDetailActivity.class);
        // 把歌曲ID放进Intent传递
        intent.putExtra("KEY_SONG_ID", songId);
        // 也可以顺便把title传过去,省得再查一次:
        // String currentTitle = ((TextView)view.findViewById(R.id.tv_title)).getText().toString();
        // intent.putExtra("KEY_SONG_TITLE", currentTitle);
        startActivity(intent);
    }
});

2. 在目标Activity接收数据并查询数据库

到了带有两个按钮的Activity后,先取出Intent里的歌曲ID,再调用你DBHelper里的查询方法,获取对应的歌词和视频URL。

代码示例:

public class SongDetailActivity extends AppCompatActivity {
    private long songId;
    private String songLyrics;
    private String videoUrl;
    private DBHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_song_detail);
        
        // 初始化数据库助手
        dbHelper = new DBHelper(this);
        
        // 获取传递过来的歌曲ID
        Intent intent = getIntent();
        if (intent != null) {
            songId = intent.getLongExtra("KEY_SONG_ID", -1); // -1作为默认值,表示未获取到有效ID
            // 如果传了title,可以直接设置到页面标题:
            // String songTitle = intent.getStringExtra("KEY_SONG_TITLE");
            // getSupportActionBar().setTitle(songTitle);
        }
        
        // 查询对应歌曲的详情
        if (songId != -1) {
            Cursor cursor = dbHelper.getSongByID(songId); // 用你已有的查询方法,或者参考下面的优化
            if (cursor != null && cursor.moveToFirst()) {
                // 从Cursor中取出对应字段,注意字段名要和数据库表一致
                songLyrics = cursor.getString(cursor.getColumnIndex("lyrics"));
                videoUrl = cursor.getString(cursor.getColumnIndex("urldelvideo"));
                cursor.close(); // 用完记得关闭Cursor,避免内存泄漏
            }
        }
        
        // 给两个按钮绑定点击逻辑
        Button btnViewLyrics = findViewById(R.id.btn_view_lyrics);
        Button btnViewVideo = findViewById(R.id.btn_view_video);
        
        btnViewLyrics.setOnClickListener(v -> {
            // 显示歌词,比如用Toast、Dialog或者跳转新页面展示完整内容
            Toast.makeText(SongDetailActivity.this, songLyrics, Toast.LENGTH_LONG).show();
        });
        
        btnViewVideo.setOnClickListener(v -> {
            // 处理视频URL,比如打开浏览器跳转
            if (!TextUtils.isEmpty(videoUrl)) {
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(videoUrl));
                startActivity(browserIntent);
            } else {
                Toast.makeText(SongDetailActivity.this, "暂无视频链接", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 关闭数据库连接
        if (dbHelper != null) {
            dbHelper.close();
        }
    }
}

3. 补充:优化DBHelper的查询方法

如果你的DBHelper还没有专门根据ID查询歌曲的方法,可以补充一个,让逻辑更清晰:

public Cursor getSongByID(long songId) {
    SQLiteDatabase db = this.getReadableDatabase();
    // 查询指定ID的歌曲,返回包含所需字段的Cursor
    return db.query(
            "your_song_table_name", // 替换成你的数据库表名
            new String[]{"title", "lyrics", "urldelvideo"},
            "id = ?", // 假设你的ID字段名为id
            new String[]{String.valueOf(songId)},
            null, null, null
    );
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:53:52