gpt4 book ai didi

java - FirebaseRecyclerAdapter 无法工作或显示任何数据

转载 作者:行者123 更新时间:2023-12-02 08:59:54 27 4
gpt4 key购买 nike

我是 Android 编程新手。我已经能够在 firebase 中创建用户配置文件表,并且可以在 recyclerview 中显示用户配置文件表中的用户数据,但我无法让名为“Arbrg”的产品表在 firebaserecyclerview 中显示。请告诉我代码中哪里做错了。非常感谢您的帮助。

我的代码如下:

  1. viewARBRG.java(在按下按钮时调用)
package com.example.fb_app2;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class viewARBRG extends AppCompatActivity {
LinearLayoutManager mLinearLayoutManager;
RecyclerView mRecyclerView;
FirebaseDatabase mFirebaseDatabase;
DatabaseReference mDatabaseReference;
FirebaseRecyclerAdapter<MasterBarangClass, ViewHolderARBRG> firebaseRecyclerAdapter;
FirebaseRecyclerOptions<MasterBarangClass> options;

@Override
protected void onStart() {
super.onStart();
firebaseRecyclerAdapter.startListening();

}

@Override
protected void onStop() {
super.onStop();
firebaseRecyclerAdapter.stopListening();
}

@Override
public void onBackPressed() {
}

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

mLinearLayoutManager = new LinearLayoutManager(this);
mLinearLayoutManager.setReverseLayout(true);
mLinearLayoutManager.setStackFromEnd(true);

mRecyclerView = findViewById(R.id.recyclerView_arbrg);
mFirebaseDatabase = FirebaseDatabase.getInstance();

showData();
}

private void showData() {
mDatabaseReference = mFirebaseDatabase.getReference("Arbrg");

options = new FirebaseRecyclerOptions.Builder<MasterBarangClass>()
.setQuery(mDatabaseReference, MasterBarangClass.class).build();

Toast.makeText(getApplicationContext(), "Table : " + mDatabaseReference.toString(), Toast.LENGTH_SHORT).show();

firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<MasterBarangClass, ViewHolderARBRG>(options){
@Override
protected void onBindViewHolder(@NonNull ViewHolderARBRG viewHolderARBRG, int i, @NonNull MasterBarangClass masterBarangClass) {
final String barcode = masterBarangClass.getBarcode();
final String divbrg = masterBarangClass.getDivbrg();
final String hrgjual = masterBarangClass.getHrgjual();
final String jnsbrg = masterBarangClass.getJnsbrg();
final String kdbrg = masterBarangClass.getKdbrg();
final String nmbrg = masterBarangClass.getNmbrg();

viewHolderARBRG.setDetails(viewARBRG.this,
masterBarangClass.getBarcode(),
masterBarangClass.getDivbrg(),
masterBarangClass.getHrgjual(),
masterBarangClass.getJnsbrg(),
masterBarangClass.getKdbrg(),
masterBarangClass.getNmbrg());

viewHolderARBRG.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), showuser2klik.class);
intent.putExtra("barcode", barcode);
intent.putExtra("divbrg", divbrg);
intent.putExtra("hrgjual", hrgjual);
intent.putExtra("jnsbrg", jnsbrg);
intent.putExtra("kdbrg", kdbrg);
intent.putExtra("nmbrg", nmbrg);
startActivity(intent);
}
});
}
@NonNull
@Override
public ViewHolderARBRG onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
final View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_arbrg, parent, false);

final ViewHolderARBRG viewHolder = new ViewHolderARBRG(itemView);

viewHolder.setOnClickListener(new ViewHolderARBRG.ClickListener() {
@Override
public void onItemClick(View view, int position) {
Toast.makeText(getApplicationContext(), "clicked on " +position, Toast.LENGTH_SHORT).show();
}

@Override
public void onItemLongClick(View view, int position) {
Toast.makeText(getApplicationContext(), "onItem Long click...", Toast.LENGTH_SHORT).show();
}
});
return viewHolder;
}
};
mRecyclerView.setLayoutManager(mLinearLayoutManager);
firebaseRecyclerAdapter.startListening();
mRecyclerView.setAdapter(firebaseRecyclerAdapter);
}

