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       
      } 
     }); 

} 
+0

C哎喲這個答案我已經張貼了一段時間回到所以:http://stackoverflow.com/a/12363961/450534。它有一個完整的解決方案,包括適配器和用於過濾(搜索)的主要活動。這是一個'GridView'。但這個概念是可以互換的。 –

+0

@SiddharthLele對不起,但我沒有從這個解決方案的想法。 – zanky

回答

1

你可以做的是超越您的適配器回到「真正」的歌曲指數getItemId方法。然後,您將從onItemClick回調的id參數中檢索此索引。在onItemClick方法

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

然後只需使用:

試試這個:

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

,並確保您的songsListData被聲明爲final(這樣就可以從訪問它的適配器內) id而不是position

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

Thanx It works perfactly .. U save my lot of time .. – zanky

+0

@fiddler,can you help me in this case ... http://stackoverflow.com/questions/20606766/search-item-in-edittext-from -listview-showing-wrong-result –

+0

實際上我被困在這一點,需要一些解決方案asap ......請 –