【发布时间】:2018-02-03 15:55:13
【问题描述】:
我正在尝试复制 messageTxt.setText(downloadUrl.toString()); 这行代码所做的 Link here: (one of I/O 2016 demo)。它为 EditText 小部件提供与存储在 firebase 存储中的图像相关联的 url,并将其加载为图像。 Live demo,28:37播放。
就我而言,我打算使用 firebaseui 的 FirebaseRecyclerAdapter 将图像和其他数据(例如图像描述)加载到 recyclerview 上。我已经将数据保存到 firebase 数据库中,如下图所示。如您所见,其中一个值是 image_path,它保存了 firebase 存储中相应图像的 url。我使用taskSnapshot.getDownloadUrl(); 获取图片网址。
但是,当我尝试使用此代码将此图像 url 加载到 TextView 时,
public void setImage_path(String image) {
feed_image = mView.findViewById(R.id.feed_image);
feed_image.setText(image);
}
仅显示文本,并且我希望显示图像的位置显示为空白。我该如何解决这个问题?提前致谢!我如何实现它的完整代码如下。让我知道是否需要任何其他代码,例如我的模型类。
代码:
Feed 片段
public class Feed extends Fragment {
private Button upload;
private String businessID;
private RecyclerView rv;
private DatabaseReference mDatabase;
private static StorageReference mStorage;
private FirebaseRecyclerAdapter<ImageInformation, ImageHolder> mFirebaseAdapter;
public Feed() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mStorage = FirebaseStorage.getInstance().getReference("feed_photos");
...
}
@Override
public void onStart() {
super.onStart();
mFirebaseAdapter = new FirebaseRecyclerAdapter<ImageInformation, ImageHolder>(
ImageInformation.class,
R.layout.card_item_feed,
ImageHolder.class,
mDatabase) {
@Override
public void populateViewHolder(ImageHolder holder, ImageInformation chat, int position) {
Glide.with(Feed.this).load(chat.getImage_path()).into(holder.feed_image);
holder.image_desc.setText(chat.getCaption());
holder.date.setText(chat.getDate_created());
holder.tag.setText(chat.getTags());
}
};
rv.setAdapter(mFirebaseAdapter);
}
public static class ImageHolder extends RecyclerView.ViewHolder{
ImageView feed_image;
TextView image_desc;
TextView date;
TextView tag;
public ImageHolder(View v) {
super(v);
feed_image = v.findViewById(R.id.feed_image);
image_desc = v.findViewById(R.id.image_desc);
date = v.findViewById(R.id.date);
tag = v.findViewById(R.id.tags);
}
}
}
卡片视图 XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="-3dp"
android:layout_marginRight="-3dp"
card_view:cardBackgroundColor="@android:color/white"
card_view:cardCornerRadius="0dp"
card_view:cardUseCompatPadding="false"
card_view:contentPadding="0dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/feed_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="0dp"
android:adjustViewBounds="true"
android:visibility="visible" />
</LinearLayout>
<TextView
android:id="@+id/image_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/linear_layout"/>
<TextView
android:id="@+id/date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/linear_layout"/>
<TextView
android:id="@+id/tags"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/linear_layout"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
编辑:我意识到我错过了我的 Firebase 存储参考。我已经添加了它,但仍然卡住了,因为我找不到如何检索 Uri(selectedImageUri 代表演示中的 uri 变量)。 (参见 Feed Fragment 中的 cmets)
编辑 2: - 更改了 ImageHolder 类 - 更新了 populateViewHolder 方法中的内容 - 更新了卡片视图小部件
【问题讨论】:
-
feed_imagetextView 不应该是 ImageView 吗? -
@UmarHussain,是的,你是对的。我已经更新了我的问题。
标签: android firebase-realtime-database firebase-storage android-recyclerview firebaseui