recyclerview

uidvcgyl  于 2021-07-07  发布在  Java
关注(0)|答案(3)|浏览(369)

我陷入了这个问题,我不知道这是怎么了。我正在尝试使用recyclerview并用firebase实时数据库中的数据填充它。我已经设置了适配器,但问题是onbindviewholder和oncreateviewholder没有被调用。这是密码
工作适配器.java

public class WorkoutAdapter extends RecyclerView.Adapter<WorkoutAdapter.WorkoutViewHolder> {
private List<Workout> workoutList;

public WorkoutAdapter(List<Workout> workoutList) {
    this.workoutList = workoutList;
}

public WorkoutAdapter() {
}

public List<Workout> getWorkoutList() {
    return workoutList;
}

public void setWorkoutList(List<Workout> workoutList) {
    this.workoutList = workoutList;
}

@NonNull
@Override
public WorkoutViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    Log.i("DEBUG MENU", "onCreateViewHolder");
    LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
    ItemWorkoutBinding binding = ItemWorkoutBinding.inflate(layoutInflater, parent, false);
    return new WorkoutViewHolder(binding);
}

@Override
public void onBindViewHolder(@NonNull WorkoutViewHolder holder, int position) {
    Log.i("DEBUG MENU", "onBindViewHolder");
    Workout workout = workoutList.get(position);
    holder.bind(workout);
}

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

class WorkoutViewHolder extends RecyclerView.ViewHolder {
    private ItemWorkoutBinding binding;

    public WorkoutViewHolder(ItemWorkoutBinding binding) {
        super(binding.getRoot());
        this.binding = binding;
    }

    public void bind(Workout workout) {
        binding.setWorkout(workout);
        binding.executePendingBindings();
    }
}

工作片段.java

public class WorkoutPlanFragment extends Fragment {

private WorkoutPlanViewModel mViewModel;
private WorkoutPlanFragmentBinding binding;
private DatabaseReference mDatabase;
Plan plan;
List<Workout> workoutList;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mDatabase = FirebaseDatabase.getInstance().getReference();
}

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {
    WorkoutPlanViewModelFactory workoutPlanViewModelFactory = new WorkoutPlanViewModelFactory(workoutList);
    mViewModel = new ViewModelProvider(this, workoutPlanViewModelFactory).get(WorkoutPlanViewModel.class);
    binding = DataBindingUtil.inflate(inflater, R.layout.workout_plan_fragment, container, false);
    binding.setViewmodel(mViewModel);
    View view = binding.getRoot();
    assert getArguments() != null;
    plan = WorkoutPlanFragmentArgs.fromBundle(getArguments()).getPlan();
    ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(plan.getPlan_name());
    Glide.with(binding.getRoot().getContext()).load(plan.getUri()).into(binding.imageView2);
    return view;
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    binding.setPlan(plan);
    setupRvWorkout();
}

public void setupRvWorkout() {
    RecyclerView recyclerView = binding.workoutRV;
    LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
    recyclerView.setLayoutManager(layoutManager);
    mDatabase.child("exercise_workout").child("data").orderByChild("plan_id").equalTo(plan.getPlan_key()).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            workoutList = new ArrayList<>();
            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                String workout_id = snapshot.child("workout_id").getValue(String.class);
                mDatabase.child("workout").child(workout_id).addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot snapshot) {
                        Workout workout = snapshot.getValue(Workout.class);
                        workout.setWorkout_id(workout_id);
                        Log.d("GET", "workout_name: " + workout.getWorkout_name() + " " + workout.getWorkout_id());
                        workoutList.add(workout);
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError error) {

                    }
                });
            }
            WorkoutAdapter adapter = new WorkoutAdapter();
            recyclerView.setAdapter(adapter);
            adapter.setWorkoutList(workoutList);
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {

        }
    });

}

就这些了,提前谢谢你!
编辑:来自firebase的数据被添加到工作列表中,下面是证据:
添加到列表的数据
运动计划集合
运动系列
训练系列
项目\u workout.xml

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/txt_workout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:fontFamily="@font/roboto_medium"
            android:text="@{workout.workout_name}"
            android:textSize="24sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0" />
    </androidx.constraintlayout.widget.ConstraintLayout>

    <data>

        <variable
            name="workout"
            type="com.example.zahfitclient.model.Workout" />
    </data>
</layout>

训练计划碎片.xml

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/frameLayout2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ui.workoutplan.WorkoutPlanFragment">

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="0dp"
            android:layout_height="180dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:src="@tools:sample/avatars" />

        <TextView
            android:id="@+id/txt_plannameWO"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:fontFamily="@font/roboto_slab_bold"
            android:text="@{plan.plan_name}"
            android:textSize="24sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageView2" />

        <TextView
            android:id="@+id/txt_planlevelWO"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="16dp"
            android:fontFamily="@font/roboto_medium"
            android:text="@{plan.level_name}"
            android:textColor="?android:attr/textColorSecondary"
            app:layout_constraintEnd_toStartOf="@+id/view"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/txt_plannameWO" />

        <TextView
            android:id="@+id/txt_plantypeWO"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="8dp"
            android:fontFamily="@font/roboto_medium"
            android:text="@{plan.type_name}"
            android:textColor="?android:attr/textColorSecondary"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toEndOf="@+id/view"
            app:layout_constraintTop_toBottomOf="@+id/txt_plannameWO" />

        <View
            android:id="@+id/view"
            android:layout_width="2dp"
            android:layout_height="20dp"
            android:layout_marginStart="204dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="204dp"
            android:background="@color/black"
            android:backgroundTint="@color/material_on_surface_emphasis_medium"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/txt_plannameWO" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/workoutRV"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginTop="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/view" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <data>

        <variable
            name="plan"
            type="com.example.zahfitclient.model.Plan" />

        <variable
            name="viewmodel"
            type="com.example.zahfitclient.ui.workoutplan.WorkoutPlanViewModel" />
    </data>
