如果我错了,请纠正我,我有一个cardview里面有一个按钮,而cardview在片段里面。在我的适配器类中,我正在调用固定器中的按钮,但是它不起作用。
Adapter.java
class MyViewHolder extends RecyclerView.ViewHolder {
TextView name,id,number;
Button btn;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
btn = (Button) itemView.findViewById(R.id.butCheck);
}
public void onClick(int position)
{
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, position+"is clicked", Toast.LENGTH_SHORT).show();
Intent intent= new Intent(Adapter.this,DetailsActivity.class);
startActivity(intent);
}
});
}
}
完整的适配器类
公共类Adapter扩展了RecyclerView。适配器{
Context context;
ArrayList<Profile> profiles;
public Adapter(Context c, ArrayList<Profile> p)
{
context = c;
profiles= p;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.cardlayout,viewGroup,false));
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
if(profiles.get(i).getPermission()) {
myViewHolder.btn.setVisibility(View.VISIBLE);
myViewHolder.onClick(i);
}
}
@Override
public int getItemCount()
{
return profiles.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
Button btn;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
btn = (Button) itemView.findViewById(R.id.butCheck);
}
public void onClick(int position)
{
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, position+"is clicked", Toast.LENGTH_SHORT).show();
Intent intent= new Intent(,DetailsActivity.class);
startActivity(intent);
}
});
}