gpt4 book ai didi

java - 图像未在 firebase RecyclerView 中显示

转载 作者:行者123 更新时间:2023-12-02 10:24:48 25 4
gpt4 key购买 nike

我正在尝试显示 firebase 数据库中的图像。我认为在 firebase 中一切都很好,但没有显示任何内容。它没有在布局中显示图像,我为 recyclerview 和 item_view.xml 创建了 main_layout 这是我的代码。

MainActivity.java

<i>public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private ImageAdapter mAdapter;


private DatabaseReference mDatabaseRef;
private List<Upload> mUploads;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));

mUploads = new ArrayList<>();

mDatabaseRef = FirebaseDatabase.getInstance().getReference("Data");

mDatabaseRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Upload upload = postSnapshot.getValue(Upload.class);
mUploads.add(upload);
}

mAdapter = new ImageAdapter(MainActivity.this, mUploads);


}

@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(MainActivity.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}

}

ImageAdapter.java

<i>public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageViewHolder> {
private Context mContext;
private List<Upload> mUploads;

public ImageAdapter(Context context, List<Upload> uploads) {
mContext = context;
mUploads = uploads;
}

@Override
public ImageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(mContext).inflate(R.layout.item_view, parent, false);
return new ImageViewHolder(v);
}

@Override
public void onBindViewHolder(ImageViewHolder holder, int position) {
Upload uploadCurrent = mUploads.get(position);
picasso.get()
.load(uploadCurrent.getImageUrl())
.placeholder(R.mipmap.ic_launcher)
.fit()
.centerCrop()
.into(holder.imageView);
}

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

public class ImageViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView;

public ImageViewHolder(View itemView) {
super(itemView);

imageView = itemView.findViewById(R.id.image_view);
}
}
Picasso.Builder picassoBuilder = new Picasso.Builder(mContext);
Picasso picasso = picassoBuilder.build();

}

上传.java

<i>

公开课上传{ 私有(private)字符串mImageUrl;

public Upload() {

}

public Upload( String imageUrl) {
mImageUrl = imageUrl;
}

public String getImageUrl() {
return mImageUrl;
}

public void setImageUrl(String imageUrl) {
mImageUrl = imageUrl;
}

}

调试输出:

E/RecyclerView:未连接适配器;跳过布局

最佳答案

您没有为 recyclerView 设置适配器,并且在从 Firebase 接收数据后也没有更新它。我将通过以下方式解决这个问题。首先,在适配器中添加一个 setter:

public void setUploads(List<Upload> uploads) {
this.mUploads = uploads;
this.notifyDataSetChanged();
}

这将允许您轻松修改适配器的数据集。然后,在您的 Activity 中,更改 onCreate 方法,如下所示:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));

mAdapter = new ImageAdapter(MainActivity.this, new ArrayList<>());
mRecyclerView.setAdapter(mAdapter);

mDatabaseRef = FirebaseDatabase.getInstance().getReference("Data");

mDatabaseRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<Upload> readUploads = new ArrayList<>();
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Upload upload = postSnapshot.getValue(Upload.class);
readUploads.add(upload);
}
mAdapter.setUploads(readUploads);
}

@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(MainActivity.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}

这样,您将从一个空列表开始,因此您的 recyclerView 将不会显示任何内容。然后,当您收到数据时,您只需更新它,在适配器中设置数据集并通知适配器某些内容发生了变化,因此它必须重新绘制列表

关于java - 图像未在 firebase RecyclerView 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54040841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com