ClassCastException:Main(MainActivity)无法强制转换为PlayPauseClick(接口)

时间:2017-02-14 17:05:43

标签: android android-fragments methods interface

我试图从我的适配器类调用一个方法..但是我收到了这个错误:

ClassCastException:Main(MainActivity)无法转换为PlayPauseClick(界面)

这是我在 Fragment (UpdatesFragment)中的方法:

     void imageButtonOnClick(View v, int position){
    playPause=(ImageView)v.findViewById(R.id.playPause);
    Product m = productList.get(position);
    playPause.setImageResource(m.getPlayPauseId());
    playPause.setTag(position);
    playPause.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                int pos = (Integer) v.getTag();
                Product m =(Product) productList.get(pos);
                if (paused) {
                m.setPlayPauseId(R.drawable.ic_pause);
                paused=false;
                }else {
                 m.setPlayPauseId(R.drawable.ic_play);
                 paused = true;
                }
                adapter.notifyDataSetChanged();     
           }

        });
    }

这是我的界面(PlayPauseClick):

public interface PlayPauseClick{
void imageButtonOnClick(View v, int position);
}

这是我的适配器

public class FunDapter<T> extends BaseAdapter implements Filterable {



protected List<T> mDataItems;
protected List<T> mOrigDataItems;
protected LongExtractor<T> idExtractor;
protected final Context mContext;
private final int mLayoutResource;
private final BindDictionary<T> mBindDictionary;
private int oddColorRes;
private int evenColorRes;
private FunDapterFilter<T> funDapterFilter;
private Filter mFilter;



public FunDapter(Context context, List<T> dataItems, int layoutResource,
                 BindDictionary<T> dictionary) {
  this(context, dataItems, layoutResource, null, dictionary);
}


public FunDapter(Context context, List<T> dataItems, int layoutResource,
                 LongExtractor<T> idExtractor, BindDictionary<T> dictionary) {
    this.mContext = context;
    this.mDataItems = dataItems;
    this.mOrigDataItems = dataItems;
    this.mLayoutResource = layoutResource;
    this.idExtractor = idExtractor;
    this.mBindDictionary = dictionary;

}


public void updateData(List<T> dataItems) {
    this.mDataItems = dataItems;
    this.mOrigDataItems = dataItems;
    notifyDataSetChanged();
}

@Override
public int getCount() {
    if (mDataItems == null || mBindDictionary == null) return 0;

    return mDataItems.size();
}

@Override
public T getItem(int position) {
    return mDataItems.get(position);
}

@Override
public boolean hasStableIds() {
    if(idExtractor == null) return super.hasStableIds();
    else return true;
}

@Override
public long getItemId(int position) {
    if(idExtractor == null) return position;
    else return idExtractor.getLongValue(getItem(position), position);
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    View v = convertView;
    final GenericViewHolder holder;
    if (null == v) {
        LayoutInflater vi =
                (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(mLayoutResource, null);
        holder = new GenericViewHolder();
        holder.root = v;


        holder.playPause=(ImageView)v.findViewById(R.id.playPause); 

        FunDapterUtils.initViews(v, holder, mBindDictionary);

        v.setTag(holder);


    } else {
        holder = (GenericViewHolder) v.getTag();
    }

    final T item = getItem(position);
    showData(item, holder, position);



    Product m =(Product) mDataItems.get(position);
    holder.playPause.setImageResource(m.getPlayPauseId());
    holder.playPause.setTag(position);
    holder.playPause.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ((PlayPauseClick)mContext).imageButtonOnClick(v, position);        
        }
    });


       return v;
    }

我做错了什么?

1 个答案:

答案 0 :(得分:2)

holder.playPause.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        ((PlayPauseClick)mContext).imageButtonOnClick(v, position);        
    }
});
     

无法将MainActivity强制转换为PlayPauseClick

所以,实施它

public class MainActivity extends AppCompatActivity 
    implements PlayPauseClick { // *** HERE *** //

    @Override
    public void imageButtonOnClick(View v, int position) {
        // TODO: Implement this
    }