0

我创建了一个交错网格布局来从 Firebase 加载图像,但它无法正常工作。使用 picasso 加载图像并使用实时数据库读取项目...当我使用 Log 时,Log cat 中没有显示错误。仍然将空白页面显示为空白活动

// private List<PostItem> postItems;
public static List<PostItem> postItems;

//////////////
private OnItemClickListener mListener;
public interface OnItemClickListener {
    void onItemClick(int position);

}
public void setOnItemClickListener(OnItemClickListener listener) {
    mListener = listener;
}

///////////////

public PostsAdapter(List<PostItem> postItems) {
    PostsAdapter.postItems = postItems;
    
}

@NonNull
@Override
public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    return new PostViewHolder(
            LayoutInflater.from(parent.getContext()).inflate(
                    R.layout.post_item_container,parent,false

            )
    );

}

@Override
public void onBindViewHolder(@NonNull PostViewHolder holder, int position) {
    holder.setPostImage(postItems.get(position));
    
}

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

}

class PostViewHolder extends RecyclerView.ViewHolder{

    RoundedImageView postImageView;


    PostViewHolder(@NonNull View itemView) {
        super(itemView);
        
        postImageView = itemView.findViewById(R.id.imagePost);


        ///////
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    int position = getAdapterPosition();

                    //if (position != RecyclerView.NO_POSITION) {
                    //   mListener.onItemClick(position);
                    //}
                }                 
            });
            ///////
        }
        void setPostImage(PostItem postItem){
            //load image here in picasso.
           
            Picasso.get().load(postItem.getPit()).into(postImageView);
           // postImageView.setImageResource(postItem.getImage());
        }
    }
}

上面使用的代码PostsAdapter.class 还有另一个类,PostItem.class 下面的代码用于读取数据并写入列表数组 - gallery.java

public class Gallery extends AppCompatActivity {

    DatabaseReference databaseL;
    PostItem postItem;
    public static List<PostItem> postItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gallery);
        //
        databaseL = FirebaseDatabase.getInstance().getReference("galumon");
        postItem = new PostItem();

        final RecyclerView postRecyclerView = findViewById(R.id.postRecyclerview);
        postRecyclerView.setLayoutManager(
                new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL)
        );
        postItems = new ArrayList<>();
        /*
        List<PostItem> postItems = new ArrayList<>();
        postItems.add(new PostItem(R.drawable.a));
        postItems.add(new PostItem(R.drawable.b));
        postItems.add(new PostItem(R.drawable.c));
        postItems.add(new PostItem(R.drawable.d));
        postItems.add(new PostItem(R.drawable.e));
        postItems.add(new PostItem(R.drawable.f));
        postItems.add(new PostItem(R.drawable.g));
        postItems.add(new PostItem(R.drawable.h));
        postItems.add(new PostItem(R.drawable.i));

         */
        //
        databaseL.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {

                for (DataSnapshot ds : snapshot.getChildren()) {
                    postItem = ds.getValue(PostItem.class);
                    postItems.add(new PostItem(postItem.getPit()));
                    
                }

            }

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

            }
        });

        postRecyclerView.setAdapter(new PostsAdapter(postItems));
        

    }
}
4

1 回答 1

0

这是因为您先将适配器设置为 RecyclerView,然后再更新列表。

我之前假设当您在列表中手动添加图像并设置适配器时它正在工作(参考注释代码)。

尝试这个:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gallery);
    //
    databaseL = FirebaseDatabase.getInstance().getReference("galumon");
    postItem = new PostItem();

    final RecyclerView postRecyclerView = findViewById(R.id.postRecyclerview);
    postRecyclerView.setLayoutManager(
            new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL)
    );
    postItems = new ArrayList<>();
    
    //set the adapter 
    PostsAdapter postsAdapter = new PostsAdapter(postItems);
    postRecyclerView.setAdapter(postsAdapter);

    /*
    List<PostItem> postItems = new ArrayList<>();
    postItems.add(new PostItem(R.drawable.a));
    postItems.add(new PostItem(R.drawable.b));
    postItems.add(new PostItem(R.drawable.c));
    postItems.add(new PostItem(R.drawable.d));
    postItems.add(new PostItem(R.drawable.e));
    postItems.add(new PostItem(R.drawable.f));
    postItems.add(new PostItem(R.drawable.g));
    postItems.add(new PostItem(R.drawable.h));
    postItems.add(new PostItem(R.drawable.i));

     */
    //
    databaseL.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {

            for (DataSnapshot ds : snapshot.getChildren()) {
                postItem = ds.getValue(PostItem.class);
                postItems.add(new PostItem(postItem.getPit()));
            }

            //notify the adapter
            postsAdapter.notifyDataSetChanged();
        }

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

        }
    }); 
}
于 2020-09-06T12:41:50.743 回答