亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

ListView OnItemClickListener 歌曲

ListView OnItemClickListener 歌曲

慕神8447489 2023-05-10 15:07:05
我創建了一個 ListView (myList)。通過按下 ListView 上的其中一項,應用程序應該將用戶定向到 PlaySongActivity 頁面。我使用了 searchById 函數來嘗試匹配歌曲的 ID 和我數據庫中的歌曲。(獲取歌曲的 ID 并匹配數據庫中的歌曲 ID 以播放同一首歌曲)但是,我的老師告訴我我正在搜索ListView 的 ID,而不是歌曲。那么有什么方法可以按歌曲名稱搜索或可能為 ListView 中的每個項目添加一個 ID?我是編碼的初學者,已經搜索了幾個小時,但在互聯網上找不到解決方案:(private SongCollection mySongCollection = new SongCollection();myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {                Intent intent = new Intent(SearchSong.this, PlaySongActivity.class);                String resourceId = AppUtil.getResourceId(SearchSong.this, myList);                Song selectedSong = mySongCollection.searchById(resourceId);                AppUtil.popMessage(SearchSong.this, "Streaming music: " + selectedSong.getTitle());                intent.putExtra("id", selectedSong.getId());                intent.putExtra("title", selectedSong.getTitle());                intent.putExtra("artiste", selectedSong.getArtiste());                intent.putExtra("fileLink", selectedSong.getFileLink());                intent.putExtra("coverArt", selectedSong.getCoverArt());                startActivity(intent);            }        });SongCollection.class代碼    package com.example.musix;public class SongCollection {    private Song[] allSongs = new Song[9];    public SongCollection (){        prepareSongs();    }
查看完整描述

1 回答

?
慕森卡

TA貢獻1806條經驗 獲得超8個贊

String resourceId = AppUtil.getResourceId(SearchSong.this, myList);

 Song selectedSong = mySongCollection.searchById(resourceId);

resourceId 將成為列表視圖元素的 id(例如,第一個元素 id = 0、第二個 id = 1 等等)。


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;

}

為什么?:


您返回實際的 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。這沒有意義,因為您已經可以識別出實際的歌曲。因此,要么應用這些更改,要么更改此行:


intent.putExtra("id", resourceId);


查看完整回答
反對 回復 2023-05-10
  • 1 回答
  • 0 關注
  • 281 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號