Error CS0115 '…OnBindViewHolder(Object, int)': no suitable method found to override

FedericoNavarreteFedericoNavarrete SVMember ✭✭
edited June 23 in Xamarin.Android

I'm binding this library:

https://github.com/mancj/MaterialSearchBar

And generally it works, however, I have an issue when I try to add the support of the RecyclerView, I added the following libraries:

And I got the following errors:

I tried to follow this advice of creating some partial classes:

https://stackoverflow.com/questions/45754424/xamarin-android-binding-thorw-does-not-implement-inherited-abstract-member-rec

But it didn't work and I started to get duplicates, personally, I believe the main issue is here:

Severity Code Description Project File Line Suppression State
Error CS0115 'SuggestionsAdapter.OnBindViewHolder(Object, int)': no suitable method found to override Xamarin-MaterialSearchBar C:\Users\feder\source\repos\Xamarin-MaterialSearchBar\Xamarin-MaterialSearchBar\obj\Release\generated\src\Com.Mancj.Materialsearchbar.Adapter.SuggestionsAdapter.cs 666 Active

This is the configuration of my VS:

The only dependencies in the Gradle of the project are the following ones:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    testImplementation 'junit:junit:4.12'
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    implementation 'com.android.support:cardview-v7:28.0.0'
}

If you want the compiled aar file and the project to test it.

Which as you can see I have them all. Any idea, what am I missing? Thanks.

