gpt4 book ai didi

java - 类 RecyclerViewAdapter 中的构造函数 RecyclerViewAdapter 无法应用于给定类型

转载 作者:行者123 更新时间:2023-12-01 18:02:29 26 4
gpt4 key购买 nike

我正在尝试使用接口(interface)方法为我的 RecyclerView CardView 项目创建 onClickListener。我是新手,据我所知,您需要有适配器类的构造函数。我已经创建了构造函数,但仍然收到错误。当我在“RecyclerViewAdapter myrvAdapter = new RecyclerViewAdapter(NewCategory,this);”中输入“this”时,会显示“mData”而不是“onCardViewListener”。

错误:

error: constructor RecyclerViewAdapter in class RecyclerViewAdapter cannot be applied to given types;
required: Context,List<Category>,OnCardViewListener
found: ActivityTwo,ActivityTwo,ActivityTwo,List<Category>
reason: actual and formal argument lists differ in length

我在 Android Studio 中有以下 Java 代码:

RecyclerViewAdapter.java:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {

private Context mContext;
private List<Category> mData;
private OnCardViewListener mOnCardViewListener;

public RecyclerViewAdapter(Context mContext, List<Category> mData, OnCardViewListener onCardViewListener){
this.mContext = mContext;
this.mData = mData;
this.mOnCardViewListener = onCardViewListener;
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

View view;
LayoutInflater mInflater = LayoutInflater.from(mContext);
view = mInflater.inflate(R.layout.cardview_items_categories,parent,false);
return new MyViewHolder(view, mOnCardViewListener);
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

holder.tv_category_title.setText(mData.get(position).getTitle());
holder.img_category_icon.setImageResource(mData.get(position).getIcon());
}

@Override
public int getItemCount() {
return mData.size();
}

public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

TextView tv_category_title;
ImageView img_category_icon;
CardView cardView;
OnCardViewListener mOnCardViewListener;

public MyViewHolder(View itemView, OnCardViewListener onCardViewListener){
super(itemView);

tv_category_title = (TextView) itemView.findViewById(R.id.category_text);
img_category_icon = (ImageView) itemView.findViewById(R.id.category_icon);
cardView = (CardView) itemView.findViewById(R.id.cv_nav);
mOnCardViewListener = onCardViewListener;

itemView.setOnClickListener(this);
}

@Override
public void onClick(View view) {
mOnCardViewListener.onCardViewClick(getAdapterPosition());

}
}

public interface OnCardViewListener{
void onCardViewClick(int position);
}

ActivityTwo.java:

public class ActivityTwo extends AppCompatActivity implements RecyclerViewAdapter.OnCardViewListener {

private static final String TAG = "ActivityTwo";

private SectionsPagerAdapter mSectionsPagerAdapter;

private ViewPager mViewPager;

List<Category> NewCategory;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);

NewCategory = new ArrayList<>();
NewCategory.add(new Category("Food & Beverages",R.drawable.fnb_24));
NewCategory.add(new Category("Administration",R.drawable.admin_white_24dp));
NewCategory.add(new Category("Faculty Blocks",R.drawable.faculty_white_24dp));
NewCategory.add(new Category("Teaching Buildings",R.drawable.teaching_24));
NewCategory.add(new Category("Library & Study Areas",R.drawable.library_white_24dp));
NewCategory.add(new Category("Research Buildings",R.drawable.research_24));
NewCategory.add(new Category("Campus Accommodations",R.drawable.accommt_white_24dp));
NewCategory.add(new Category("Sports",R.drawable.sports_white_24dp));
NewCategory.add(new Category("Student Association",R.drawable.sa_24dp));
NewCategory.add(new Category("Information Services",R.drawable.it_white_24dp));
NewCategory.add(new Category("Islamic Centre & Mosques",R.drawable.mosque_24));
NewCategory.add(new Category("Guard Houses",R.drawable.security_24));

RecyclerView myrv = (RecyclerView) findViewById(R.id.recylcerview_navigation);
RecyclerViewAdapter myrvAdapter = new RecyclerViewAdapter(NewCategory,this);
myrv.setLayoutManager(new GridLayoutManager(this,2));
myrv.setAdapter(myrvAdapter);

mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav_bar);

Menu menu = bottomNavigationView.getMenu();
MenuItem menuItem = menu.getItem(2);
menuItem.setChecked(true);

bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.timetable:
Intent intent0 = new Intent(ActivityTwo.this, MainActivity.class);
startActivity(intent0);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
break;

case R.id.transports:
Intent intent1 = new Intent(ActivityTwo.this, ActivityOne.class);
startActivity(intent1);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
break;

