单击 RecyclerView 后使用 Firebase 实时数据库填充详细信息活动

Posted

技术标签:

【中文标题】单击 RecyclerView 后使用 Firebase 实时数据库填充详细信息活动【英文标题】:Populate details activity with Firebase Real Time Database after click on RecyclerView 【发布时间】:2021-02-23 02:20:50 【问题描述】:

我想在我的回收站视图中单击并打开一个新活动来显示数据。直到现在我 已完成打开活动并显示下一个意图的值“interno”和“siniiga”。如何从 firebase 获得“madre and padre”值?应用添加父亲和母亲的方法,然后我将它用于xml显示的其余值。原谅我可怕的代码。如果有人知道其他方法,请告诉我

附上代码和图片。

主要活动

    RecyclerView rv;
    List<cow> vacas;
    adapter adapter;
    ImageButton mbuttoninf;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rv = (RecyclerView) findViewById(R.id.recyclerViewGirdView);
        rv.setLayoutManager(new LinearLayoutManager(this));
        vacas = new ArrayList<>();


        FirebaseDatabase database = FirebaseDatabase.getInstance();

        adapter = new adapter(vacas);
        rv.setAdapter(adapter);


        database.getReference("Vacas").addValueEventListener(new ValueEventListener() 
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) 
                vacas.removeAll(vacas);
                for (DataSnapshot snapshot1 :
                        snapshot.getChildren()) 
                    cow vaca = snapshot1.getValue(cow.class);
                    vacas.add(vaca);
                
                adapter.notifyDataSetChanged();
            

            @Override
            public void onCancelled(@NonNull DatabaseError error) 

            
        );

    

    String interno;
    String siniiga;
    String ulr;
    String madre;
    String padre;


    public cow() 
    

    public cow(String interno, String siniiga, String ulr, String madre, String padre) 
        this.interno = interno;
        this.siniiga = siniiga;
        this.ulr = ulr;
        this.madre = madre;
        this.padre = padre;
    


    public String getInterno() 
        return interno;
    

    public void setInterno(String interno) 
        this.interno = interno;
    

    public String getSiniiga() 
        return siniiga;
    

    public void setSiniiga(String siniiga) 
        this.siniiga = siniiga;
    

    public String getUlr() 
        return ulr;
    

    public void setUlr(String ulr) 
        this.ulr = ulr;
    

    public String getMadre() 
        return madre;
    

    public void setMadre(String madre) 
        this.madre = madre;
    

    public String getPadre() 
        return padre;
    

    public void setPadre(String padre) 
        this.padre = padre;
    

适配器

    List<cow> vacas;
    Context context;


    public adapter(List<cow> vacas) 
        this.vacas = vacas;
    

    @NonNull
    @Override
    public cowviewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_vaca,parent,false);
        cowviewHolder holder = new cowviewHolder(v);
        return holder;

    

    @Override
    public void onBindViewHolder(@NonNull final cowviewHolder holder, final int position) 
        final cow vacaslist = vacas.get(position);
        holder.textViewinterno.setText(String.valueOf(vacaslist.interno));
        holder.textViewsiniiga.setText(String.valueOf(vacaslist.siniiga));
        Glide.with(holder.imageviewrec.getContext()).load(vacaslist.getUlr()).into(holder.imageviewrec);
        holder.itemView.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                Intent intent=new Intent(v.getContext(),detailactivity.class);
                intent.putExtra("keyint", vacaslist.getInterno());
                intent.putExtra("keysin", vacaslist.getSiniiga());
                v.getContext().startActivity(intent);
            
        );



    

    @Override
    public int getItemCount() 

        return vacas.size();
    

    public static class cowviewHolder extends RecyclerView.ViewHolder 
        TextView textViewinterno, textViewsiniiga, tvmadre, tvpadre;
        ImageView imageviewrec;


        public cowviewHolder(@NonNull View itemView) 
            super(itemView);
            textViewinterno = itemView.findViewById(R.id.interno);
            textViewsiniiga = itemView.findViewById(R.id.siniiga);
            imageviewrec = itemView.findViewById(R.id.imgrec);
            tvmadre = itemView.findViewById(R.id.tvmadre);
            tvpadre = itemView.findViewById(R.id.tvpadre);

        


    

详情活动



   TextView textView;
   @Override
   protected void onCreate(@Nullable Bundle savedInstanceState) 
       super.onCreate(savedInstanceState);
       setContentView(R.layout.informationfire);

       TextView tvinterno = (TextView) findViewById(R.id.tvinterno);
       TextView tvsiniiga = (TextView) findViewById(R.id.tvsiniiga);


       String vinterno = "";
       String vsiniiga = "";



       Bundle extras = getIntent().getExtras();
       if (extras !=null);
       vinterno = extras.getString("keyint");
       vsiniiga = extras.getString("keysin");
       
       tvinterno.setText(vinterno);
       tvsiniiga.setText(vsiniiga);
       
   
   

火力基地

enter image description here

详情请参考xml

enter image description here

【问题讨论】:

【参考方案1】:

当您从 firebase 获取数据时,您将该数据保存在一个 Object 中,然后将该对象添加到列表中,因此您的列表包含 objects,因此您可以像执行其他操作一样通过意图。

    holder.itemView.setOnClickListener(new View.OnClickListener() 
                @Override
                public void onClick(View v) 
                    Intent intent=new Intent(v.getContext(),detailactivity.class);
                    intent.putExtra("keyint", vacaslist.getInterno());
                    intent.putExtra("keysin", vacaslist.getSiniiga());
                    intent.putExtra("madre", vacaslist.getMadre());
                    intent.putExtra("padre", vacaslist.getPadre());
                    v.getContext().startActivity(intent);
                
            );

【讨论】:

以上是关于单击 RecyclerView 后使用 Firebase 实时数据库填充详细信息活动的主要内容,如果未能解决你的问题,请参考以下文章

单击按钮后,Recyclerview适配器中的项目会跳转到右侧

过滤 RecyclerView 项目并单击它后会给出错误的项目

单击 RecyclerView 时显示附加信息

无法在 Recyclerview 中显示图像 - 文本已成功显示

在Android recyclerVIew中单击项目时如何保存状态

从片段中获取意图值后,我如何在 recyclerview 项目中实现单击