android.support.v7.widget.SearchView不适合我

时间:2016-07-19 13:50:56

标签: java android android-appcompat

一个月前,我的硬盘有坏道,我丢失了一切。我试图重新开始,但SearchView阻止了我。 这是在我的hdd破坏enter image description here

之前的输出

这里是我的新项目enter image description here

一周我无法确定问题是什么.code:

menu.xml文件

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="@+id/action_settings" android:title="@string/action_settings"
    android:orderInCategory="100" app:showAsAction="never" />
<item android:id="@+id/action_search" android:title="@string/action_search" android:icon="@drawable/ic_search_dark"
    app:showAsAction="collapseActionView|ifRoom" app:actionViewClass="android.support.v7.widget.SearchView"/></menu>

MainActivity.java

public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_settings:
            startActivity(new Intent(this,SettingsActivity.class));
            return true;
        case R.id.action_search:
            return true;
    }

    return super.onOptionsItemSelected(item);
}

我的抽象片段

public abstract class MainFragments extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    return super.onCreateView(inflater, container, savedInstanceState);
}}

我自己的光标

private Cursor getAllArtists(String search) {
    ContentResolver contentResolver = ourContext.getContentResolver();
    String[] projection = {MediaStore.Audio.Artists._ID,
            MediaStore.Audio.Artists.ARTIST,
            MediaStore.Audio.Artists.NUMBER_OF_TRACKS,
            MediaStore.Audio.Artists.NUMBER_OF_ALBUMS};
    if (search != null && search.length() > 0) {
        search = "LOWER("
                + MediaStore.Audio.Albums.ARTIST + ") LIKE LOWER('" + search + "%') ";
    }

    return contentResolver.query(MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI,
            projection,
            search,
            null,
            MediaStore.Audio.Artists.ARTIST + " ASC");

}

public ArrayList<MetaInfo> getArtists(String search) {
    ArrayList<MetaInfo> infos = new ArrayList<>();
    Cursor c = getAllArtists(search);
    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
        MetaInfo info = new MetaInfo();
        info.setId(c.getString(c.getColumnIndex(MediaStore.Audio.Artists._ID)));
        info.setName(c.getString(c.getColumnIndex(MediaStore.Audio.Artists.ARTIST)));
        info.setNumber_of_songs(c.getString(c.getColumnIndex(MediaStore.Audio.Artists.NUMBER_OF_TRACKS)));
        info.setNumber_of_album(c.getString(c.getColumnIndex(MediaStore.Audio.Artists.NUMBER_OF_ALBUMS)));
        infos.add(info);
    }
    c.close();
    return infos;
}

片段和适配器

public boolean onQueryTextSubmit(String query) {
searchName(query);
return false;
}
public boolean onQueryTextChange(String newText) {
searchName(newText);
return false;
}
private void searchName(String s) {
infos = database.getArtists(s);
adapter.addAll(infos);
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu,inflater);
searchView = (SearchView)menu.findItem(R.id.action_search).getActionView();
searchView.setOnQueryTextListener(this);
}
public class ArtistListAdapter extends BaseAdapter {

    public void addAll(ArrayList<MetaInfo> metaInfos) {
    infos.clear();
    this.infos = metaInfos;
    notifyDataSetChanged();
    }
}

1 个答案:

答案 0 :(得分:0)

确保您已更改目标活动的清单。

       <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
            <action android:name="android.intent.action.SEARCH" />
            <action android:name="android.intent.action.VIEW" />
        </intent-filter>