</layout>
lvjbypge

lvjbypge1#

@Override
 public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
         workoutList = new ArrayList < > ();
         for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
             String workout_id = snapshot.child("workout_id").getValue(String.class);
             mDatabase.child("workout").child(workout_id).
             addListenerForSingleValueEvent(new ValueEventListener() {
                 @Override
                 public void onDataChange(@NonNull DataSnapshot snapshot) {
                     Workout workout = snapshot.getValue(Workout.class);
                     workout.setWorkout_id(workout_id);
                     Log.d("GET", "workout_name: " + workout.getWorkout_name() +
                     " " + workout.getWorkout_id());
                     workoutList.add(workout);
                 }

                 @Override
                 public void onCancelled(@NonNull DatabaseError error) {}
             });
         }
         WorkoutAdapter adapter = new WorkoutAdapter();
         recyclerView.setAdapter(adapter);
         adapter.setWorkoutList(workoutList);
         adapter.notifyDataSetChanged();

for each loop 以异步方式和之后的方式填充列表 foreach 你马上打电话来 adapter.setWorkoutList(workoutList) 在你打电话的时候 adapter.setWorkoutList(workoutList) 列表中没有数据,因为您是以异步方式填充列表的。您必须等到数据已提取或使用同步方式提取数据并在所有数据已提取完毕后调用

adapter.setWorkoutList(workoutList);
adapter.notifyDataSetChanged();

为此,您可以这样做:

@Override
 public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
         workoutList = new ArrayList < > ();
         WorkoutAdapter adapter = new WorkoutAdapter();
         recyclerView.setAdapter(adapter);
         int[] num= {0};
         for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
             String workout_id = snapshot.child("workout_id").getValue(String.class);
             mDatabase.child("workout").child(workout_id).
             addListenerForSingleValueEvent(new ValueEventListener() {
                 @Override
                 public void onDataChange(@NonNull DataSnapshot snapshot) {
                     Workout workout = snapshot.getValue(Workout.class);
                     workout.setWorkout_id(workout_id);
                     Log.d("GET", "workout_name: " + workout.getWorkout_name() +
                     " " + workout.getWorkout_id());
                     workoutList.add(workout);
                     num[0]=num[0]++;
                     if(num[0]==dataSnapshot.getChildren().size(){
                          adapter.setWorkoutList(workoutList);
                          adapter.notifyDataSetChanged();
                     }
                 }

                 @Override
                 public void onCancelled(@NonNull DatabaseError error) {}
             });
         }
emeijp43

emeijp432#

呼叫 notifyDataSetChanged 需要在里面 onDataSetChanged 因为这是您向视图显示的列表添加项的地方 workoutList.add(workout) :

mDatabase.child("exercise_workout").child("data").orderByChild("plan_id").equalTo(plan.getPlan_key()).addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        workoutList = new ArrayList<>();
        for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
            String workout_id = snapshot.child("workout_id").getValue(String.class);
            mDatabase.child("workout").child(workout_id).addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {
                    Workout workout = snapshot.getValue(Workout.class);
                    workout.setWorkout_id(workout_id);
                    Log.d("GET", "workout_name: " + workout.getWorkout_name() + " " + workout.getWorkout_id());
                    workoutList.add(workout);
                    adapter.notifyDataSetChanged();
                }

                @Override
                public void onCancelled(@NonNull DatabaseError error) {
                    throw error.toException(); // don't ignore errors
                }
            });
        }
        WorkoutAdapter adapter = new WorkoutAdapter();
        recyclerView.setAdapter(adapter);
        adapter.setWorkoutList(workoutList);
    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {
        throw error.toException(); // don't ignore errors
    }
});

您可以简化这一点,只需在所有数据加载之外构造一次适配器:

WorkoutAdapter adapter = new WorkoutAdapter();
recyclerView.setAdapter(adapter);
workoutList = new ArrayList<>();
adapter.setWorkoutList(workoutList);
mDatabase.child("exercise_workout").child("data").orderByChild("plan_id").equalTo(plan.getPlan_key()).addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        workoutList.clear();
        for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
            String workout_id = snapshot.child("workout_id").getValue(String.class);
            mDatabase.child("workout").child(workout_id).addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {
                    Workout workout = snapshot.getValue(Workout.class);
                    workout.setWorkout_id(workout_id);
                    Log.d("GET", "workout_name: " + workout.getWorkout_name() + " " + workout.getWorkout_id());
                    workoutList.add(workout);
                    adapter.notifyDataSetChanged();
                }

                @Override
                public void onCancelled(@NonNull DatabaseError error) {
                    throw error.toException(); // don't ignore errors
                }
            });
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {
        throw error.toException(); // don't ignore errors
    }
});

这使得你可以更清楚地操纵 workoutList 当你有数据更新,只要你打电话 notifyDataSetChanged() 每当需要更新ui时。
另请参见:
getcontactsfromfirebase()方法返回空列表
在firebase侦听器中设置单例属性值

mzillmmw

mzillmmw3#

您需要更改:

WorkoutAdapter adapter = new WorkoutAdapter();
recyclerView.setAdapter(adapter);
adapter.setWorkoutList(workoutList);
adapter.notifyDataSetChanged();

对此:

WorkoutAdapter adapter = new WorkoutAdapter();
adapter.setWorkoutList(workoutList);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();

并在ondatachange方法中调用此函数
希望能成功。

相关问题