public void back2main(View view) {
finish();
Intent int1 = new Intent(viewARBRG.this, MenuMasterActivity.class);
startActivity(int1);
}
}
  • activity_view_arbrg.xml
  • <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".viewARBRG">

    <TextView
    android:id="@+id/textView3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="RecyclerView - MASTER BARANG VIEW" />
    <TextView
    android:id="@+id/textViewUserList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="20dp"
    android:text="LIST BARANG (ALL)" />
    <Button
    android:id="@+id/buttonBack"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="back2main"
    android:text="BACK" />

    <androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView_arbrg"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
    </LinearLayout>

  • MasterBarangClass.java(模型类)
  • package com.example.fb_app2;

    public class MasterBarangClass {
    public String barcode, divbrg, hrgjual, jnsbrg, kdbrg, nmbrg;

    public MasterBarangClass(){}

    public MasterBarangClass(String barcode, String divbrg, String hrgjual, String jnsbrg, String kdbrg, String nmbrg) {
    this.barcode = barcode;
    this.divbrg = divbrg;
    this.hrgjual = hrgjual;
    this.jnsbrg = jnsbrg;
    this.kdbrg = kdbrg;
    this.nmbrg = nmbrg;
    }

    public String getBarcode() {
    return barcode;
    }
    public void setBarcode(String barcode) {
    this.barcode = barcode;
    }

    public String getDivbrg() {
    return divbrg;
    }
    public void setDivbrg(String divbrg) {
    this.divbrg = divbrg;
    }

    public String getHrgjual() {
    return hrgjual;
    }
    public void setHrgjual(String hrgjual) {
    this.hrgjual = hrgjual;
    }

    public String getJnsbrg() {
    return jnsbrg;
    }
    public void setJnsbrg(String jnsbrg) {
    this.jnsbrg = jnsbrg;
    }

    public String getKdbrg() {
    return kdbrg;
    }
    public void setKdbrg(String kdbrg) {
    this.kdbrg = kdbrg;
    }

    public String getNmbrg() {
    return nmbrg;
    }
    public void setNmbrg(String nmbrg) {
    this.nmbrg = nmbrg;
    }
    }

  • row_arbrg.XML(布局文件)
  • <?xml version="1.0" encoding="utf-8"?>
    <androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    app:cardBackgroundColor="#fff"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:cardCornerRadius="3dp"
    app:cardElevation="3dp"
    app:cardUseCompatPadding="true"
    app:contentPadding="5dp"
    android:layout_margin="10dp">

    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="5dp" >

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/ra_tvNmbrg"
    android:text="Nama Barang"
    android:textSize="18sp"
    android:textColor="#000"
    android:textStyle="bold"
    android:layout_marginLeft="10dp"
    android:layout_marginStart="10dp"/>

    <ImageView
    android:layout_width="110dp"
    android:layout_height="130dp"
    android:id="@+id/imageIv"
    android:src="@mipmap/ic_launcher"
    android:layout_below="@id/ra_tvNmbrg"/>

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/ra_tvKdbrg"
    android:text="Kode Barang"
    android:textSize="18sp"
    android:textColor="#000"
    android:layout_toRightOf="@id/imageIv"
    android:layout_toEndOf="@id/imageIv"
    android:layout_marginStart="10dp"
    android:layout_marginTop="20dp"/>

    <TextView
    android:id="@+id/ra_tvBarcode"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/ra_tvKdbrg"
    android:layout_marginStart="10dp"
    android:layout_marginTop="0dp"
    android:layout_toEndOf="@id/imageIv"
    android:layout_toRightOf="@id/imageIv"
    android:text="Bar code"
    android:textColor="#000"
    android:textSize="18sp" />
    <TextView
    android:id="@+id/ra_tvHargaJual"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/ra_tvBarcode"
    android:layout_marginStart="10dp"
    android:layout_marginTop="0dp"
    android:layout_toEndOf="@id/imageIv"
    android:layout_toRightOf="@id/imageIv"
    android:text="Harga Jual"
    android:textColor="#000"
    android:textSize="18sp" />
    <TextView
    android:id="@+id/ra_tvDivBrg"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/ra_tvHargaJual"
    android:layout_marginStart="10dp"
    android:layout_marginTop="0dp"
    android:layout_toEndOf="@id/imageIv"
    android:layout_toRightOf="@id/imageIv"
    android:text="Divisi barang"
    android:textColor="#000"
    android:textSize="18sp" />
    <TextView
    android:id="@+id/ra_tvJnsBrg"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/ra_tvDivBrg"
    android:layout_marginStart="10dp"
    android:layout_marginTop="0dp"
    android:layout_toEndOf="@id/imageIv"
    android:layout_toRightOf="@id/imageIv"
    android:text="Jenis barang"
    android:textColor="#000"
    android:textSize="18sp" />
    </RelativeLayout>

    </androidx.cardview.widget.CardView>

  • ViewHolderARBRG.java
  • package com.example.fb_app2;

    import android.content.Context;
    import android.view.View;
    import android.widget.TextView;

    import androidx.annotation.NonNull;
    import androidx.recyclerview.widget.RecyclerView;

    public class ViewHolderARBRG extends RecyclerView.ViewHolder {
    View mView;

    public ViewHolderARBRG(@NonNull View itemView) {
    super(itemView);
    mView = itemView;

    itemView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
    mClickListener.onItemClick(view, getAdapterPosition()); }
    });

    itemView.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View view) {
    mClickListener.onItemLongClick(view, getAdapterPosition());
    return true; }
    });
    }

    public void setDetails(Context ctx, String barcode, String divbrg, String hrgjual, String jnsbrg, String kdbrg, String nmbrg) {
    TextView mbarcode = mView.findViewById(R.id.ra_tvBarcode);
    TextView mdivbrg = mView.findViewById(R.id.ra_tvDivBrg);
    TextView mhrgjual = mView.findViewById(R.id.ra_tvHargaJual);
    TextView mjnsbrg = mView.findViewById(R.id.ra_tvJnsBrg);
    TextView mkdbrg = mView.findViewById(R.id.ra_tvKdbrg);
    TextView mnmbrg = mView.findViewById(R.id.ra_tvNmbrg);

    mbarcode.setText(barcode);
    mdivbrg.setText(divbrg);
    mhrgjual.setText(hrgjual);
    mjnsbrg.setText(jnsbrg);
    mkdbrg.setText(kdbrg);
    mnmbrg.setText(nmbrg);
    }

    private ViewHolderARBRG.ClickListener mClickListener;
    public interface ClickListener {
    void onItemClick(View view, int position);
    void onItemLongClick(View view, int position);
    }

    public void setOnClickListener(ViewHolderARBRG.ClickListener clickListener){
    mClickListener = clickListener;
    }
    }

    数据库结构如下:

    FirebaseDatabase Structure for Arbrg

    {
    "ArBrg" : {
    "-M0CKcYgvjxeYF6sGCma" : {
    "barcode" : "001",
    "divbrg" : "Alba satu",
    "hrgjual" : "899787",
    "jnsbrg" : "ALBA",
    "kdbrg" : "AAM",
    "nmbrg" : "500000"
    }
    },
    "Arsale" : {
    "-M02z2kgntKpso966nVB" : {
    "kdsale" : "SSR",
    "nmsale" : "Showroom salesperson"
    },
    "-M02z5me5mwau1jSMtlV" : {
    "kdsale" : "ANDI",
    "nmsale" : "ANDI"
    },
    "-M02z899rEkNHbjfANi7" : {
    "kdsale" : "BUDI",
    "nmsale" : "BUDI"
    }
    },
    "DivBrg" : {
    "-M02sY8LQIykdvaQph3a" : {
    "divbrg" : "LORUS",
    "nmdivbrg" : "LORUS"
    },
    "-M02scwzlD5-iFz_7r0j" : {
    "divbrg" : "ALBA",
    "nmdivbrg" : "ALBA"
    },
    "-M02sgYbypIab-m4gTAx" : {
    "divbrg" : "SEIKO",
    "nmdivbrg" : "SEIKO"
    },
    "-M02skAS79c-xKK_xdag" : {
    "divbrg" : "ASTRON",
    "nmdivbrg" : "ASTRON"
    },
    "vSrawft4GiUgMO1Z7ZZTwaU50H42" : {
    "divbrg" : "JEMIS",
    "nmdivbrg" : "JEMIS"
    }
    },
    "JnsBrg" : {
    "-M02w-iyneYG_rjxFDj4" : {
    "divbrg" : "ALBA",
    "jnsbrg" : "AAL",
    "nmjnsbrg" : "Analog Ladies"
    },
    "-M02w3YsFzX6SWjz0z0m" : {
    "divbrg" : "ALBA",
    "jnsbrg" : "AAM",
    "nmjnsbrg" : "Analog Men"
    },
    "-M02wC9M0cjJKEKbQHJl" : {
    "divbrg" : "SEIKO",
    "jnsbrg" : "KTL",
    "nmjnsbrg" : "kinetic ladies Seiko"
    }
    },
    "UserProfil" : {
    "2GGGxrGCQkQqaK9LFIBSdJJQ22i2" : {
    "email" : "maestro.info@gmail.com",
    "name" : "budi handoyo",
    "phone" : "+6281888112576"
    },
    "9RHvuwMVbJRkxYZsx2j5f9Gqlwx1" : {
    "email" : "maestro280620@gmail.com",
    "memail" : "maestro280620@gmail.com",
    "mname" : "Api Email 2",
    "mphone" : "+6282288887529",
    "name" : "Api Email 2",
    "phone" : "+6282225397529"
    },
    "GTxmtkcY5LPBWuUK9su0JQ8Dtcv2" : {
    "email" : "celia.candr@yahoo.com",
    "name" : "celia ",
    "phone" : "+6281255082289"
    },
    "JBmBE4jd1cXARJJtzP7ZpMWi3TU2" : {
    "email" : "maestro.infosis@gmail.com",
    "memail" : "maestro.infosis@gmail.com",
    "mname" : "Andi Hidajat 2",
    "mphone" : "+6281888992276",
    "name" : "Andi Hidajat 2",
    "phone" : "+6281288972576"
    },
    "vSrawft4GiUgMO1Z7ZZTwaU50H42" : {
    "email" : "maestro_info@yahoo.com",
    "memail" : "maestro_info@yahoo.com",
    "mname" : "Andi Hidajat",
    "mphone" : "+62818902712",
    "name" : "Andi Hidajat",
    "phone" : "+628189888512"
    }
    }
    }

    构建 APK 时,它没有显示错误,但当我执行 viewARBRG.java 时,Arbrg firebase 表中的产品数据没有显示。任何帮助

    问候,安迪

    最佳答案

    您的数据库引用不正确。它区分大小写。所以,请指正。

    使用

    mDatabaseReference = mFirebaseDatabase.getReference("ArBrg");

    而不是

    mDatabaseReference = mFirebaseDatabase.getReference("Arbrg");

    关于java - FirebaseRecyclerAdapter 无法工作或显示任何数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60247397/

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