Java ListView正在播放一首监听器歌曲
我已经创建了一个列表视图(myList)。通过按下ListView上的一个项目,应用程序将引导用户进入PlaySongActivity页面。 我使用searchById函数尝试匹配歌曲ID和数据库中的歌曲。(获取歌曲ID并匹配数据库中的歌曲ID以播放同一首歌曲)但是,我的老师告诉我,我是根据ListView的ID搜索,而不是歌曲 那么,有没有任何方法可以通过歌曲标题进行搜索,或者可能为ListView中的每个项目添加一个ID 我是一名编码初学者,在互联网上搜索了数小时,却没有找到解决方案:( getResourceId的代码Java ListView正在播放一首监听器歌曲,java,arrays,Java,Arrays,我已经创建了一个列表视图(myList)。通过按下ListView上的一个项目,应用程序将引导用户进入PlaySongActivity页面。 我使用searchById函数尝试匹配歌曲ID和数据库中的歌曲。(获取歌曲ID并匹配数据库中的歌曲ID以播放同一首歌曲)但是,我的老师告诉我,我是根据ListView的ID搜索,而不是歌曲 那么,有没有任何方法可以通过歌曲标题进行搜索,或者可能为ListView中的每个项目添加一个ID 我是一名编码初学者,在互联网上搜索了数小时,却没有找到解决方案:( g
public final class AppUtil
{
public static void popMessage(Context context, String message)
{
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}
public static String getResourceId(Context context, View view)
{
String id = context.getResources().getResourceEntryName(view.getId());
return id;
}
resourceId将成为列表视图元素的id(例如,第一个元素id=0,第二个元素id=1等等)
为什么
您正在返回实际的songid,但在
Song selectedSong = mySongCollection.searchById(resourceId); <-- resourceId is already the Id stored in the database and not the index of mySongCollection.
intent.putExtra("id", selectedSong.getId());
您将歌曲ID存储在什么中?在我的song.class文件中哦,好的,很抱歉,因为我仍然无法修复错误,我应该明天交上来:(它不工作。当我更改为intent.putExtra(“ID”,Integer.parseInt(resourceId));应用程序崩溃。我更改searchById方法的另一个应用程序也不起作用。当我在listview中单击该项目时,它会转到我在数据库中输入的最后一首歌曲。它仍然不起作用。它会转到我在列表视图中单击的每首歌曲的最后一首歌曲。是否仍要为listview中的项目提供ID?由changin提供g将其设置为Song selectedSong=allSongs[Integer.parseInt(id)];一旦我尝试搜索某个内容,应用程序现在就会崩溃。现在,我喜欢使用listview id查找songId,因此我想我需要更改它,以便使用listview中的项目id查找songId。但我不确定如何为listview中的项目提供id。你可以使用System.out.println吗(id)在selectedSong=…”行的正上方,它应该打印出在列表中单击的对象的位置
package com.example.musix;
public class Song {
//private attributes are hidden from other classes/files
private String id;
private String title;
private String artiste;
private String fileLink;
private double songLength;
private String coverArt;
public Song(String _id, String _title, String _artiste, String _fileLink, double _songLength, String _coverArt){
this.id = _id;
this.title = _title;
this.artiste = _artiste;
this.fileLink = _fileLink;
this.songLength = _songLength;
this.coverArt = _coverArt;
}
//encapsulation
//SET methods for setting/changing of the values of the attributes
public void setId(String id) {
this.id = id;
}
public void setTitle(String title) {
this.title = title;
}
public void setArtiste(String artiste) {
this.artiste = artiste;
}
public void setFileLink(String fileLink) {
this.fileLink = fileLink;
}
public void setSongLength(double songLength) {
this.songLength = songLength;
}
public void setCoverArt(String coverArt) {
this.coverArt = coverArt;
}
//GET methods allows us to retrieve values of the attributes
public String getId() {
return this.id;
}
public String getTitle() {
return this.title;
}
public String getArtiste() {
return this.artiste;
}
public String getFileLink() {
return this.fileLink;
}
public double getSongLength() {
return this.songLength;
}
public String getCoverArt() {
return this.coverArt;
}
}
public final class AppUtil
{
public static void popMessage(Context context, String message)
{
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}
public static String getResourceId(Context context, View view)
{
String id = context.getResources().getResourceEntryName(view.getId());
return id;
}
String resourceId = AppUtil.getResourceId(SearchSong.this, myList);
Song selectedSong = mySongCollection.searchById(resourceId);
public Song searchById (String id){
Song selectedSong = null;
for(int index=0; index<allSongs.length; index++){
selectedSong = allSongs[index];
if(selectedSong.getId().equals(id)){
return selectedSong;
}
}
return selectedSong;
}
public Song searchById (String id){
//we are returning the song selected by the index of its Arrays
Song selectedSong = allSongs[Integer.parseInt(id)];
return selectedSong;
}
Song selectedSong = mySongCollection.searchById(resourceId); <-- resourceId is already the Id stored in the database and not the index of mySongCollection.
intent.putExtra("id", selectedSong.getId());
intent.putExtra("id", resourceId);