1

Actually, I am performing the CRUD Operations in the android application. The create is working fine but I am getting the problem in reading the data from the firebase. Basically, I have created my schema in a different class.

Here is my RecyclerAdapter

public class RecyclerAdapter extends 
RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder> { //I am getting error here in this line i.e ERROR(RecyclerAdapter.java:22)

private Context mContext;
private List<StorageSchema> storageSchemas;
private OnItemClickListener mListener;

public RecyclerAdapter(Context context,List<StorageSchema> uploads){
    mContext = context;
    storageSchemas = uploads;
}

@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int 
viewType) {
View v = 
LayoutInflater.from(mContext).inflate(R.layout.row_model,parent,false);
    return new RecyclerViewHolder(v);
}

@Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
    StorageSchema currentItem = storageSchemas.get(position);
    holder.productnameTextView.setText(currentItem.getProductname());
    holder.productdescriptionTextView.setText(
                    currentItem.getProductdescription());
    Picasso.get()
            .load(currentItem.getImageUrl())
            .placeholder(R.drawable.placeholder)
            .fit()
            .centerCrop()
            .into(holder.productImageView);
    holder.rateTextView.setText(currentItem.getRate()); //I am getting error here in this line i.e ERROR(RecyclerAdapter.java:51)
    holder.unitTextView.setText(currentItem.getUnit());
}

public class RecyclerViewHolder extends RecyclerView.ViewHolder 
implements View.OnClickListener,
    View.OnCreateContextMenuListener, MenuItem.OnMenuItemClickListener {

    public TextView productnameTextView, productdescriptionTextView, 
rateTextView, unitTextView;
    public ImageView productImageView;

    public RecyclerViewHolder(View itemView){
        super(itemView);

        productnameTextView = 
itemView.findViewById(R.id.product_name_textview);
        productdescriptionTextView = 
itemView.findViewById(R.id.product_description_textview);
        rateTextView = itemView.findViewById(R.id.rate_textview);
        unitTextView = itemView.findViewById(R.id.unit_textview);
        productImageView = itemView.findViewById(R.id.imageView2);

        itemView.setOnClickListener(this);
        itemView.setOnCreateContextMenuListener(this);
    }
}

Here is my actual activity where I want to show the data

public class AdminHome extends AppCompatActivity implements 
RecyclerAdapter.OnItemClickListener {

private RecyclerView frecyclerView;
private RecyclerAdapter frecyclerAdapter;
private ProgressBar fprogressBar;
private FirebaseStorage fstorage;
private DatabaseReference fDatabaseRef;
private ValueEventListener fDBListener;
private List<StorageSchema> fProduct;
private FirebaseAuth firebaseAuth;

FloatingActionButton additem;

private void openDetailsActivity(String[] data){
    Intent i=new Intent(this, DetailsActivity.class);
    i.putExtra("PRODUCTNAME_KEY",data[0]);
    i.putExtra("PRODUCTDESCRIPTION_KEY",data[1]);
    i.putExtra("IMAGE_KEY",data[2]);
    i.putExtra("PRODUCTRATE_KEY",data[3]);
    i.putExtra("UNIT_KEY",data[4]);
    startActivity(i);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_admin_home);
    additem = findViewById(R.id.create_list_item);

    //listening the floatingbutton action
    additem.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(AdminHome.this,Add_Item.class));
        }
    });


    firebaseAuth=FirebaseAuth.getInstance();
    FirebaseUser user = firebaseAuth.getCurrentUser();


    if(firebaseAuth.getCurrentUser()==null){
        startActivity(new Intent(this,LoginActivity.class));
    }

//starting reading data from firebase database and showing it into a list
    frecyclerView = findViewById(R.id.frecyclerview);
    frecyclerView.setHasFixedSize(true);
    frecyclerView.setLayoutManager(new LinearLayoutManager(this));

    fprogressBar = findViewById(R.id.fprogressBar);
    fprogressBar.setVisibility(View.VISIBLE);

    fProduct = new ArrayList<>();
    frecyclerAdapter = new RecyclerAdapter(AdminHome.this, fProduct);
    frecyclerView.setAdapter(frecyclerAdapter);
    frecyclerAdapter.setOnItemClickListener(AdminHome.this);

    fstorage = FirebaseStorage.getInstance();
    fDatabaseRef = 
FirebaseDatabase.getInstance().getReference("Products");
    fDatabaseRef.keepSynced(true);

    fDBListener = fDatabaseRef.addValueEventListener(new 
 ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
         fProduct.clear();
         for (DataSnapshot productSnapshot : dataSnapshot.getChildren()){
         StorageSchema upload = 
productSnapshot.getValue(StorageSchema.class);
                upload.setKey(productSnapshot.getKey());
                fProduct.add(upload);
            }
            frecyclerAdapter.notifyDataSetChanged();
            fprogressBar.setVisibility(View.GONE);
        }
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            Toast.makeText(AdminHome.this,databaseError.getMessage(), 
 Toast.LENGTH_SHORT).show();
            fprogressBar.setVisibility(View.INVISIBLE);
        }
    });
  }
}

Here is My Schema

public class StorageSchema {

private String productname;
private String productdescription;
private String imageUrl;
private int rate;
private String unit;
private int position;
private String key;


public StorageSchema(){
    //empty constructor needed
}

public StorageSchema(int position){
    this.position=position;
}

public StorageSchema(String productname, String productdescription, 
String imageUrl, int rate, String unit){
if(productname.trim().equals("")){
    productname = "No Name";
}
this.productname = productname;
this.productdescription = productdescription;
this.imageUrl = imageUrl;
this.rate = rate;
this.unit = unit;
}
//and here are the getter and setter which are autogenerated.

I have written the code by taking help from the internet but my recycler view named as "RecyclerAdapter" method onBindViewHolder is generating an error as FATAL EXCEPTION: main Process: com.example.freshveg, PID: 6823 android.content.res.Resources$NotFoundException: String resource ID #0x1e

at Adapter.RecyclerAdapter.onBindViewHolder(RecyclerAdapter.java:51)

at Adapter.RecyclerAdapter.onBindViewHolder(RecyclerAdapter.java:22)

1 Answers1

0

The problem is you are trying to set an Integer(currentItem.getRate()) value for the textview. Textview.setText() only support Charsequence.

To solve this use String.valueOf() to convert the int value to a string as given below.

holder.rateTextView.setText(String.valueOf(currentItem.getRate()));
sanoJ
  • 2,935
  • 2
  • 8
  • 18
  • Happy to help. Great if you can mark this as the answer – sanoJ Jul 21 '19 at 12:17
  • One last thing I want to ask It will not take much time. That I have created a Floating Action Button but when the list inflates in my recycler view the FAB gets behind the list what property I need to set to bring it in front? – StayAtOrbit Jul 21 '19 at 12:31
  • By default, the FAB has an [elevation](https://material.io/design/environment/elevation.html#elevation-in-material-design) of 6dp to have it over other views. Try to add more `elevation`. check [this](https://stackoverflow.com/a/30740379/9263083) – sanoJ Jul 21 '19 at 12:55