#android #android-recyclerview #dialog #adapter
#Android #android-recyclerview #диалоговое окно #адаптер
Вопрос:
Я пытаюсь использовать диалоговое окно при нажатии на пункт меню. Я могу сделать это успешно, когда в диалоговом окне нет recyclerview.
Вот код действия, в котором обрабатывается щелчок по элементу меню, а также инициализация диалогового окна и recyclerview:
// Menu Stuff
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.add_exercise_activity_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (item.getItemId() == R.id.timer) {
Toast bread = Toast.makeText(getApplicationContext(), "Timer Selected", Toast.LENGTH_SHORT);
bread.show();
} else if (item.getItemId() == R.id.history) {
// Prepare to show exercise history dialog box
LayoutInflater inflater = LayoutInflater.from(AddExerciseActivity.this);
View view = inflater.inflate(R.layout.exercise_history_dialog, null);
AlertDialog alertDialog = new AlertDialog.Builder(AddExerciseActivity.this).setView(view).create();
// Declare local data structure
ArrayList<WorkoutExercise> All_Performed_Sessions = new ArrayList<>();
// Find all performed sessions of a specific exercise and add them to local data structure
for (int i = 0; i < MainActivity.Workout_Days.size(); i ) {
for (int j = 0; j < MainActivity.Workout_Days.get(i).getExercises().size(); j ) {
if (MainActivity.Workout_Days.get(i).getExercises().get(j).getExercise().equals(exercise_name)) {
All_Performed_Sessions.add(MainActivity.Workout_Days.get(i).getExercises().get(j));
}
}
}
// Set Exercise History Recycler View
RecyclerView recyclerView = findViewById(R.id.recyclerView_Exercise_History);
WorkoutExerciseAdapter4 workoutExerciseAdapter4 = new WorkoutExerciseAdapter4(AddExerciseActivity.this, All_Performed_Sessions);
// Crash Here
recyclerView.setAdapter(workoutExerciseAdapter4);
recyclerView.setLayoutManager(new LinearLayoutManager(AddExerciseActivity.this));
alertDialog.show();
}
return super.onOptionsItemSelected(item);
}
Это код адаптера:
public class WorkoutExerciseAdapter4 extends
RecyclerView.Adapter<WorkoutExerciseAdapter4.MyViewHolder> {
Context ct;
ArrayList<WorkoutExercise> Exercises;
public WorkoutExerciseAdapter4(Context ct, ArrayList<WorkoutExercise> Exercises) {
this.ct = ct;
this.Exercises = new ArrayList<>(Exercises);
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(this.ct);
View view = inflater.inflate(R.layout.workout_exercise_row4, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
// Change TextView text
holder.tv_date.setText(Exercises.get(position).getDate());
// Recycler View Stuff
// Change RecyclerView items
WorkoutSetAdapter workoutSetAdapter = new WorkoutSetAdapter(ct, Exercises.get(position).getSets());
holder.recyclerView.setAdapter(workoutSetAdapter);
holder.recyclerView.setLayoutManager(new LinearLayoutManager(ct));
}
@Override
public int getItemCount() {
return this.Exercises.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView tv_date;
RecyclerView recyclerView;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
tv_date = itemView.findViewById(R.id.tv_date);
recyclerView = itemView.findViewById(R.id.recycler_view_day);
}
}
}
Я пробовал использовать getApplicationContext()
, this
, Activityname.this
, но, похоже, ничего не работает, и я всегда получаю сбой в одной setAdapter(...)
строке.
Ответ №1:
Попробуйте изменить это:
RecyclerView recyclerView = findViewById(R.id.recyclerView_Exercise_History);
К этому:
RecyclerView recyclerView = view.findViewById(R.id.recyclerView_Exercise_History);
Комментарии:
1. Спасибо, это исправлено!
2. Я рад вам помочь. Проверьте, пожалуйста, ответьте как принято.