我设置了一个回收者视图成功对象,该对象通过RetroFit从MYSQL服务器获取信息。最后一步是实现单击式侦听器,以便当用户单击“回收者视图”卡时,将其带到显示所有信息的页面。
我一直在关注CodingwithMitch最佳实践教程 https://www.youtube.com/watch?v=69C1ljfDvl0&list=PLceqSj9t5VtrkGKncLPaskYDTK5XkNN13&index=24&t=0s
我一直进行到最后,当我需要更新适配器时,我陷入了错误,指出构造函数不匹配的参数。请参阅“适配器”页面和“片段”页面中的代码以获取更多上下文。
适配器页面:
public class WorkoutLogAdapter extends RecyclerView.Adapter<WorkoutLogAdapter.WorkoutLogViewHolder>{
private Context mContext;
private List<WorkoutLogList> mWorkoutLogList;
private OnLogListener mOnLogListener;
public WorkoutLogAdapter(Context context, List<WorkoutLogList> mWorkoutLogList, OnLogListener onLogListener){
this.mContext = context;
this.mWorkoutLogList = mWorkoutLogList;
this.mOnLogListener = onLogListener;
}
/**
* Method which contains the display i.e Cards!
* @param parent
* @param viewType
* @return
*/
@NonNull
@Override
public WorkoutLogViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(mContext).inflate(R.layout.recyclerview_workoutlog, parent, false);
return new WorkoutLogViewHolder(v, mOnLogListener);
}
/**
* Method which controls the position of the item arraylist in the viewholder.
* @param holder
* @param position
*/
@Override
public void onBindViewHolder(@NonNull WorkoutLogViewHolder holder, int position) {
WorkoutLogList workoutLogList = mWorkoutLogList.get(position);
holder.mTitle.setText(workoutLogList.getAlternativeThought());
}
/**
* Method which makes our recycler list the same size as our arraylist of items.
* @return
*/
@Override
public int getItemCount() {
return mWorkoutLogList.size();
}
public class WorkoutLogViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView mTitle;
OnLogListener onLogListener;
public WorkoutLogViewHolder(@NonNull View itemView, OnLogListener onLogListener) {
super(itemView);
mTitle = itemView.findViewById(R.id.titleWorkoutLog);
this.onLogListener = onLogListener;
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
onLogListener.onLogClick(getAdapterPosition());
}
}
/**
*
*/
public interface OnLogListener{
void onLogClick(int position);
}
片段页面:
public class WorkoutLogFragment extends Fragment implements WorkoutLogAdapter.OnLogListener {
private SharedPreferences loginPref;
private RecyclerView mRecyclerView;
private WorkoutLogAdapter mWorkoutLogAdapter;
private List<WorkoutLogList> mWorkoutLogList;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_workout_log, container, false);
}
@Override
public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
loginPref = getContext().getSharedPreferences("loginPref", Context.MODE_PRIVATE);
final int userId = loginPref.getInt("userId", 0);
mRecyclerView = view.findViewById(R.id.mRecyclerView);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
Call<List<WorkoutLogList>> call = RetrofitClient.getInstance().getApi()
.getLogs(userId);
call.enqueue(new Callback<List<WorkoutLogList>>() {
@Override
public void onResponse(Call<List<WorkoutLogList>> call, Response<List<WorkoutLogList>>response) {
mWorkoutLogList = response.body();
mWorkoutLogAdapter = new WorkoutLogAdapter(getActivity(), mWorkoutLogList, this);
mRecyclerView.setAdapter(mWorkoutLogAdapter);
}
@Override
public void onFailure(Call<List<WorkoutLogList>> call, Throwable t) {
}
});
}
@Override
public void onLogClick(int position) {
mWorkoutLogList.get(position);
Fragment workoutLogFragment = new HomeFragment();
FragmentTransaction workoutLogTransaction = getActivity().getSupportFragmentManager().beginTransaction();
workoutLogTransaction.replace(R.id.fragment_container, workoutLogFragment);
workoutLogTransaction.commit();
}
}
这是Fragment类中的这一行,抛出红线 mWorkoutLogAdapter = new WorkoutLogAdapter(getActivity(),mWorkoutLogList,this);最后,它自己接受“ this”参数。我按照米切斯的讲道来信。它适用于他,但是他在视频中使用MYSQL lite,就像我在使用翻新呼叫一样。因此,我确定我必须以某种方式修改构造函数,但是我不知道该怎么做。有人可以帮我修改代码,使其生效。