Java 未使用Firebase将数据导入我的回收器视图
我的RecyclerView没有显示任何数据 buynow.javaJava 未使用Firebase将数据导入我的回收器视图,java,android,firebase,firebase-realtime-database,firebaseui,Java,Android,Firebase,Firebase Realtime Database,Firebaseui,我的RecyclerView没有显示任何数据 buynow.java public class buynow extends AppCompatActivity { private RecyclerView recyclerView; DatabaseReference ProductRef; private EditText searchField; private Button search_btn; @Override protected void onCreate(Bundle sav
public class buynow extends AppCompatActivity {
private RecyclerView recyclerView;
DatabaseReference ProductRef;
private EditText searchField;
private Button search_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buynow);
ProductRef = FirebaseDatabase.getInstance().getReference().child("Products");
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
searchField = (EditText) findViewById(R.id.search_field);
search_btn = (Button) findViewById(R.id.search_button);
search_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
firebaseUserSearch();
}
});
}
private void firebaseUserSearch(){
FirebaseRecyclerOptions<Products> options = new FirebaseRecyclerOptions.Builder<Products>()
.setQuery(ProductRef,Products.class).build();
FirebaseRecyclerAdapter<Products, UsersViewHolder> firebaseRecyclerAdapter = new
FirebaseRecyclerAdapter<Products, UsersViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull UsersViewHolder holder, int position, @NonNull
Products model) {
holder.setDetails(model.getPname(), model.getPprice(), model.getPmrp(),
model.getPcondition(), model.getPimage());
}
@NonNull
@Override
public UsersViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return null;
}
};
recyclerView.setAdapter(firebaseRecyclerAdapter);
}
//View Holder Class
public class UsersViewHolder extends RecyclerView.ViewHolder{
View mView;
public UsersViewHolder(@NonNull View itemView) {
super(itemView);
mView = itemView;
}
public void setDetails(String phoneName, String phonePrice, String phoneMrp, String
phoneCondition, String phoneImage){
TextView phone_name = (TextView) mView.findViewById(R.id.product_name);
TextView phone_price = (TextView) mView.findViewById(R.id.product_price);
TextView phone_mrp = (TextView) mView.findViewById(R.id.product_mrp);
TextView phone_condition = (TextView) mView.findViewById(R.id.product_condition);
ImageView phone_image = (ImageView)mView.findViewById(R.id.product_image);
phone_name.setText(phoneName);
phone_price.setText(phonePrice);
phone_mrp.setText(phoneMrp);
phone_condition.setText(phoneCondition);
Picasso.with(getApplicationContext()).load(phoneImage).into(phone_image);
}
}
}
公共类buynow扩展了AppCompative活动{
私人回收站;
数据库参考ProductRef;
私人编辑文本搜索字段;
私人按钮搜索;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buynow);
ProductRef=FirebaseDatabase.getInstance().getReference().child(“产品”);
recyclerView=(recyclerView)findViewById(R.id.recycler\u视图);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(新的LinearLayoutManager(本));
searchField=(EditText)findViewById(R.id.search\u字段);
search_btn=(按钮)findviewbyd(R.id.search_按钮);
search_btn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
firebaseUserSearch();
}
});
}
私有void firebaseUserSearch(){
FirebaseRecyclerOptions选项=新建FirebaseRecyclerOptions.Builder()
.setQuery(ProductRef,Products.class).build();
FirebaseRecyclerAdapter FirebaseRecyclerAdapter=新
FirebaseRecyclerAdapter(选件){
@凌驾
受保护的无效onBindViewHolder(@NonNull UsersViewHolder holder,int位置,@NonNull
产品(型号){
holder.setDetails(model.getPname()、model.getPprice()、model.getPmrp()、model,
model.getPcondition(),model.getPimage();
}
@非空
@凌驾
public UsersViewHolder onCreateViewHolder(@NonNull ViewGroup父级,int viewType){
返回null;
}
};
recyclerView.setAdapter(firebaseRecyclerAdapter);
}
//视图持有者类
公共类UsersViewHolder扩展了RecyclerView.ViewHolder{
视图视图;
public UsersViewHolder(@NonNull View itemView){
超级(项目视图);
mView=项目视图;
}
public void setDetails(字符串phoneName、字符串phonePrice、字符串phoneMrp、字符串
phoneCondition,字符串phoneImage){
TextView phone_name=(TextView)mView.findViewById(R.id.product_name);
TextView手机价格=(TextView)mView.findViewById(R.id.product\u价格);
TextView-phone\u-mrp=(TextView)mView.findviewbyd(R.id.product\u-mrp);
TextView电话\条件=(TextView)mView.findViewById(R.id.product\条件);
ImageView phone_image=(ImageView)mView.findViewById(R.id.product_image);
phone_name.setText(phoneName);
phone_price.setText(phonePrice);
phone_mrp.setText(phoneMrp);
phone_condition.setText(phoneCondition);
Picasso.with(getApplicationContext()).load(phoneImage).into(phone\u image);
}
}
}
Products.java
public class Products {
private String pname,pprice,pimage,pmrp,pcondition;
public Products(){
}
public Products(String pname, String pprice, String pimage, String pmrp, String pcondition) {
this.pname = pname;
this.pprice = pprice;
this.pimage = pimage;
this.pmrp = pmrp;
this.pcondition = pcondition;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public String getPprice() {
return pprice;
}
public void setPprice(String pprice) {
this.pprice = pprice;
}
public String getPimage() {
return pimage;
}
public void setPimage(String pimage) {
this.pimage = pimage;
}
public String getPmrp() {
return pmrp;
}
public void setPmrp(String pmrp) {
this.pmrp = pmrp;
}
public String getPcondition() {
return pcondition;
}
public void setPcondition(String pcondition) {
this.pcondition = pcondition;
}
}
Layout for RecyclerView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:background="@color/primanrybg">
<RelativeLayout
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/buyphones_layout_bg">
<ImageView
android:id="@+id/product_image"
android:layout_width="60dp"
android:layout_height="90dp"
android:src="@drawable/rapid_pickup_foreground" />
<TextView
android:id="@+id/product_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/product_image"
android:textSize="16sp"
android:textColor="@color/black"
android:paddingStart="16dp"
android:paddingBottom="4dp"
android:text="iPhone 6s Space Grey (64GB)"/>
<LinearLayout
android:id="@+id/condition"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/product_image"
android:layout_marginLeft="16dp"
android:background="@drawable/search_frame"
android:layout_below="@id/product_name"
android:layout_marginBottom="3dp"
android:padding="2dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/yes_no_bg"
android:textColor="@color/white"
android:paddingStart="4dp"
android:padding="1dp"
android:paddingEnd="4dp"
android:text="Condition:" />
<TextView
android:id="@+id/product_condition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_marginRight="3dp"
android:padding="1dp"
android:textColor="@color/black"
android:text="Like New" />
</LinearLayout>
<TextView
android:id="@+id/product_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/condition"
android:layout_toRightOf="@+id/product_image"
android:textSize="18sp"
android:textColor="@color/black"
android:paddingStart="16dp"
android:text="9,9999"/>
<TextView
android:id="@+id/product_mrp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/product_price"
android:layout_below="@id/condition"
android:textSize="18sp"
android:text="24000"
android:textColor="@color/grey"
android:paddingStart="10dp"/>
公共类产品{
私有字符串pname、pprice、pimage、pmrp、PCCONDITION;
公共产品(){
}
公共产品(字符串pname、字符串pprice、字符串pimage、字符串pmrp、字符串PCCONDITION){
this.pname=pname;
this.pprice=pprice;
this.pimage=pimage;
这是pmrp=pmrp;
this.pccondition=pccondition;
}
公共字符串getPname(){
返回pname;
}
公共void setPname(字符串pname){
this.pname=pname;
}
公共字符串getPprice(){
返回许可证;
}
公共无效设置权限(字符串权限){
this.pprice=pprice;
}
公共字符串getPimage(){
返回pimage;
}
公共void setPimage(字符串pimage){
this.pimage=pimage;
}
公共字符串getPmrp(){
返回pmrp;
}
公共void setPmrp(字符串pmrp){
这是pmrp=pmrp;
}
公共字符串getPcondition(){
返回条件;
}
public void setPcondition(字符串pcondition){
this.pccondition=pccondition;
}
}
RecyclerView的布局
我没有将数据输入我的RecyclerView。它只是没有显示任何内容,没有错误,没有任何内容。非常感谢您的帮助。
我从firebase获取数据,并将其放入recyclerview以显示产品列表
如果您能指出我的代码有什么问题,我会非常高兴,这样我就可以纠正它。您需要添加
startListening()
来开始侦听中的数据:
FirebaseRecyclerAdapter使用事件侦听器监视对Firebase查询的更改。要开始侦听数据,请调用startListening()方法。您可能希望在onStart()方法中调用此函数。在调用startListening()之前,请确保已完成读取数据所需的任何身份验证,否则查询将失败
请编辑您的问题并将您的数据库结构添加为屏幕截图。
@Override
protected void onStart() {
super.onStart();
firebaseRecyclerAdapter.startListening();
}