Best Answer

  • FedericoNavarreteFedericoNavarrete SV ✭✭
    Accepted Answer

    How to fix this issue? Technically, it's not so simple, the best solution and there are 6 steps to follow:

    1. Add the following NuGet Packages:

      These are the minimum requirements found in the build.gradle.

    2. Remove the class SuggestionsAdapter from the future library from your Metadata.xml with this piece of code (inspired by the Leo Zhu - MSFT' answer).

      <remove-node path="/api/package[@name='com.mancj.materialsearchbar.adapter']/class[@name='SuggestionsAdapter']" />

      Why? Because this section of the code is not properly ported to C# by the binder; perhaps, the reason is that the V represents the RecyclerView.ViewHolder and it's too generic for the binder. You can see the original code here: SuggestionsAdapter.java

      Also, you might ask why I chose to migrate the SuggestionsAdapter over the DefaultSuggestionsAdapter. There are 2 reasons:

      • SuggestionsAdapter is the base class.
      • DefaultSuggestionsAdapter calls XML codes that you cannot access from C#, you can see them in the lines 34, 55 and 56.
    3. Build your library.

    4. Create a new folder in your Additions called Adapter where you need to create a class called SuggestionsAdapter.

    5. Migrate the code from Java to C#.

      namespace Com.Mancj.Materialsearchbar.Adapter
      {
          public abstract class SuggestionsAdapter<S, V> : RecyclerView.Adapter, IFilterable
          {
              private readonly LayoutInflater Inflater;
              protected List<S> Suggestions = new List<S>();
              protected List<S> Suggestions_clone = new List<S>();
              protected int MaxSuggestionsCount = 5;
      
              public void AddSuggestion(S r)
              {
                  if (MaxSuggestionsCount <= 0)
                  {
                      return;
                  }
      
                  if (r == null)
                  {
                      return;
                  }
                  if (!Suggestions.Contains(r))
                  {
                      if (Suggestions.Count >= MaxSuggestionsCount)
                      {
                          Suggestions.RemoveAt(MaxSuggestionsCount - 1);
                      }
                      Suggestions.Insert(0, r);
                  }
                  else
                  {
                      Suggestions.Remove(r);
                      Suggestions.Insert(0, r);
                  }
                  Suggestions_clone = Suggestions;
                  NotifyDataSetChanged();
              }
      
              public void SetSuggestions(List<S> suggestions)
              {
                  Suggestions = suggestions;
                  Suggestions_clone = suggestions;
                  NotifyDataSetChanged();
              }
      
              public void ClearSuggestions()
              {
                  Suggestions.Clear();
                  Suggestions_clone = Suggestions;
                  NotifyDataSetChanged();
              }
      
              public void DeleteSuggestion(int position, S r)
              {
                  if (r == null)
                  {
                      return;
                  }
                  //delete item with animation
                  if (Suggestions.Contains(r))
                  {
                      NotifyItemRemoved(position);
                      Suggestions.Remove(r);
                      Suggestions_clone = Suggestions;
                  }
              }
      
              public List<S> GetSuggestions()
              {
                  return Suggestions;
              }
      
              public int GetMaxSuggestionsCount()
              {
                  return MaxSuggestionsCount;
              }
      
              public void SetMaxSuggestionsCount(int maxSuggestionsCount)
              {
                  MaxSuggestionsCount = maxSuggestionsCount;
              }
      
              protected LayoutInflater GetLayoutInflater()
              {
                  return Inflater;
              }
      
              public SuggestionsAdapter(LayoutInflater inflater)
              {
                  Inflater = inflater;
              }
      
              public abstract int GetSingleViewHeight();
      
              public int GetListHeight()
              {
                  return ItemCount * GetSingleViewHeight();
              }
      
              public abstract void OnBindSuggestionHolder(S suggestion, RecyclerView.ViewHolder holder, int position);
      
              public override int ItemCount => Suggestions.Count;
      
              public Filter Filter => null;
      
              public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
              {
                  OnBindSuggestionHolder(Suggestions[position], holder, position);
              }
      
              public interface IOnItemViewClickListener
              {
                  void OnItemClickListener(int position, View v);
                  void OnItemDeleteListener(int position, View v);
              }
          }
      }
      
    6. Build your project again and that's all! Your library is fully working.

    If you want to check the result.

Answers

  • FedericoNavarreteFedericoNavarrete SVMember ✭✭

    I tried following this advice:
    https://stackoverflow.com/a/56731209/2889347

    But then the methods are hidden when you compile them.

  • FedericoNavarreteFedericoNavarrete SVMember ✭✭
    Accepted Answer

    How to fix this issue? Technically, it's not so simple, the best solution and there are 6 steps to follow:

    1. Add the following NuGet Packages:

      These are the minimum requirements found in the build.gradle.

    2. Remove the class SuggestionsAdapter from the future library from your Metadata.xml with this piece of code (inspired by the Leo Zhu - MSFT' answer).

      <remove-node path="/api/package[@name='com.mancj.materialsearchbar.adapter']/class[@name='SuggestionsAdapter']" />

      Why? Because this section of the code is not properly ported to C# by the binder; perhaps, the reason is that the V represents the RecyclerView.ViewHolder and it's too generic for the binder. You can see the original code here: SuggestionsAdapter.java

      Also, you might ask why I chose to migrate the SuggestionsAdapter over the DefaultSuggestionsAdapter. There are 2 reasons:

      • SuggestionsAdapter is the base class.
      • DefaultSuggestionsAdapter calls XML codes that you cannot access from C#, you can see them in the lines 34, 55 and 56.
    3. Build your library.

    4. Create a new folder in your Additions called Adapter where you need to create a class called SuggestionsAdapter.

    5. Migrate the code from Java to C#.

      namespace Com.Mancj.Materialsearchbar.Adapter
      {
          public abstract class SuggestionsAdapter<S, V> : RecyclerView.Adapter, IFilterable
          {
              private readonly LayoutInflater Inflater;
              protected List<S> Suggestions = new List<S>();
              protected List<S> Suggestions_clone = new List<S>();
              protected int MaxSuggestionsCount = 5;
      
              public void AddSuggestion(S r)
              {
                  if (MaxSuggestionsCount <= 0)
                  {
                      return;
                  }
      
                  if (r == null)
                  {
                      return;
                  }
                  if (!Suggestions.Contains(r))
                  {
                      if (Suggestions.Count >= MaxSuggestionsCount)
                      {
                          Suggestions.RemoveAt(MaxSuggestionsCount - 1);
                      }
                      Suggestions.Insert(0, r);
                  }
                  else
                  {
                      Suggestions.Remove(r);
                      Suggestions.Insert(0, r);
                  }
                  Suggestions_clone = Suggestions;
                  NotifyDataSetChanged();
              }
      
              public void SetSuggestions(List<S> suggestions)
              {
                  Suggestions = suggestions;
                  Suggestions_clone = suggestions;
                  NotifyDataSetChanged();
              }
      
              public void ClearSuggestions()
              {
                  Suggestions.Clear();
                  Suggestions_clone = Suggestions;
                  NotifyDataSetChanged();
              }
      
              public void DeleteSuggestion(int position, S r)
              {
                  if (r == null)
                  {
                      return;
                  }
                  //delete item with animation
                  if (Suggestions.Contains(r))
                  {
                      NotifyItemRemoved(position);
                      Suggestions.Remove(r);
                      Suggestions_clone = Suggestions;
                  }
              }
      
              public List<S> GetSuggestions()
              {
                  return Suggestions;
              }
      
              public int GetMaxSuggestionsCount()
              {
                  return MaxSuggestionsCount;
              }
      
              public void SetMaxSuggestionsCount(int maxSuggestionsCount)
              {
                  MaxSuggestionsCount = maxSuggestionsCount;
              }
      
              protected LayoutInflater GetLayoutInflater()
              {
                  return Inflater;
              }
      
              public SuggestionsAdapter(LayoutInflater inflater)
              {
                  Inflater = inflater;
              }
      
              public abstract int GetSingleViewHeight();
      
              public int GetListHeight()
              {
                  return ItemCount * GetSingleViewHeight();
              }
      
              public abstract void OnBindSuggestionHolder(S suggestion, RecyclerView.ViewHolder holder, int position);
      
              public override int ItemCount => Suggestions.Count;
      
              public Filter Filter => null;
      
              public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
              {
                  OnBindSuggestionHolder(Suggestions[position], holder, position);
              }
      
              public interface IOnItemViewClickListener
              {
                  void OnItemClickListener(int position, View v);
                  void OnItemDeleteListener(int position, View v);
              }
          }
      }
      
    6. Build your project again and that's all! Your library is fully working.

    If you want to check the result.

Sign In or Register to comment.