gpt4 book ai didi

java - Intent 在 recyclerview onclick 中不起作用

转载 作者:行者123 更新时间:2023-12-01 18:17:22 25 4
gpt4 key购买 nike

我创建了一个 recyclerview,它显示来自 firebase 的用户列表,我想在用户单击列表中的某个项目时将用户发送到另一个 Activity ,但它一直说“CurrentActivity 不是封闭标签“每当我呼吁 Intent 时。代码如下

  public class UserActivity extends AppCompatActivity {
private Toolbar mToobar;

private RecyclerView recyclerView;
private UserAdapter adapter;


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

mToobar = findViewById(R.id.toolbar);
setSupportActionBar(mToobar);
Objects.requireNonNull(getSupportActionBar()).setTitle("Find New Friends");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mToobar.setTitleTextColor(Color.parseColor("WHITE"));

recyclerView = findViewById(R.id.recycler);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//recyclerView.setHasFixedSize(true);



FirebaseRecyclerOptions<UserModel> options = new FirebaseRecyclerOptions.Builder<UserModel>()
.setQuery(FirebaseDatabase.getInstance().getReference().child("Users"), UserModel.class)
.build();

adapter = new UserAdapter(options);
recyclerView.setAdapter(adapter);
recyclerView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(UserActivity.this, ProfileActivity.class);
startActivity(i);
}
});
}



@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}

@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}


}

这是适配器类

public class UserAdapter extends FirebaseRecyclerAdapter<UserModel, UserAdapter.UserViewHolder> {



/**
* Initialize a {@link RecyclerView.Adapter} that listens to a Firebase query. See
* {@link FirebaseRecyclerOptions} for configuration options.
*
* @param options
*/
public UserAdapter(@NonNull FirebaseRecyclerOptions<UserModel> options) {
super(options);
}


@Override
protected void onBindViewHolder(@NonNull UserViewHolder holder, int position, @NonNull UserModel model) {

holder.mName.setText(model.getName());
holder.mStatus.setText(model.getStatus());
Picasso.get().load(model.getImage()).placeholder(R.drawable.avatar).into(holder.mImage);

holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(UserActivity.this, ProfileActivity.class);
startActivity(intent);
//heres the error, UserActivity is not an enclosing tag
}
});


}

@NonNull
@Override
public UserViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.single_user_layout, parent, false);
return new UserViewHolder(view);
}

class UserViewHolder extends RecyclerView.ViewHolder{

TextView mName, mStatus;
CircleImageView mImage;

public UserViewHolder(@NonNull View itemView) {
super(itemView);

mName = itemView.findViewById(R.id.user_displayname);
mStatus = itemView.findViewById(R.id.user_status);
mImage = itemView.findViewById(R.id.user_photo);
}
}
}

这是模型类

public class UserModel {
public String name;
String image;
String status;

public UserModel() {
}

@Override
public String toString() {
return "UserModel{" +
"name='" + name + '\'' +
", image='" + image + '\'' +
", status='" + status + '\'' +
'}';
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getImage() {
return image;
}

public void setImage(String image) {
this.image = image;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}
}

有人可以帮助我更好地实现 onClickListener 吗?

最佳答案

在适配器类中使用 holder.itemView.getContext() 而不是 UserActivity.this

holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Context context = holder.itemView.getContext();
Intent intent = new Intent(context, ProfileActivity.class);
context.startActivity(intent);
}
})

并删除了这段代码,因为您需要在 RecyclerView 中的各个 View 上设置点击监听器,而不是在整个 RecyclerView 上设置点击监听器。

recyclerView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(UserActivity.this, ProfileActivity.class);
startActivity(i);
}
});

关于java - Intent 在 recyclerview onclick 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60342922/

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