"No suitable method found to Override" Android 中的函数错误

"No suitable method found to Override" function Error in Android

   public override void OnBindSwipeContentHolder(ListViewHolder holder, int position)

嗨,朋友们,我正在使用 Telerik UI 为 android 控件开发 Android 应用程序 请帮助我 "OnBindSwipeContentHolder" 未找到错误 我该如何解决这个问题。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Com.Telerik.Widget.List;
using Android.Support.V7.Widget;
using System.Collections;

namespace test
{
 public class TelerikListView : Activity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);


        SetContentView(Resource.Layout.ListView);
        // Create your application here
        RadListView listview = FindViewById<RadListView>(Resource.Id.listView);
        ListViewAdapter adapter = new ListViewAdapter(GetListOfCities());
        listview.SetAdapter(adapter);


        //CityAdapter adapterlist = new CityAdapter(GetListOfCities());
        //listview.SetAdapter(adapterlist);

        SelectionBehavior selectionBehavior = new SelectionBehavior();
        listview.AddBehavior(selectionBehavior);
        selectionBehavior.SetSelectionMode(SelectionBehavior.SelectionMode.Single);

        SelectionListener selectionListener = new SelectionListener(this, selectionBehavior);
        selectionBehavior.AddListener(selectionListener);

        SwipeExecuteBehavior swipeExecuteBehavior = new SwipeExecuteBehavior();
        listview.AddBehavior(swipeExecuteBehavior);

        SwipeExecuteListener swipeExecuteListener = new SwipeExecuteListener(adapter);
        swipeExecuteBehavior.AddListener(swipeExecuteListener);

        swipeExecuteBehavior.SwipeLimitStart = -50;
        swipeExecuteBehavior.SwipeLimitEnd = 50;


    }

    private List<City> GetListOfCities()
    {
        List<City> cities = new List<City>();
        cities.Add(new City("London", "United Kingdom"));
        cities.Add(new City("Berlin", "Germany"));
        cities.Add(new City("Madrid", "Spain"));
        cities.Add(new City("Rome", "Italy"));
        cities.Add(new City("Paris", "France"));
        cities.Add(new City("Hamburg", "Germany"));
        cities.Add(new City("Barcelona", "Spain"));
        cities.Add(new City("Munich", "Germany"));
        cities.Add(new City("Milan", "Italy"));
        cities.Add(new City("Cologne", "Germany"));
        return cities;
    }



    public override void OnBindSwipeContentHolder(ListViewHolder holder, int position)
    {
        RelativeLayout mainLayout = (RelativeLayout)holder.ItemView;
        LinearLayout leftLayout = (LinearLayout)mainLayout.GetChildAt(0);
        LinearLayout rightLayout = (LinearLayout)mainLayout.GetChildAt(1);

        Button leftButton = new Button(mainLayout.Context);
        leftButton.Text = "edit";

        Button rightButton = new Button(mainLayout.Context);
        rightButton.Text = "delete";

        leftLayout.RemoveAllViews();
        leftLayout.AddView(leftButton);

        rightLayout.RemoveAllViews();
        rightLayout.AddView(rightButton);
    }

}

public class CityAdapter : ListViewAdapter
{
    public CityAdapter(IList items)
        : base(items)
    {
    }

    public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
    {
        LayoutInflater inflater = LayoutInflater.From(parent.Context);
        View view = inflater.Inflate(Resource.Layout.city_list_item, parent, false);
        return new CityViewHolder(view);
    }

    public override void OnBindListViewHolder(ListViewHolder holder, int position)
    {
        CityViewHolder viewHolder = (CityViewHolder)holder;
        City city = (City)Items[position];
        viewHolder.nameView.Text = city.Name;
        viewHolder.countryView.Text = city.Country;
    }

    public class CityViewHolder : ListViewHolder
    {
        public TextView nameView;
        public TextView countryView;

        public CityViewHolder(View itemView)
            : base(itemView)
        {

            nameView = (TextView)itemView.FindViewById(Resource.Id.nameView);
            countryView = (TextView)itemView.FindViewById(Resource.Id.countryView);
        }
    }
}

