我已经创建了一个片段,我正试图从firebase中获取项目并将其显示在片段上的recyclerView中,但是它没有显示任何内容,该片段为空白。
该片段的代码如下:-
public class NavCart extends Fragment {
String pos, s, lastPos, product, Uid;
private RecyclerView recyclerView;
FirebaseDatabase firebaseDatabase;
DatabaseReference dRef;
DatabaseReference Rdatabase;
DatabaseReference databaseReference;
Button trail, remove, Buy;
String Cost, Shipping, sign;
FirebaseUser currentFirebaseUser;
Double amount, Amount;
double sum = 0;
FirebaseRecyclerAdapter<CartConstructor, ShowDataViewHolder> mFirebaseAdapter;
ArrayList<String>checkedItem = new ArrayList<String>();
ArrayList<String>Removecode = new ArrayList<String>();
ArrayList<Double>cost = new ArrayList<Double>();
RelativeLayout layout;
public NavCart() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_nav_cart, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
firebaseDatabase = FirebaseDatabase.getInstance();
currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser() ;
Uid = String.valueOf(currentFirebaseUser.getUid());
dRef = FirebaseDatabase.getInstance().getReference();
Rdatabase = FirebaseDatabase.getInstance().getReference();
databaseReference = FirebaseDatabase.getInstance().getReference();
trail = (Button)view.findViewById(R.id.fCartButtonRemove);
trail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
builder.setTitle("Product Trail");
builder.setMessage("Once you request a trail of product from Strease, you are subject to pay a trail fee of INR " +
"140, if all the products are returned back.")
.setCancelable(false)
.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if (checkedItem.size() < 6) {
String pKey = String.valueOf(checkedItem);
Intent intent = new Intent(getApplicationContext(), addressInfo.class);
intent.putExtra("ProductArray", pKey);
startActivity(intent);
getActivity().finish();
} else {
layout = (RelativeLayout) layout.findViewById(R.id.rel);
Snackbar snackbar = Snackbar.make(layout,"A Maximum of 5 products can" +
" be selected for Trials!",Snackbar.LENGTH_LONG);
View sbView = snackbar.getView();
TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(Color.WHITE);
snackbar.show();
}
}
})
.setNegativeButton("Not now", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
remove = (Button)view.findViewById(R.id.fCartButtonTrail);
remove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
builder.setMessage("Remove product(s)?")
.setCancelable(false)
.setPositiveButton("Remove", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
int i;
for (i=0; i<checkedItem.size(); i++) {
String checkeditem = String.valueOf(Removecode.get(i));
Rdatabase.child("Cart").child(Uid).child(checkeditem).removeValue();
}
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
Buy = (Button)view.findViewById(R.id.fCartButtonBuy);
Buy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String Request = "Order";
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = prefs.edit();
editor.putString("Request", Request);
editor.commit();
Double netAmount = 0d;
for (int i = 0; i<cost.size(); i++){
netAmount += cost.get(i);
}
Cost = String.valueOf(netAmount);
if (netAmount < 500d) {
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
builder.setTitle("Total Order Value");
builder.setMessage("Your total order for " + String.valueOf(checkedItem.size()) + " products is: \n\n" +
"Total Amount: \t\t\t\t\t\t\t\t\t\t" + sign + Cost + "\nShipping cost: \t\t\t\t\t\t\t\t\t " + sign + "50.00")
.setCancelable(false)
.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String pKey = String.valueOf(checkedItem);
String pCost = String.valueOf(Cost);
Intent intent = new Intent(getApplicationContext(), addressInfo.class);
intent.putExtra("ProductArray", pKey);
intent.putExtra("Pcost",pCost);
startActivity(intent);
getActivity().finish();
}
})
.setNegativeButton("Not now", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
builder.setTitle("Total Order Value");
builder.setMessage("Your total order for " + String.valueOf(checkedItem.size()) + " products is: \n\n" +
"Total Amount: \t\t\t\t\t\t\t\t\t\t" + sign + Cost + "\nShipping Cost: \t\t\t\t\t\t\t\t\t " + sign + "00.00")
.setCancelable(false)
.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String pKey = String.valueOf(checkedItem);
Intent intent = new Intent(getApplicationContext(), addressInfo.class);
intent.putExtra("ProductArray", pKey);
startActivity(intent);
getActivity().finish();
}
})
.setNegativeButton("Not now", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
});
recyclerView = (RecyclerView) view.findViewById(R.id.fCartRecyclerView);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
Query query = firebaseDatabase.getReference().child("Cart").child(Uid).orderByKey();
FirebaseRecyclerOptions options = new FirebaseRecyclerOptions.Builder<CartConstructor>().setQuery(query,CartConstructor.class).build();
mFirebaseAdapter = new FirebaseRecyclerAdapter<CartConstructor, ShowDataViewHolder>(options) {
@Override
public ShowDataViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_view,parent,false);
return new ShowDataViewHolder(view);
}
@Override
protected void onBindViewHolder(@NonNull final ShowDataViewHolder holder, final int position, @NonNull CartConstructor model) {
holder.Heading(model.getHead());
holder.rate(model.getRating());
holder.Price(model.getPrice());
holder.PPrice(model.getPprice());
holder.Size(model.getSize());
holder.Quantity(model.getQuantity());
holder.setImg(getApplicationContext(),model.getImageUrl());
holder.Pos(model.getPosition());
holder.LastPosition(model.getLastPos());
holder.Product(model.getProduct());
holder.Code(model.getCode());
holder.Pcode(model.getProductCode());
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@SuppressLint("NewApi")
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
trail.setVisibility(View.VISIBLE);
remove.setVisibility(View.VISIBLE);
Buy.setVisibility(View.VISIBLE);
holder.itemView.setBackgroundColor(Color.LTGRAY);
checkedItem.add(holder.ProductCode.getText().toString());
Removecode.add(holder.code.getText().toString());
String Price = holder.pric.getText().toString();
String gAm[] = Price.split(" ");
sign = String.valueOf(gAm[0]);
amount = Double.parseDouble(String.valueOf(gAm[1]));
cost.add(amount);
} else {
holder.itemView.setBackgroundColor(Color.TRANSPARENT);
checkedItem.remove(holder.ProductCode.getText().toString());
Removecode.remove(holder.code.getText().toString());
String Price = holder.pric.getText().toString();
String gAm[] = Price.split(" ");
sign = String.valueOf(gAm[0]);
amount = Double.parseDouble(String.valueOf(gAm[1]));
cost.remove(amount);
if (checkedItem.isEmpty()){
trail.setVisibility(View.INVISIBLE);
remove.setVisibility(View.INVISIBLE);
Buy.setVisibility(View.INVISIBLE);
sum = 0;
}
}
}
});
}
};
recyclerView.setAdapter(mFirebaseAdapter);
}
public class ShowDataViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView head, pric, rate, Pprice, size, quantity, posit, LastPos, Prod, code, ProductCode;
ImageView img;
CheckBox checkBox;
public ShowDataViewHolder(final View itemView) {
super(itemView);
itemView.setOnClickListener(this);
checkBox = (CheckBox)itemView.findViewById(R.id.checkbox);
}
public void Pcode(String productcode){
ProductCode = (TextView)itemView.findViewById(R.id.ProductCode);
ProductCode.setText(productcode);
}
public void Pos(String position){
posit = (TextView)itemView.findViewById(R.id.CartPos);
posit.setText(position);
}
public void LastPosition(String Lpos){
LastPos = (TextView)itemView.findViewById(R.id.CartLastPos);
LastPos.setText(Lpos);
}
public void Code(String CodeNumber){
code = (TextView)itemView.findViewById(R.id.CartCode);
code.setText(CodeNumber);
// Removecode = String.valueOf(code);
}
public void Product(String product){
Prod = (TextView)itemView.findViewById(R.id.CartProduct);
Prod.setText(product);
}
public void Heading(String heading){
head=(TextView)itemView.findViewById(R.id.CartHead);
head.setText(heading);
}
public void Price(String price){
pric=(TextView)itemView.findViewById(R.id.CartPrice);
pric.setText(price);
}
public void rate(String Rate){
rate = (TextView)itemView.findViewById(R.id.CartRating);
rate.setText(Rate);
}
public void PPrice(String pprice){
Pprice = (TextView)itemView.findViewById(R.id.CartPprice);
Pprice.setText(pprice);
}
public void Size(String Siz){
size = (TextView)itemView.findViewById(R.id.CartSize1);
size.setText(Siz);
}
public void Quantity(String quant){
quantity = (TextView)itemView.findViewById(R.id.CartQuantity1);
quantity.setText(quant);
}
public void setImg(Context ctx, String img1){
img=(ImageView)itemView.findViewById(R.id.CartImageView);
Picasso.get().load(img1).placeholder(R.drawable.notification).into(img);
}
@Override
public void onClick(View v) {
pos = String.valueOf(getPosition());
String Position = posit.getText().toString();
String LasPos = LastPos.getText().toString();
String prodct = Prod.getText().toString();
Intent intent = new Intent(getApplicationContext(), Checkout_page.class);
intent.putExtra("position", Position);
intent.putExtra("product", prodct);
intent.putExtra("last_pos", LasPos);
startActivity(intent);
getActivity().finish();
}
}
@Override
public void onStart() {
super.onStart();
mFirebaseAdapter.startListening();
recyclerView.setAdapter(mFirebaseAdapter);
}
@Override
public void onStop() {
super.onStop();
mFirebaseAdapter.stopListening();
}
}
这是片段类的完整代码,在活动中编写时,代码绝对可以正常工作,但是由于我对片段的经验较少,因此我对片段的确切工作方式和错误之处了解得较少在我的代码中。
任何帮助将不胜感激。