0

我想将搜索功能添加到我的 listView 。我想按歌名搜索。但我没有办法包含这个功能。

这是我的代码:

ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>();
        Cursor cursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null , null, null, null);          
          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);

我没有得到如何过滤我的列表视图的解决方案。

然后我还添加了以下代码:

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) PlayListActivity.this.adapter).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                          
            }
        });

但我无法为我的这个列表视图找到 filterng 代码。任何人都可以帮我找到解决方案吗?提前谢谢。

4

1 回答 1

1

我测试了这个示例代码,它似乎工作。也许它会帮助你解决你的问题:

public class TestActivity extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    EditText inputSearch = new EditText(this);
    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                          
        }
    });     
    getListView().addHeaderView(inputSearch);

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

    {
        HashMap<String, String> song = new HashMap<String, String>();
        song.put("songTitle", "We are made of stars");
        song.put("artist", "Moby");
        songsListData.add(song);
    }
    {
        HashMap<String, String> song = new HashMap<String, String>();
        song.put("songTitle", "Californication");
        song.put("artist", "Red hot chili peppers");
        songsListData.add(song);
    }

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

    setListAdapter(adapter);
}
}

使用以下 playlist_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/songTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/songArtist"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="10sp" />

    <TextView
        android:id="@+id/duration"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="10sp" />

</LinearLayout>
于 2012-11-20T19:03:06.757 回答