Вот так выглядит моя база данных
![enter image description here](https://i.stack.imgur.com/zdt4c.png)
Я хотел перебрать все дочерние узлы, как это, и получить значение от их соответствующего дочернего узла Но я не могу этого сделать, пожалуйста, все равно предложите это сделать. Код для фрагмента, для которого должен отображаться список, приведен ниже
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.List;
/**
* A simple {@link Fragment} subclass.
* Use the {@link HomeFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class HomeFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
RecyclerView recyclerView;
List<Product> productList;
public HomeFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment HomeFragment.
*/
// TODO: Rename and change types and number of parameters
public static HomeFragment newInstance(String param1, String param2) {
HomeFragment fragment = new HomeFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v= inflater.inflate(R.layout.fragment_home, container, false);
recyclerView = v.findViewById(R.id.recyclerView);
initData();
initRecyclerView();
return v;
}
private void initRecyclerView() {
ProductAdapter productAdapter = new ProductAdapter(productList);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(productAdapter);
}
private void initData() {
String user = FirebaseAuth.getInstance().getCurrentUser().getUid();
productList = new ArrayList<>();
FirebaseDatabase.getInstance().getReference().child(user)
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
String pname = snapshot.child("pname").getValue(String.class);
String pdesc = dataSnapshot.child("description").getValue(String.class);
String price = dataSnapshot.child("price").getValue(String.class);
String imgURL = dataSnapshot.child("image").getValue(String.class);
productList.add(new Product(pname,price,pdesc,imgURL));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
Код для класса адаптера продукта указан ниже
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductVH> {
private static final String TAG = "ProductAdapter";
List<Product> productList;
public ProductAdapter(List<Product> productList) {
this.productList = productList;
}
@NonNull
@Override
public ProductVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row, parent, false);
return new ProductVH(view);
}
@Override
public void onBindViewHolder(@NonNull ProductVH holder, int position) {
Product product = productList.get(position);
holder.prodName.setText(product.getProductName());
holder.prodPrice.setText(product.getProductPrice());
holder.prodDesc.setText(product.getProductDesc());
boolean isExpanded = productList.get(position).isExpanded();
holder.expandableLayout.setVisibility(isExpanded ? View.VISIBLE : View.GONE);
}
@Override
public int getItemCount() {
return productList.size();
}
public class ProductVH extends RecyclerView.ViewHolder {
private static final String TAG = "ProductVH";
ConstraintLayout expandableLayout;
TextView prodName , prodPrice , prodDesc ;
ImageView productImage , delete , instock , edit;
public ProductVH(@NonNull View itemView) {
super(itemView);
expandableLayout = itemView.findViewById(R.id.expandableLayout);
prodName = itemView.findViewById(R.id.product_list_name);
prodPrice = itemView.findViewById(R.id.product_list_price);
prodDesc = itemView.findViewById(R.id.product_description);
productImage = itemView.findViewById(R.id.product_image);
delete = itemView.findViewById(R.id.delete);
instock = itemView.findViewById(R.id.in_stock);
edit = itemView.findViewById(R.id.edit);
prodName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Product product = productList.get(getAdapterPosition());
product.setExpanded(!product.isExpanded());
notifyItemChanged(getAdapterPosition());
}
});
}
}
}
Класс продукта указан ниже
public class Product {
String productName,productDesc, productPrice,imgURI;
private boolean expanded;
public Product() {
}
public Product(String productName, String productDesc, String productPrice, String imgURI) {
this.productName = productName;
this.productDesc = productDesc;
this.productPrice = productPrice;
this.imgURI = imgURI;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getProductDesc() {
return productDesc;
}
public void setProductDesc(String productDesc) {
this.productDesc = productDesc;
}
public String getProductPrice() {
return productPrice;
}
public void setProductPrice(String productPrice) {
this.productPrice = productPrice;
}
public String getImgURI() {
return imgURI;
}
public void setImgURI(String imgURI) {
this.imgURI = imgURI;
}
public boolean isExpanded() {
return expanded;
}
public void setExpanded(boolean expanded) {
this.expanded = expanded;
}
@Override
public String toString() {
return "Movie{" +
"Product name ='" + productName + '\'' +
", price ='" + productPrice + '\'' +
", product descrition ='" + productDesc + '\'' +
", image ='" + imgURI + '\'' +
", expanded=" + expanded +
'}';
}
}