Я пытаюсь создать onClickListener для своих элементов RecyclerView CardView, используя метод интерфейса. Я новичок и, насколько мне известно, вам нужны конструкторы для класса адаптера. Я создал конструкторы, но все еще получаю ошибку. Когда я набираю «this» в «RecyclerViewAdapter myrvAdapter = new RecyclerViewAdapter (NewCategory, this);», вместо «onCardViewListener» появляется «mData».
Ошибка:
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
I иметь следующий Java код в Android Studio:
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;
}