不使用 Firebase 将数据导入我的 Recycler View
Not Getting Data into my Recycler View using Firebase
我的 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 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);
}
}
}
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"/>
我没有将数据导入我的 RecyclerView。它只是没有显示任何东西,没有错误。非常感谢任何帮助。
我正在从 firebase 获取我的数据并将它们放入 recyclerview 以显示产品列表。
如果您能指出我的代码有什么问题以便我可以更正,我将非常高兴。
您需要添加 startListening()
才能开始侦听 firebaseui 中的数据:
The FirebaseRecyclerAdapter uses an event listener to monitor changes to the Firebase query. To begin listening for data, call the startListening() method. You may want to call this in your onStart() method. Make sure you have finished any authentication necessary to read the data before calling startListening() or your query will fail.
@Override
protected void onStart() {
super.onStart();
firebaseRecyclerAdapter.startListening();
}
我的 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 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);
}
}
}
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"/>
我没有将数据导入我的 RecyclerView。它只是没有显示任何东西,没有错误。非常感谢任何帮助。 我正在从 firebase 获取我的数据并将它们放入 recyclerview 以显示产品列表。
如果您能指出我的代码有什么问题以便我可以更正,我将非常高兴。
您需要添加 startListening()
才能开始侦听 firebaseui 中的数据:
The FirebaseRecyclerAdapter uses an event listener to monitor changes to the Firebase query. To begin listening for data, call the startListening() method. You may want to call this in your onStart() method. Make sure you have finished any authentication necessary to read the data before calling startListening() or your query will fail.
@Override
protected void onStart() {
super.onStart();
firebaseRecyclerAdapter.startListening();
}