英文:
error: incompatible types: <anonymous Callback<List<UserDataResponse>>> cannot be converted to OnNoteListener
问题
这段代码有什么问题吗?
我使用了Activity并将其传递给了Fragment
RecyclerView的OnClickListener是我在这个代码方法中使用的。
我只是想点击RecyclerView以便将我发送到另一个页面。
请立即帮助我
这是我的UsersAdapter
public class UsersAdapter extends RecyclerView.Adapter<UsersAdapter.UsersViewHolder> {
private Context mCtx;
private ArrayList<UserDataResponse> userDataResponseList = new ArrayList<>();
private OnNoteListener mOnNoteListener;
public UsersAdapter(Context mCtx, ArrayList<UserDataResponse> userDataResponseList, OnNoteListener onNoteListener) {
this.mCtx = mCtx;
this.userDataResponseList = userDataResponseList;
this.mOnNoteListener = onNoteListener;
}
@NonNull
@Override
public UsersViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mCtx).inflate(R.layout.recycle_view, parent, false);
return new UsersAdapter.UsersViewHolder(view, mOnNoteListener);
}
@Override
public void onBindViewHolder(@NonNull UsersAdapter.UsersViewHolder holder, int position) {
holder.textViewName.setText("Username: " + userDataResponseList.get(position).getRTMusername());
holder.textViewEmail.setText("Firstname: " + userDataResponseList.get(position).getRTMfirstname());
holder.textViewLastname.setText("Lastname: " + userDataResponseList.get(position).getRTMlastname());
holder.textViewDepartment.setText("Department: " + userDataResponseList.get(position).getRTMdepartment());
}
@Override
public int getItemCount() {
return userDataResponseList.size();
}
class UsersViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView textViewName, textViewEmail, textViewLastname, textViewDepartment;
OnNoteListener onNoteListener;
public UsersViewHolder(@NonNull View itemView, OnNoteListener onNoteListener) {
super(itemView);
textViewName = itemView.findViewById(R.id.textViewName);
textViewEmail = itemView.findViewById(R.id.textViewEmail);
textViewLastname = itemView.findViewById(R.id.textViewLastname);
textViewDepartment = itemView.findViewById(R.id.textViewDepartment);
this.onNoteListener = onNoteListener;
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
onNoteListener.onNoteClick(getAdapterPosition());
}
}
public interface OnNoteListener {
void onNoteClick(int position);
}
}
这是我的AdminUser.java
public class AdminUserFragment extends Fragment implements UsersAdapter.OnNoteListener {
private RecyclerView recyclerView;
private UsersAdapter adapter;
Api api;
ArrayList<String> Userdata;
ArrayList<UserDataResponse> userDataResponseArrayList = new ArrayList<>();
Redirectinglog redirectinglog;
private Handler refresherHandler = new Handler();
public AdminUserFragment() {
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_adminuser, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Userdata = new ArrayList<>();
redirectinglog = new Redirectinglog(getActivity());
api = RetrofitClientInstance.GetResultAPI().create(Api.class);
recyclerView = view.findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
refreshRunnable.run();
}
private void responseuserdata() {
Call<List<UserDataResponse>> usercall = api.getUsers();
usercall.enqueue(new Callback<List<UserDataResponse>>() {
@Override
public void onResponse(Call<List<UserDataResponse>> call, Response<List<UserDataResponse>> response) {
userDataResponseArrayList = new ArrayList<>(response.body());
for (UserDataResponse UserDataResponsetest : userDataResponseArrayList) {
adapter = new UsersAdapter(getActivity(), userDataResponseArrayList, this);
recyclerView.setAdapter(adapter);
}
}
@Override
public void onFailure(Call<List<UserDataResponse>> call, Throwable t) {
Toast.makeText(getActivity(), t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
private Runnable refreshRunnable = new Runnable() {
@Override
public void run() {
responseuserdata();
refresherHandler.postDelayed(this, 10000);
}
};
@Override
public void onNoteClick(int position) {
}
}
我应该使用什么?
请帮助我,谢谢!呜呜
英文:
What is the problem from this code?
I used Activity and passing it to fragment
RecyclerView OnClickListener the one i use in this coding method.
i just want to click the recyclerview to send me in to another page
Please help me right now
Here is my UsersAdapter
public class UsersAdapter extends RecyclerView.Adapter<UsersAdapter.UsersViewHolder> {
private Context mCtx;
private ArrayList<UserDataResponse> userDataResponseList = new ArrayList<>();
private OnNoteListener mOnNoteListener;
public UsersAdapter(Context mCtx, ArrayList<UserDataResponse> userDataResponseList, OnNoteListener
onNoteListener) {
this.mCtx = mCtx;
this.userDataResponseList = userDataResponseList;
this.mOnNoteListener = onNoteListener;
}
@NonNull
@Override
public UsersViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mCtx).inflate(R.layout.recycle_view, parent, false);
return new UsersAdapter.UsersViewHolder(view, mOnNoteListener);
}
@Override
public void onBindViewHolder(@NonNull UsersAdapter.UsersViewHolder holder, int position) {
holder.textViewName.setText("Username: " + userDataResponseList.get(position).getRTMusername());
holder.textViewEmail.setText("Firstname: " + userDataResponseList.get(position).getRTMfirstname());
holder.textViewLastname.setText("Lastname: " + userDataResponseList.get(position).getRTMlastname());
holder.textViewDepartment.setText("Department: " + userDataResponseList.get(position).getRTMdepartment());
}
@Override
public int getItemCount() {
return userDataResponseList.size();
}
class UsersViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView textViewName,textViewEmail,textViewLastname,textViewDepartment;
OnNoteListener onNoteListener;
public UsersViewHolder(@NonNull View itemView, OnNoteListener onNoteListener) {
super(itemView);
textViewName = itemView.findViewById(R.id.textViewName);
textViewEmail = itemView.findViewById(R.id.textViewEmail);
textViewLastname = itemView.findViewById(R.id.textViewLastname);
textViewDepartment = itemView.findViewById(R.id.textViewDepartment);
this.onNoteListener = onNoteListener;
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
onNoteListener.onNoteClick(getAdapterPosition());
}
}
public interface OnNoteListener{
void onNoteClick(int postion);
}
}
And here is my AdminUser.java
public class AdminUserFragment extends Fragment implements UsersAdapter.OnNoteListener {
private RecyclerView recyclerView;
private UsersAdapter adapter;
Api api;
ArrayList<String> Userdata;
ArrayList<UserDataResponse> userDataResponseArrayList = new ArrayList<>();
Redirectinglog redirectinglog;
private Handler refresherHandler = new Handler();
public AdminUserFragment() {
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_adminuser,container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Userdata = new ArrayList<>();
redirectinglog = new Redirectinglog(getActivity());
api = RetrofitClientInstance.GetResultAPI().create(Api.class);
recyclerView = view.findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
/////////////////////////////////////////////////////
refreshRunnable.run();
}
private void responseuserdata(){
Call<List<UserDataResponse>> usercall = api.getUsers();
usercall.enqueue(new Callback<List<UserDataResponse>>() {
@Override
public void onResponse(Call<List<UserDataResponse>> call, Response<List<UserDataResponse>> response) {
userDataResponseArrayList = new ArrayList<>(response.body());
for(UserDataResponse UserDataResponsetest : userDataResponseArrayList){
adapter = new UsersAdapter(getActivity(), userDataResponseArrayList, this);
recyclerView.setAdapter(adapter);
}
}
@Override
public void onFailure(Call<List<UserDataResponse>> call, Throwable t) {
Toast.makeText(getActivity(), t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
private Runnable refreshRunnable = new Runnable() {
@Override
public void run() {
responseuserdata();
refresherHandler.postDelayed(this, 10000);
}
};
@Override
public void onNoteClick(int postion) {
}
}
What should I use?
Help me please huhuh
答案1
得分: 0
adapter = new UsersAdapter(getActivity(), userDataResponseArrayList, this);
在这里,您传递了一个匿名类,而不是传递一个其类实现了onNoteListener接口的对象。这就是为什么存在兼容性问题。"this"对应于匿名类,因为您是从一个匿名类中调用它,而该匿名类是作为Usercall.enque参数传递的。
我对您的建议是,不要创建所有这些混乱,您可以在onbindViewHolder中设置一个标记,传递要传递的位置,无论何时用户点击回收站,您都可以在onClick方法中检查它,只需将Onclicklistener连接到回收站。您可以从相关行中检索标记值,然后找出点击了哪个位置。简单又容易。如果您对我所说的有任何困难,请在下面评论,我还会写代码示例。希望我的回答有所帮助,如果有帮助,请将其标记为已接受。干杯。
在您的UserViewHolderClass中创建一个视图作为成员变量,并在这个"ViewHolderClass"的构造函数中写入this.view = view,以将行值分配给它。
//在onBindViewholder类内部写入
MyViewHolder.view.setTag("position");
MyHolder.view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = view.getTag("position");// 这将给您点击的行的位置
}
});
此外,如果您将创建一个成员值视图,您不需要在文本视图中保留其他成员变量,在onCreateViewHolder中,您只需使用view.findviewid("传递文本视图、图像视图等在行中使用的标识符"),并获得行中作为元素的适当部件。这是因为这个视图将拥有所有您在行的xml文件中使用的文本视图、图像视图等。
英文:
adapter = new UsersAdapter(getActivity(), userDataResponseArrayList, this);
Here you are passing an anonymous class instead of passing an object whose class implements onNoteListener interface. Thats why, there is a problem of compatability."this corresponds to an anonymous class because you are calling it from an anonymous class which is passed it as a parameter of Usercall.enque.
My suggestion to you would be instead of creating all that mess, you can simply set a tag in your onbindViewHolder with the position being passed and whenever user taps on the recycler view , you can check it on onClick method which you will be having if you just wire an Onclicklistener to the recycler view.You can retreive the tag value from the row in question and find out which position was clicked.Simple and easy.Comment down if you have any trouble undertsanding in what i said, i will write the code sample as well.Hope my answer helps,if it helps please mark it as accepted.Cheers.
create a view in your UserViewHolderClass as a member variable and inside the constructor of this "ViewHolderClass" write this.view = view, to assign row value to it.
//inside onBindViewholder class, write
MyViewHolder.view.setTag("position")
MyHolder.view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = view.getTag("position")// this will give you
//the position of the row which will be tapped
}
});
Also, if you will create a member value view, yo dont need to keep the other member variable in the text view,in your onCreateViewHolder, you can simplydo view.findviewid("pass id of textview, image view whatever you have in your row ans you wanna find) and get the appropriate widget present as element in your row.This is because this view will have all your text views, image views whatever you have used in row's xml file.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论