0

I'm implementing a searchview in the action bar in one of my activity. In that activity it list all the songs of my phone and I'm using custom listview for it. everything works fine except for the Searchview its showing in the action bar but it is not functioning, its like when I'm trying to type in it nothing happens. I try to search everything about implementing searchview and tried to solve the issue on my own but nothing really works.

this what i have done so far.

searchmenu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/search"
    android:title="@string/Search"
    android:icon="@drawable/cast_ic_mini_controller_play"
    app:showAsAction="always"
    app:actionViewClass="android.support.v7.widget.SearchView" />

searchable.xml

<searchable
xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/search_hint">

androidmanifest.xml

<activity
        android:name=".Music"
        android:label="@string/title_activity_music"
        android:parentActivityName=".backup_menu"
        android:theme="@style/AppTheme.NoActionBar">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.chill.leoj.burp.backup_menu" />
        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable"
            />
    </activity>

this is my custom adapter, Songadapter

    class SongAdapter extends BaseAdapter implements Filterable {
    private ArrayList<SongInfo> _songs = new ArrayList<SongInfo>();
    private ArrayList<SongInfo> songsfilt = new ArrayList<SongInfo>();
    private ItemFilter itemFilter;
    private Context context;
    LayoutInflater inflater;
    int checkAccumulator;

    public SongAdapter(Context _context, ArrayList<SongInfo> songs) {
        this.context = _context;
        this._songs = songs;
        this.songsfilt = songs;
        checkAccumulator = 0;

        getFilter();

    }

    public int getCount() {
        return _songs.size();
    }

    public Object getItem(int position) {
        return _songs.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    @Override
    public Filter getFilter() {
        if (itemFilter == null) {
            itemFilter = new ItemFilter();
        }

        return itemFilter;

    }

    private class ViewHolder {

        protected CheckBox checkBox;
        private TextView tvSongName;
        private TextView tvSongArtist;


    }

    public View getView(final int position, View convertView, ViewGroup parent) {



        ViewHolder viewHolder = null;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.row_songs, parent, false);

            viewHolder = new ViewHolder();

            viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);
            viewHolder.tvSongName = (TextView) convertView.findViewById(R.id.tvSongName);
            viewHolder.tvSongArtist = (TextView) convertView.findViewById(R.id.tvArtistName);
            viewHolder.checkBox.setOnCheckedChangeListener(null);


            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }


        viewHolder.tvSongName.setText(_songs.get(position).getSongname());
        viewHolder.tvSongArtist.setText(_songs.get(position).getArtistname());

        viewHolder.checkBox.setOnCheckedChangeListener(null);
        viewHolder.checkBox.setChecked(_songs.get(position).isSelected());

        viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int getPosition = (Integer) buttonView.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                count = (isChecked) ? count+1 : count - 1;


                if (!_songs.get(getPosition).isSelected()) {

                    _songs.get(getPosition).setSelected(true);
                    _songs.get(getPosition).setSelected(count > 0);
                    getChoice.setEnabled(true);
                    Log.e("URL", _songs.get(position).getSongUrl() + " ");

                } else {
                    _songs.get(getPosition).setSelected(false);
                    _songs.get(getPosition).setSelected(count < 1);
                    getChoice.setEnabled(false);

                }


            }


        });
        convertView.setTag(R.id.tvSongName, viewHolder.tvSongName);
        convertView.setTag(R.id.tvArtistName, viewHolder.tvSongArtist);
        convertView.setTag(R.id.checkBox, viewHolder.checkBox);

        viewHolder.checkBox.setTag(position); // This line is important.

        return convertView;
    }

    private class ItemFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults filterResults = new FilterResults();
            if (constraint!=null && constraint.length()>0) {
                ArrayList<SongInfo> tempList = new ArrayList<SongInfo>();

                // search content in friend list
                for (SongInfo user : songsfilt) {
                    if (user.getSongname().toLowerCase().contains(constraint.toString().toLowerCase())) {
                        tempList.add(user);
                    }
                }

                filterResults.count = tempList.size();
                filterResults.values = tempList;
            } else {
                filterResults.count = songsfilt.size();
                filterResults.values = songsfilt;
            }

            return filterResults;
        }

        /**
         * Notify about filtered list to ui
         * @param constraint text
         * @param results filtered result
         */
        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            _songs = (ArrayList<SongInfo>) results.values;
            notifyDataSetChanged();
        }
    }

on my Activity, Music.java

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.search_menu, menu);

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    searchMenuItem = menu.findItem(R.id.search);
    searchView = (SearchView) searchMenuItem.getActionView();

    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setSubmitButtonEnabled(true);
    searchView.setOnQueryTextListener(this);

    return true;
}


@Override
public boolean onQueryTextSubmit(String query) {
    return false;
}

@Override
public boolean onQueryTextChange(String newText) {
    songAdapter.getFilter().filter(newText);
    // use to enable search view popup text
    if (TextUtils.isEmpty(newText)) {
        listView.clearTextFilter();
    }
    else {
        listView.setFilterText(newText.toString());
    }
    return true;
}

need your help guys. Thank you in Advance!

3
  • Are you saying that you aren't able to input text, or you can input text but you can't submit the query? Commented Sep 4, 2017 at 0:17
  • I can input text but nothing happens thats my problem :( it seems like its not functioning. what i want is for example, when I input letter "A" in the searchview the listview will be updated and it will list all the music that starts with letter "A". Commented Sep 4, 2017 at 4:18
  • Please help me. Commented Sep 4, 2017 at 9:40

1 Answer 1

1

Swap out BaseAdapter for ArrayAdapter and pass songs into the super class constructor. Then remove your _songs variable and access them instead via the super class getItem() method. Once you've done that, change your publishResults() method to directly modify the super class data e.g:

protected void publishResults(CharSequence constraint, FilterResults results) {
    clear();
    addAll((List<SongInfo>)results.values);
    notifyDataSetInvalidated();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.