可搜索列表视图

时间:2012-11-24 06:40:57

标签: android android-listview android-edittext listactivity simpleadapter

这里我正在创建显示歌曲列表的ListView。在列表项目上单击我正在通过歌曲的位置来播放它。意味着我有playlke playSong(Int Index)。这在某种程度上是有效的。但是,当我执行搜索时,它会显示可搜索的项目,并在该项目上点击它我不是它的真实位置。

示例:

列表显示我的项目如1)与你在一起2)你能在这里我3)让它成为

但是现在当我搜索“你能来到我这里”时,它会显示“你能来我这里”的清单。当我点击这个项目时,它需要0索引并播放“Be with You”歌曲。

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

这是我的代码:

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

}

1 个答案:

答案 0 :(得分: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;
            ...