public class SelectionCallback : Java.Lang.Object, ActionMode.ICallback
{
    private SelectionBehavior selectionBehavior;
    public SelectionCallback(SelectionBehavior behavior)
    {
        selectionBehavior = behavior;
    }

    public bool OnCreateActionMode(ActionMode mode, IMenu menu)
    {
        MenuInflater inflater = mode.MenuInflater;
        inflater.Inflate(Resource.Menu.menu_main, menu);
        return true;
    }

    public bool OnPrepareActionMode(ActionMode mode, IMenu menu)
    {
        return false;
    }

    public bool OnActionItemClicked(ActionMode mode, IMenuItem item)
    {
        return false;
    }

    public void OnDestroyActionMode(ActionMode mode)
    {
        selectionBehavior.EndSelection();
    }
}
public class SelectionListener : Java.Lang.Object,
SelectionBehavior.ISelectionChangedListener
{

    private ActionMode actionMode;
    private ActionMode.ICallback callback;
    private Activity activity;
    private SelectionBehavior selectionBehavior;

    public SelectionListener(Activity activity, SelectionBehavior selectionBehavior)
    {
        this.callback = new SelectionCallback(selectionBehavior);
        this.activity = activity;
        this.selectionBehavior = selectionBehavior;
    }

    public void OnSelectionStarted()
    {
        actionMode = activity.StartActionMode(callback);
    }
    public void OnItemIsSelectedChanged(int position, bool newValue)
    {
        int selectionCount = selectionBehavior.SelectedItems().Count;
        actionMode.Title = selectionCount.ToString();
    }
    public void OnSelectionEnded()
    {
        actionMode.Finish();
    }
}

/// <summary>
/// Swiping behaviour
/// </summary>
public class SwipeExecuteListener : Java.Lang.Object, SwipeExecuteBehavior.ISwipeExecuteListener
{

    private ListViewAdapter listViewAdapter;
    public SwipeExecuteListener(ListViewAdapter adapter)
    {
        listViewAdapter = adapter;
    }
    public void OnSwipeStarted(int position)
    {
    }
    public void OnSwipeProgressChanged(int position, int offset, View swipeContent)
    {
    }
    public void OnSwipeEnded(int position, int offset)
    {
        int absOffset = Math.Abs(offset);
        if (absOffset > 300)
        {
            listViewAdapter.Remove(position);
        }
        listViewAdapter.NotifySwipeExecuteFinished();
    }
    public void OnExecuteFinished(int position)
    {
    }
}
}

Activity class 没有要覆盖的OnBindSwipeContentHolder 方法。

在 Telerik 中,ListViewAdapterListViewDataSourceAdapter 执行:

onBindSwipeContentHolder

public void onBindSwipeContentHolder(ListViewHolder holder, int position)

Called by RadListView to display the swipe content behind an item at the specified position. This method should update the contents of the com.telerik.widget.list.ListViewHolder#itemView to reflect the swipe content behind the item at the given position.


In order to achieve this we will need to override the ListViewAdapter's onBindSwipeContentHolder(ListViewHolder holder, int position).


我相信你的方法有误class,你应该在你的自定义列表适配器中。

public class CityAdapter : ListViewAdapter
{

~~~~
    public override void OnBindSwipeContentHolder(ListViewHolder holder, int position)
    {
        RelativeLayout mainLayout = (RelativeLayout)holder.ItemView;
        LinearLayout leftLayout = (LinearLayout)mainLayout.GetChildAt(0);
        LinearLayout rightLayout = (LinearLayout)mainLayout.GetChildAt(1);

        Button leftButton = new Button(mainLayout.Context);
        leftButton.Text = "edit";

        Button rightButton = new Button(mainLayout.Context);
        rightButton.Text = "delete";

        leftLayout.RemoveAllViews();
        leftLayout.AddView(leftButton);

        rightLayout.RemoveAllViews();
        rightLayout.AddView(rightButton);
    }

~~~~

}

您正在尝试覆盖 class TelerikListView 中的 OnBindSwipeContentHolder 方法,该方法从 Android 继承 Activity class。 OnBindSwipeContentHolder class 只能被继承 ListViewAdapter (Telerik 控制)的 class 覆盖。

所以基本上将 OnBindSwipeContentHolder 的定义从 TelerikListView class 移动到 CityAdapter class 将解决构建错误。