gpt4 book ai didi

java - [Android][RecyclerView] 方法不会覆盖或实现父类(super class)型的方法

转载 作者:行者123 更新时间:2023-12-01 17:50:05 26 4
gpt4 key购买 nike

我正在开发一个简单的 Android 应用程序,如下 this教程。我在 onBindViewHolder 方法上遇到编译错误,并显示以下消息:

错误:方法没有重写或实现父类(super class)型的方法

这是我的代码:

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class CryptogramPairingAdapter extends RecyclerView.Adapter {
private String[] mDataset;

// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView mTextView;
public ViewHolder(TextView v) {
super(v);
mTextView = v;
}
}

// Provide a suitable constructor (depends on the kind of dataset)
public CryptogramPairingAdapter(String[] myDataset) {
mDataset = myDataset;
}

// Create new views (invoked by the layout manager)
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
TextView v = (TextView) LayoutInflater.from(parent.getContext())
.inflate(R.layout.pair_crypto_recyclerview, parent, false);
CryptogramPairingAdapter.ViewHolder vh = new ViewHolder(v);
return vh;
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
holder.mTextView.setText(mDataset[position]);

}

// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mDataset.length;
}
}

我很困惑,因为代码与引用几乎相同(除了修改后的名称)。

最佳答案

我建议您将 ViewHolder 更改为 ViewHolder 以外的名称,因为它与 ViewHolder 冲突,并且只会导致您无法区分 ViewHolderViewHolder

(和 I complained to Google about this lousy practice in their example )

鉴于当前状态的代码,请更改:

public class CryptogramPairingAdapter extends RecyclerView.Adapter

至:

public class CryptogramPairingAdapter extends RecyclerView.Adapter<CryptogramPairingAdapter.ViewHolder>

关于java - [Android][RecyclerView] 方法不会覆盖或实现父类(super class)型的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51236637/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com