How to create a suggestion list for custom searches in Android

I have implemented custom content provider suggestions based on this guide ; it works, however i have a problem with making a list of suggestions. The suggestion list I have has a black background and white text. I want to change it to white background and black text. I did a web search and tried something like this in the styles.xml file:

<style name="MySearchViewStyle" parent="Widget.AppCompat.SearchView">
    <item name="suggestionRowLayout">@layout/list_item_search_suggestion</item>
</style>
<style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
    <item name="searchViewStyle">@style/MySearchViewStyle</item>
</style>

      

My list_item_search_suggestion looks like this:

    <?xml version="1.0" encoding="utf-8"?>
<TextView
    android:id="@android:id/text1"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:gravity="center_vertical"
    android:minHeight="56dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:textColor="@android:color/black"
    tools:text="Blah blah blah"/>

      

However, when I launch the application and search for something, it crashes until the suggestion list appears. I am getting NullPointerException from newView method in SuggestionsAdapter. This adapter offers from the SDK, I am not implementing my own adapter. I just embed the content provider and set it in the searchable.xml file. Also the stack track looks like this:

06-15 21:11:09.253    4063-4063/com.tophatter E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.tophatter, PID: 4063
java.lang.NullPointerException
        at android.support.v7.widget.SuggestionsAdapter.newView(SuggestionsAdapter.java:249)
        at android.support.v7.widget.SuggestionsAdapter.getView(SuggestionsAdapter.java:453)
        at android.widget.AbsListView.obtainView(AbsListView.java:2255)
        at android.widget.ListPopupWindow$DropDownListView.obtainView(ListPopupWindow.java:1585)
        at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
        at android.widget.ListPopupWindow.buildDropDown(ListPopupWindow.java:1167)
        at android.widget.ListPopupWindow.show(ListPopupWindow.java:554)
        at android.widget.AutoCompleteTextView.showDropDown(AutoCompleteTextView.java:1096)
        at android.widget.AutoCompleteTextView.updateDropDownForFilter(AutoCompleteTextView.java:971)
        at android.widget.AutoCompleteTextView.onFilterComplete(AutoCompleteTextView.java:953)
        at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:285)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)

      

+3


source to share


1 answer


I understood that. Basically, the SuggestionsAdapter refers to some widgets that I did not provide in my custom layout. So I copied the default default layout from the source code and changed the background and text color as my own layout. He is working now.



+2


source







All Articles