Search code examples
javaandroidgenericsparcelable

Adding values to an object that extends parcelable - Android


I am trying to implement this search box to my project. I need to create a list which includes all the suggestions when the user type some text in the text area but I am stuck.

The list I need to create has this form:

List<? extends SearchSuggestion>

and SearchSuggestion is an interface which extends Parcelable :

import android.os.Parcelable;

public interface SearchSuggestion extends Parcelable{
    String getBody();
}

I guess I have to create a class which implements SearchSuggestion and put some values there?

Something like this?

public class MyClass implements SearchSuggestion {
    public String mystring;

     public MyClass(String text){
         this.mystring = text;
     }
}

I really don't have any idea how to proceed with this problem. I don't know what else to post here to help you help me, so if you need anything just tell me.


Solution

  • When you implement SearchSuggestion it gives you the method String getBody();

    Therefore your object would look like this:

    public class MyClass implements SearchSuggestion {
        private final String myString;
    
         public MyClass(String text){
             this.myString = text;
         }
    
         @Override
         public String getBody() {
              return myString;
         }
    }
    

    Also your list does not need a wildcard (?) it can just be:

     List<SearchSuggestion>
    

    All of this is explained in the ReadMe of the repo you linked:

    https://github.com/arimorty/floatingsearchview/blob/master/README.md

    it has a demo that does what you are looking for:

    https://github.com/arimorty/floatingsearchview/blob/ef70366635e19ba1c1337c7df7b2941f8856f756/sample/src/main/java/com/arlib/floatingsearchviewdemo/data/ColorSuggestion.java

    &

    https://github.com/arimorty/floatingsearchview/blob/ef70366635e19ba1c1337c7df7b2941f8856f756/sample/src/main/java/com/arlib/floatingsearchviewdemo/data/DataHelper.java#L69

    &

    https://github.com/arimorty/floatingsearchview/blob/957d726ce0ac31c5db9ae0b55bf29878fa6aafb7/sample/src/main/java/com/arlib/floatingsearchviewdemo/fragment/ScrollingSearchExampleFragment.java#L144

    Showing that you add your suggestions like this:

     searchView.swapSuggestions(yourListOfSuggestions);