我正在创建一个连接到数据库的歌曲库。数据以表格(标题,歌词,urldelvideo)存储,并添加到仅显示标题和ID的列表视图中。
当选择列表视图项目时(在列表视图中它只显示标题),它会将我发送到一个活动,其中有两个按钮可以选择查看歌词或查看视频的网址。
如何将存储的信息传递给每个项目的活动?。
在DBHelper中我有这段代码来获取歌曲的歌词:
public Cursor getLyrics(){
SQLiteDatabase db = this.getWritableDatabase();
String sql = "select Lyrics from Songs";
Cursor c = db.rawQuery(sql, null);
return c;
}
活动歌词我有:
lyrics_tv = (TextView) findViewById(R.id.textView4);
Cursor res = getLyrics();
String str = "";
if(res.moveToFirst()){
str= res.getString(0);
}
lyrics_tv.setText(str);
因为我得到了每个项目中第一个项目的歌词......
答案 0 :(得分:0)
你需要 id 来获取哪首歌才能获得歌词。
您可以将#asd is the image variable
asd=plt.imread(imagen_fl)
#then do some image processing....
canvas.drawImage(asd, 1*cm, 3*cm, 10*cm, 10*cm )
方法更改为: -
AttributeError: 'numpy.ndarray' object has no attribute 'rfind'
您将此与getLyrics
方法中的 id 一起使用,并通过Intent Extra将其传递给第二个活动。
id 可以是定义为 rowid 列别名的列。 e.g。
public Cursor getLyrics(long id){
SQLiteDatabase db = this.getWritableDatabase();
String[] whereargs = new string[]{String.valueOf(id)};
String sql = "select Lyrics from Songs WHERE id=?";
return db.rawQuery(sql, null);
}
或者您可以在获取Listview的数据时选择使用onItemClick
,例如
CREATE TABLE Songs (id INTEGER PRIMARY KEY, .....
如何从Listview中提取 id 的细节取决于ListView的来源。
您还可以更改rowid AS id
以返回包含歌词的字符串,例如: -
SELECT *, rowid AS id FROM Songs
我建议不考虑不使用rawQuery,除非需要它,而是使用便捷方法(getLyrics
方法)并且不尝试使用硬编码列偏移(即{{1中的0)而是使用从列名派生的偏移量,这更灵活,更不容易导致错误。
因此,假设将歌曲的歌词作为字符串返回,我建议: -
public String getLyrics(long id){
String rv = "no lyrics found";
SQLiteDatabase db = this.getWritableDatabase();
String[] whereargs = new string[]{String.valueOf(id)};
String sql = "select Lyrics from Songs WHERE id=?";
Cursor c = db.rawQuery(sql, null);
if (c.moveToFirst()) {
rv = c.getString(0);
}
c.close();
return rv;
}
答案 1 :(得分:0)
要获取字段信息,请执行以下操作:
if (res.getColumnIndex("Your field name") != -1) {
str = cursor.getString(res.getColumnIndex("Your field name"));
}