如何通过检索上传到Firebase数据库或存储的Image,TextViews来设置活动的ImageView,TextViews?

时间:2019-07-02 16:12:34

标签: java android firebase firebase-realtime-database android-recyclerview

我正在创建一个新的android应用程序,该应用程序检索一个图像,使用不同或说管理应用程序已将两个文本上载到Firebase,并且上载的图像和文本视图将被检索到一个卡视图中,如图所示。现在,它在recyclerview中,我希望它像任何其他博客应用程序一样。因此,当用户单击该卡片视图,imageview,标题时,所有这些视图将按照默认布局排列,如图所示在下面的图像中。我的意思是将图像视图设置为从Firebase检索到的图像,并将两个文本视图都设置为从Firebase检索到的文本。因此,当任何用户单击从Firebase检索到的任何博客帖子时,都应打开该默认布局并且所有视图将转到声明的位置。如何实现此目标?我用来在cardview中检索和显示内容的代码如下。由于我是stackoverflow的新手,所以我没有足够的声誉来添加图像。请通过下面的图像链接。 形象一 https://ibb.co/kJtvNTm

https://ibb.co/KXB8fdj

这是出于我自己的教育目的,我是开发android应用程序和firebase的新手。我曾尝试检索它们,但并没有将它们放回原处的意愿

PostRecyclerActivity.java


    private RecyclerView mRecyclerView;
    private PostImageAdapter mAdapter;

    private ProgressBar mProgressCircle;

    private DatabaseReference mDatabaseRef;
    private List<PostUpload> mUploads;

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

        mRecyclerView = findViewById(R.id.post_recycler_view);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        mProgressCircle = findViewById(R.id.post_progress_circle);

        mUploads = new ArrayList<>();

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

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

                mAdapter = new PostImageAdapter(PostImageRecyclerActivity.this, mUploads);

                mRecyclerView.setAdapter(mAdapter);
                mProgressCircle.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Toast.makeText(PostImageRecyclerActivity.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
                mProgressCircle.setVisibility(View.INVISIBLE);
            }
        });
    }
}

PostImageAdapter.java

public class PostImageAdapter extends RecyclerView.Adapter<PostImageAdapter.ImageViewHolder> {
    private Context mContext;
    private List<PostUpload> mUploads;

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

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

    @Override
    public void onBindViewHolder(ImageViewHolder holder, int position) {
        PostUpload uploadCurrent = mUploads.get(position);
        holder.textViewName.setText(uploadCurrent.getHeading());
        Picasso.get()
                .load(uploadCurrent.getmImageUrl())
                .fit()
                .centerCrop()
                .into(holder.imageView);
    }

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

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

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

            textViewName = itemView.findViewById(R.id.text_view_name);
            imageView = itemView.findViewById(R.id.post_image_view_upload);
        }
    }
}

PostUpload.java

public class PostUpload {

    private String mHeading;
    private String mMatter;
    private String mImageUrl;

    public PostUpload() {
    }

    public PostUpload(String heading, String matter, String imageUrl) {
        if (heading.trim().equals("")) {

            heading = "No Name";
        }
        mHeading = heading;
        mMatter = matter;
        mImageUrl = imageUrl;

    }

    public String getHeading(){
        return mHeading;
    }
    public void setHeading(String name){
        mHeading=name;
    }
    public String getMatter(){
        return mMatter;
    }
    public void setMatter(String name){
        mMatter=name;
    }
    public String getmImageUrl(){
        return mImageUrl;
    }
    public  void setImageUrl(String imageUrl){
        mImageUrl=imageUrl;
    }
}


我希望单击博客文章后的输出应该将图像和标题设置在如图2所示的位置,然后从firebase检索问题并将其设置到另一个文本视图,如图2所示。

1 个答案:

答案 0 :(得分:0)

从Firebase获取数据时,会将其存储在mUploads类型的PostUpload数组中,并将要显示在CardViews中的数据

这里

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

                mAdapter = new PostImageAdapter(PostImageRecyclerActivity.this, mUploads);

                mRecyclerView.setAdapter(mAdapter);
                mProgressCircle.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Toast.makeText(PostImageRecyclerActivity.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
                mProgressCircle.setVisibility(View.INVISIBLE);
            }
        });

当您点击mRecyclerView.setAdapter(mAdapter);时,您已经填充了数组并将信息显示给用户。

之后,您可以使用适配器中的getItem()

您只需要在PostImageAdapter内覆盖该方法

public PostUpload getItem(int position) {
  return mUploads.get(position);
}

一旦有了此方法,就可以访问PostRecyclerActivity.java

中从Firebase填充的数组中的任何项目。

因此,在RecyclerView中单击某个项目后,您可以获取位置并从该位置获取对象信息

Here是实现点击recyclerview的每一行的许多好方法(我建议第一行)

因此,在实现recyclerview中每一行的点击之后,只需将该数据通过捆绑包或其他方式传递给其他活动

伪代码示例

recyclerView.onClick{...
  public void recyclerViewListClicked(View v, int position){
    if(mAdapter.getItemCount() > 0){
    PostUpload post = mAdapter.getItem(position);
    }else{
     Toast("There is no data into the element");
    }

 //Go to another Activity
  Intent intent = new Intent(PostRecyclerActivity.this,yourSecondActivity.class);
  intent.putExtra(post.getHeadding,"postheading");
  intent.putExtra(post.getMatter,"postmatter");
  //You keep doing the same with the other data you need to send out.
  startActivity(intent);
}

如何从Activity2获取数据

Intent intent = getIntent();
String postheading = intent.getStringExtra("postheading");
String postmatter = intent.getStringExtra("postmatter");

由于图片是类型为String的URL,因此只需像以前一样将其传递为intent.putExtra()即可,您可以从其他活动中获取该URL并为图片充气。