2

在这里,我正在创建显示歌曲列表的 ListView。在列表项上单击我正在传递歌曲的位置来播放它。意味着我有functionlke playSong(Int Index)。这确实有效。但是当我执行搜索时,它会显示可搜索的项目,然后在该项目上单击它我并没有得到它的真实位置。

例子 :

清单向我显示了以下项目:1) 与你同在 2) 你能在我这里吗 3) 顺其自然

但是现在当我搜索“你能在我这里吗”时,它会显示“你能在我这里吗”的列表。当我点击这个项目时,它需要 0 索引并播放“与你同在”的歌曲。

那么我的代码中缺少什么?请给我建议。

这是我的代码:

ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>();
        Cursor cursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null , null, null,MediaStore.Audio.Media.TITLE );          
          if (cursor == null) 
          {
              //Query Failed , Handle error.
          }
          else if (!cursor.moveToFirst()) 
          {
             //No media on the device.
          }
          else
          {

              int titleColumn = cursor.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
              int idColumn = cursor.getColumnIndexOrThrow(android.provider.MediaStore.Audio.Media.DATA);
              int artistcolumn = cursor.getColumnIndex(android.provider.MediaStore.Audio.Media.ARTIST);
              int durationcolumn =cursor.getColumnIndex(android.provider.MediaStore.Audio.Media.DURATION);

              for(int i=0;i<cursor.getCount();i++)
              {
                    String thisTitle = cursor.getString(titleColumn);
                    String path = cursor.getString(idColumn);
                    String artist = cursor.getString(artistcolumn);
                    Long duration = cursor.getLong(durationcolumn);
                    Utilities objUtilities = new Utilities();
                    String timeDuration = objUtilities.milliSecondsToTimer(duration);

                   HashMap<String, String> song = new HashMap<String, String>();
                   song.put("songTitle",thisTitle);
                   song.put("songPath", path);
                   song.put("artist", artist);
                   song.put("duration",timeDuration);

                   // Adding each song to SongList
                   songsList.add(song);
                   cursor.moveToNext();
              }
          }


        // looping through playlist
        for (int i = 0; i < songsList.size(); i++) {
            // creating new HashMap
            HashMap<String, String> song = songsList.get(i);

            // adding HashList to ArrayList
            songsListData.add(song);
        }


        // Adding menuItems to ListView
        String[] from = {"songTitle", "artist" , "duration"};
        int[] to={R.id.songTitle,R.id.songArtist, R.id.duration};
         adapter = new SimpleAdapter(this, songsListData,
                R.layout.playlist_item, from, to);

        setListAdapter(adapter);

        // selecting single ListView item
        ListView lv = getListView();

        // listening to single listitem click
        lv.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // getting listitem index
                int songIndex = position;

                // Starting new intent
                Intent in = new Intent(getApplicationContext(),
                        AndroidBuildingMusicPlayerActivity.class);

                // Sending songIndex to PlayerActivity
                in.putExtra("songIndex", songIndex);                
                startActivity(in);  

            }
        });

        inputSearch = (EditText) findViewById(R.id.inputSearch);
        inputSearch.addTextChangedListener(new TextWatcher() {


            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                // When user changed the Text
            ((SimpleAdapter)getListAdapter()).getFilter().filter(cs);   

            }


            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {
                // TODO Auto-generated method stub

            }


            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub                          
            }
        });

}
4

1 回答 1

1

您可以做的是覆盖getItemId适配器的方法以返回“真实”歌曲索引。然后,您将从回调的id参数中检索此索引。onItemClick

尝试这个:

    adapter = new SimpleAdapter(this, songsListData, R.layout.playlist_item, from, to) {
        public long getItemId(int position) { 
            return songsListData.indexOf(getItem(position));
        }
    };

并确保您songsListData的声明是最终的(以便您可以从适配器内部访问它):

final ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>();

然后在您的onItemClick方法中使用id,而不是position

        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // getting listitem index
            int songIndex = id;
            ...
于 2012-11-24T07:15:14.943 回答