如何在适配器中启动活动

时间:2019-05-23 05:05:31

标签: android firebase android-intent android-activity adapter

我想做的是在适配器中启动一个活动,但似乎在另一个适配器上出现错误。

java.lang.ClassCastException: com.lim.fiture.fiture.models.ProgramTracker cannot be cast to com.lim.fiture.fiture.models.ProgramExercise

我的DayExerciseAdapter上的错误点仅包含两点;运动名称和运动代表。

这是我的DayExerciseAdapter 这是错误所在

public class DayExerciseAdapter extends RecyclerView.Adapter<DayExerciseAdapter.ViewHolder> {

private Context context;
private ArrayList<ProgramExercise> programExercises;

public DayExerciseAdapter(Context context, ArrayList<ProgramExercise> programExercises) {
    this.context = context;
    this.programExercises = programExercises;
}

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

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    holder.exerciseName.setText(programExercises.get(position).getExerciseName().toUpperCase());
    holder.exerciseReps.setText(String.valueOf(programExercises.get(position).getReps()));


}

@Override
public int getItemCount() {
    return programExercises.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {

    TextView exerciseName, exerciseReps, doneTxt;

    public ViewHolder(View itemView) {
        super(itemView);
        exerciseName = itemView.findViewById(R.id.exerciseName);
        exerciseReps = itemView.findViewById(R.id.exerciseReps);
        doneTxt = itemView.findViewById(R.id.doneTxt);


    }
}
}

这是我开始活动意图的地方

public class UserProgramWeekAdapter extends RecyclerView.Adapter<UserProgramWeekAdapter.ViewHolder> {

private Context context;
private ArrayList<ProgramTrackerWrapper> programTrackers;


public UserProgramWeekAdapter(Context context, ArrayList<ProgramTrackerWrapper> programTrackers) {
    this.context = context;
    this.programTrackers = programTrackers;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_user_program_week_adapter, parent, false);
    ViewHolder viewHolder = new ViewHolder(view);
    return viewHolder;

}

@Override
public void onBindViewHolder(UserProgramWeekAdapter.ViewHolder holder, int position) {
    Log.d("UserProgramWeekAdapter", "Day: " + programTrackers.get(position).getDay());
    Log.d("UserProgramWeekAdapter", "Size: " + programTrackers.get(position).getProgramTrackers().size());
    holder.dayTxt.setText(programTrackers.get(position).getDay());

    holder.numberofExercises.setText(programTrackers.get(position).getProgramTrackers().size() + " exercise/s");
    boolean isFinished = false;
    final int sublistSize = programTrackers.get(position).getProgramTrackers().size();
    for (int i = 0; i < sublistSize; i++) {
        if (programTrackers.get(position).getProgramTrackers().get(i).isProgramExerciseFinished()) {
            isFinished = true;

}

@Override
public int getItemCount() {
    return programTrackers.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {

    TextView dayTxt, exerciseCompleted, numberofExercises;
    CardView dayCard;

    public ViewHolder(View itemView) {

        super(itemView);

        dayTxt = itemView.findViewById(R.id.dayTxt);
        exerciseCompleted = itemView.findViewById(R.id.exerciseCompleted);
        numberofExercises = itemView.findViewById(R.id.numberofExercises);
        dayCard = itemView.findViewById(R.id.dayCard);
        dayCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((StartProgramActivity) context).setWeekExercisesData(getAdapterPosition());
                ArrayList<ProgramTracker> programTrackerWrappers = ((StartProgramActivity) context).getDayOfWeekExercises();
                if (programTrackerWrappers.size() > 0) {
                    context.startActivity(new Intent(context.getApplicationContext(), DayExerciseActivity.class).putExtra("programExercises", programTrackerWrappers));

                }else {
                    CustomToast customToast = new CustomToast(context);
                    customToast.setTextColor(context.getResources().getColor(R.color.white));
                    customToast.setBackground(context.getResources().getColor(R.color.color_2));

                    customToast.showErrorToast("No exercises for this day.");
                }


            }
        });
    }


}

我得到的是://错误// enter image description here

1 个答案:

答案 0 :(得分:1)

“ DayExerciseActivity中的错误”

首先,您应该使用context而不是context.getApplicationContext,然后讨论问题 您正在通过putExtra中的 programTracker 的Arrylist   context.startActivity(new Intent(context.getApplicationContext(),DayExerciseActivity.class).putExtra(“ programExercises”,programTrackerWrappers));

而且我确定您在DayExerciseActivity中是通过 programExercise 的ArrayList对其进行解析的,这就是您收到此强制转换错误的原因。因为programTracker无法强制转换为programExersce