case R.id.navigation:
break;
}
return false;
}
});

}

@Override
public void onCardViewClick(int position) {
}

类别.java:

public class Category {

private String title;
private int icon;

public Category (){
}

public Category(String title, int icon) {
this.title = title;
this.icon = icon;
}

public String getTitle() {
return title;
}

public int getIcon() {
return icon;
}

public void setTitle(String title) {
this.title = title;
}

public void setIcon(int icon) {
this.icon = icon;
}

最佳答案

解决了问题。以前 mContext 用于 LayoutInflater。这导致构造函数需要 3 个参数,并且我收到错误,因为 ActivityTwo.java 中未定义 mContext。因此,通过改变编写 LayoutInflater 代码的方式,我设法将参数数量减少到 2 个,并且代码运行良好。希望这会对构造函数和参数的新手有所帮助。

请参阅下面所做的更改:

View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_items_categories, parent, false);

RecyclerViewAdapter.java:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {

private List<Category> mData;
private OnCardViewListener mOnCardViewListener;

public RecyclerViewAdapter(List<Category> mData, OnCardViewListener onCardViewListener){

this.mData = mData;
this.mOnCardViewListener = onCardViewListener;
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_items_categories, parent, false);
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

holder.tv_category_title.setText(mData.get(position).getTitle());
holder.img_category_icon.setImageResource(mData.get(position).getIcon());
}

@Override
public int getItemCount() {
return mData.size();
}

public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

TextView tv_category_title;
ImageView img_category_icon;
CardView cardView;
OnCardViewListener mOnCardViewListener;

public MyViewHolder(View itemView, OnCardViewListener onCardViewListener){
super(itemView);

tv_category_title = (TextView) itemView.findViewById(R.id.category_text);
img_category_icon = (ImageView) itemView.findViewById(R.id.category_icon);
cardView = (CardView) itemView.findViewById(R.id.cv_nav);
mOnCardViewListener = onCardViewListener;

itemView.setOnClickListener(this);
}

@Override
public void onClick(View view) {
mOnCardViewListener.onCardViewClick(getAdapterPosition());

}
}

public interface OnCardViewListener{
void onCardViewClick(int position);
}

ActivityTwo.java:

public class ActivityTwo extends AppCompatActivity implements RecyclerViewAdapter.OnCardViewListener {

private static final String TAG = "ActivityTwo";

private SectionsPagerAdapter mSectionsPagerAdapter;

private ViewPager mViewPager;

List<Category> NewCategory;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);

NewCategory = new ArrayList<>();
NewCategory.add(new Category("Food & Beverages",R.drawable.fnb_24));
NewCategory.add(new Category("Administration",R.drawable.admin_white_24dp));
NewCategory.add(new Category("Faculty Blocks",R.drawable.faculty_white_24dp));
NewCategory.add(new Category("Teaching Buildings",R.drawable.teaching_24));
NewCategory.add(new Category("Library & Study Areas",R.drawable.library_white_24dp));
NewCategory.add(new Category("Research Buildings",R.drawable.research_24));
NewCategory.add(new Category("Campus Accommodations",R.drawable.accommt_white_24dp));
NewCategory.add(new Category("Sports",R.drawable.sports_white_24dp));
NewCategory.add(new Category("Student Association",R.drawable.sa_24dp));
NewCategory.add(new Category("Information Services",R.drawable.it_white_24dp));
NewCategory.add(new Category("Islamic Centre & Mosques",R.drawable.mosque_24));
NewCategory.add(new Category("Guard Houses",R.drawable.security_24));

RecyclerView myrv = (RecyclerView) findViewById(R.id.recylcerview_navigation);
RecyclerViewAdapter myrvAdapter = new RecyclerViewAdapter(NewCategory,this);
myrv.setLayoutManager(new GridLayoutManager(this,2));
myrv.setAdapter(myrvAdapter);

mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav_bar);

Menu menu = bottomNavigationView.getMenu();
MenuItem menuItem = menu.getItem(2);
menuItem.setChecked(true);

bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.timetable:
Intent intent0 = new Intent(ActivityTwo.this, MainActivity.class);
startActivity(intent0);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
break;

case R.id.transports:
Intent intent1 = new Intent(ActivityTwo.this, ActivityOne.class);
startActivity(intent1);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
break;

case R.id.navigation:
break;
}
return false;
}
});

}

@Override
public void onCardViewClick(int position) {
Log.d(TAG, "onCardViewClick: Clicked!" + position);
}

关于java - 类 RecyclerViewAdapter 中的构造函数 RecyclerViewAdapter 无法应用于给定类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60606145/

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