java.lang.IndexOutOfBoundsException:索引:0,大小:0 在java.util.ArrayList.get(ArrayList.java:437) 在com.example.masterart.Adapter.VideoAdapter.onBindViewHolder(VideoAdapter.java:62) 在com.example.masterart.Adapter.VideoAdapter.onBindViewHolder(VideoAdapter.java:35)
public class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.ViewHolder>{
public Context mContext;
public List<Post> mPost;
public String video_token;
public MediaController mediaControls;
private FirebaseUser firebaseUser;
public VideoAdapter(Context mContext, List<Post> mPost) {
this.mContext = mContext;
this.mPost = mPost;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.video_item ,parent,false);
mediaControls = new MediaController(mContext);
return new VideoAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
Post post = mPost.get(position);
video_token = FirebaseDatabase.getInstance().getReference().child("Videos").child(post.getVideo_id()).child(post.getVideo()).toString();
try{
holder.videoView.setMediaController(mediaControls);
holder.videoView.setVideoURI(Uri.parse(video_token));
} catch (Exception e){
Log.e("Error", e.getMessage());
e.printStackTrace();
}
holder.videoView.requestFocus();
holder.videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
holder.videoView.start();
}
});
publisherInfo(holder.image_profile,holder.publisher,post.getVideo_uploader());
}
@Override
public int getItemCount() {
return 1;
}
public class ViewHolder extends RecyclerView.ViewHolder{
public ImageView image_profile,like;
public TextView likes,publisher;
public VideoView videoView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
image_profile = itemView.findViewById(R.id.image_profile);
videoView = itemView.findViewById(R.id.video_id);
like = itemView.findViewById(R.id.like);
likes = itemView.findViewById(R.id.likes);
publisher = itemView.findViewById(R.id.publisher);
}
}
private void publisherInfo(final ImageView image_profile, final TextView publisher, String userid)
{
try {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users").child(userid);
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
Glide.with(mContext).load(user.getImageurl()).into(image_profile);
publisher.setText(user.getUsername());
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}catch (Exception e)
{
Toast.makeText(mContext, "Error " + e, Toast.LENGTH_SHORT).show();
}
}
}
public class HomeFragment extends Fragment {
private RecyclerView recyclerView;
private VideoAdapter videoAdapter;
private List<Post> videoLists;
private List<String> followingList;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
recyclerView = view.findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(linearLayoutManager);
videoLists = new ArrayList<>();
videoAdapter = new VideoAdapter(getContext(),videoLists);
recyclerView.setAdapter(videoAdapter);
checkFollowing();
return view;
}
private void checkFollowing()
{
try {
followingList = new ArrayList<>();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Follow").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("following");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
followingList.clear();
for(DataSnapshot snapshot : dataSnapshot.getChildren())
{
followingList.add(snapshot.getKey());
}
readPosts();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}catch (Exception e)
{
Toast.makeText(getContext(), "Error" + e, Toast.LENGTH_SHORT).show();
}
}
private void readPosts()
{
try {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Posts");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
{
videoLists.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren())
{
Post video = snapshot.getValue(Post.class);
for (String id : followingList)
{
if (video.getVideo_uploader().equals(id))
{
videoLists.add(video);
}
}
}
videoAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}catch (Exception e)
{
Toast.makeText(getContext(), "Error" + e, Toast.LENGTH_SHORT).show();
}
}
}