Unable to load more than one row using FirebaseRecyclerAdapter - android

I am trying to fetch the data from firebase, using firebaseRecyclerAdapter. But I am unable to populate all the data from the firebase database to the RecyclerView.
Database is as follows:
The output I am getting is only from the "qqq" row
rvlist Class:
public class rvlist {
String name, propellant;
public rvlist() {
}
public rvlist(String name, String propellant) {
this.name = name;
this.propellant = propellant;
}
public String getName() {
return name;
}
public String getPropellant() {
return propellant;
}
}
MyViewHolder:
public class MyViewHolder extends RecyclerView.ViewHolder {
View mView;
public MyViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setName(String name) {
TextView field = (TextView) mView.findViewById(R.id.a);
field.setText(name);
}
public void setPropellant(String text) {
TextView field = (TextView) mView.findViewById(R.id.b);
field.setText(text);
}
}
MainActivity:
public class MainActivity extends AppCompatActivity {
DatabaseReference mRef;
RecyclerView rv;
ProgressBar pb;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
pb = (ProgressBar)findViewById(R.id.pb);
pb.setVisibility(View.VISIBLE);
rv = (RecyclerView) findViewById(R.id.rvv);
rv.setLayoutManager(new LinearLayoutManager(this));
mRef = FirebaseDatabase.getInstance().getReference().child("table");
}
#Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<rvlist, MyViewHolder> recyclerAdapter = new FirebaseRecyclerAdapter<rvlist, MyViewHolder>
(rvlist.class, R.layout.model, MyViewHolder.class, mRef) {
#Override
public void populateViewHolder(MyViewHolder itemsViewHolder, rvlist items, int position) {
pb.setVisibility(View.GONE);
itemsViewHolder.setName(items.getName());
itemsViewHolder.setPropellant(items.getPropellant());
}
};
rv.setAdapter(recyclerAdapter);
}
}

Make sure your RecyclerView and adapter layout_height is set to wrap_content not to match_parent
<android.support.v7.widget.RecyclerView
android:id="#+id/recv_location"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layoutManager="LinearLayoutManager"
...>
</android.support.v7.widget.RecyclerView>

Related

Retrieving data from firebase to recyclerview does not work

As mentioned in the title, my recyclerview does not work. No idea why, because I do not get any errors or anything in the logcat or while building the app. Is there a way I could debug this or could you point out the mistake that I did?
I've implemented another recyclerview with the adapter, but I used a simple adapter. I tried using the firebaseUI in this case, and it seems that the adapter somehow cannot retrieve the data from the firebase firestore.
https://imgur.com/a/JSLXuou The screenshot of my database structure.
My adapter:
public class ManageAdapter extends FirestoreRecyclerAdapter<Manage, ManageAdapter.ManageHolder> {
public ManageAdapter(#NonNull FirestoreRecyclerOptions<Manage> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull ManageHolder holder, int position, #NonNull Manage model) {
holder.textviewName.setText(model.getName());
holder.textviewGrade.setText(model.getGrade());
//holder.textviewAge.setText(String.valueOf(model.getAge()));
}
#NonNull
#Override
public ManageHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.manage_item,
parent,false);
return new ManageHolder(v);
}
class ManageHolder extends RecyclerView.ViewHolder{
TextView textviewName;
TextView textviewAge;
TextView textviewGrade;
public ManageHolder(#NonNull View itemView) {
super(itemView);
textviewAge = itemView.findViewById(R.id.manage_item_title_age);
textviewGrade = itemView.findViewById(R.id.manage_item_grade);
textviewName = itemView.findViewById(R.id.manage_item_name);
}
}
}
Main class:
private FirebaseFirestore firebaseFirestore;
private Toolbar mToolbar;
private Button delete_event;
private RecyclerView participants;
private TextView manage_events_title;
private TextView participans_text;
private ManageAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manage_events);
final String ID = getIntent().getExtras().getString("ID");
firebaseFirestore = FirebaseFirestore.getInstance();
delete_event = (Button) findViewById(R.id.delete_event);
manage_events_title = (TextView) findViewById(R.id.manage_events_title);
participans_text = (TextView) findViewById(R.id.participants_text);
participants = (RecyclerView) findViewById(R.id.recyclerview_manage);
setUpRecyclerView();
}
private void setUpRecyclerView(){
final String ID = getIntent().getExtras().getString("ID");
Query query = firebaseFirestore.collection("Events").document(ID).collection("Users");
FirestoreRecyclerOptions<Manage> options = new FirestoreRecyclerOptions.Builder<Manage>()
.setQuery(query, Manage.class).build();
adapter = new ManageAdapter(options);
participants.setHasFixedSize(true);
participants.setLayoutManager(new LinearLayoutManager(this));
participants.setAdapter(adapter);
}
#Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
}

Error - com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String

I am getting error
"com.google.firebase.database.DatabaseException:
Can't convert object of type java.lang.String to type
krunoslavkovac.ferit.test.Add"
And can't figure it out why is it happening.
I've searched many solutions here but still didn't figure it out.
MAIN ACTIVITY :
public class MainActivity extends AppCompatActivity {
EditText et_task ;
Button btn_add;
RecyclerView recycler_view;
//Firebase
FirebaseDatabase database;
DatabaseReference dbRef ;
FirebaseRecyclerOptions<Add> options;
FirebaseRecyclerAdapter<Add, MyRecyclerViewHolder> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_task = (EditText) findViewById(R.id.et_task);
btn_add = (Button) findViewById(R.id.btn_add);
recycler_view = (RecyclerView) findViewById(R.id.recycler_view);
recycler_view.setLayoutManager(new LinearLayoutManager(this));
database = FirebaseDatabase.getInstance();
dbRef = database.getReference("Test");
btn_add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addTask();
}
});
displayTask();
}
private void addTask(){
String task = et_task.getText().toString();
Add add = new Add(task);
dbRef.push().setValue(task);
adapter.notifyDataSetChanged();
}
private void displayTask() {
options =
new FirebaseRecyclerOptions.Builder<Add>()
.setQuery(dbRef,Add.class)
.build();
adapter =
new FirebaseRecyclerAdapter<Add, MyRecyclerViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull MyRecyclerViewHolder holder, int position, #NonNull Add model) {
holder.tv_task.setText(model.getTask());
}
#NonNull
#Override
public MyRecyclerViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.from(getBaseContext()).inflate(R.layout.add_item,viewGroup, false);
return new MyRecyclerViewHolder(itemView);
}
};
adapter.startListening();
recycler_view.setAdapter(adapter);
}
}
Add class
public class Add {
private String task;
public Add(){
}
public Add(String task) {
this.task = task;
}
public String getTask() {
return task;
}
public void setTask(String task) {
this.task = task;
}
}
RecyclervViewHolder
public class MyRecyclerViewHolder extends RecyclerView.ViewHolder {
TextView tv_task;
public MyRecyclerViewHolder(#NonNull View itemView) {
super(itemView);
tv_task = (TextView) itemView.findViewById(R.id.tv_task);
}
}
Database:
I want to display data from firebase.
Replace this
dbRef.push().setValue(task);
To
dbRef.push().setValue(add);

How to retrieve data from firebase database in android

Retrieving images from Firebase does not work? Hi I am working on upload and retrieve images from Firebase. I have successfully done the uploading part, but while doing for the retrieving one. I have came across a problem where i am unable to fetch image from Firebase. I am using Picasso version: 2.5.2
QuoteList.java
public class QuoteList extends AppCompatActivity {
private StorageReference storageRef;
private RecyclerView mRecyclerView;
private DatabaseReference mReference;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quote_list);
final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView = findViewById(R.id.quotes_list);
mRecyclerView.setLayoutManager(layoutManager);
mReference = FirebaseDatabase.getInstance().getReference().child("Quotes");
}
#Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Quote, QuoteViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<Quote, QuoteViewHolder>(
Quote.class,
R.layout.quote_row,
QuoteViewHolder.class,
mReference
) {
#Override
protected void populateViewHolder(QuoteViewHolder viewHolder , Quote model , int position) {
viewHolder.setQuotes(model.getQuotes());
viewHolder.setQuoteAuthor(model.getQuote_Author());
viewHolder.setImage(getApplicationContext(),model.getImages());
}
};
mRecyclerView.setAdapter(firebaseRecyclerAdapter);
}
public class QuoteViewHolder extends RecyclerView.ViewHolder{
View mView;
public QuoteViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setQuotes(String Quote){
TextView post_quote = (TextView) mView.findViewById(R.id.post_quote);
post_quote.setText(Quote);
}
public void setQuoteAuthor(String QuoteAuthor){
TextView post_quoteAuthor = (TextView) mView.findViewById(R.id.post_author);
post_quoteAuthor.setText(QuoteAuthor);
}
public void setImage(Context ctx, String image){
ImageView post_image = (ImageView) mView.findViewById(R.id.post_image);
Picasso.with(ctx).load(image).into(post_image);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
}
For reference i have used a Youtube Link.

android recyclerview scroll making mess in chat using firebase

I'm simply implementing a RecyclerView for showing chat between two. Everything is working fine but there is one problem when I am scrolling up and again scrolling down the direction of sender message goes left(GRAVITY) which I have set only for the received ones. Please look the code below hope u will understand.
Screenshot before scrolling.
Screenshot after scrolling.
Here's my RecycleActivity.java
public class RecycleActivity extends AppCompatActivity {
RecyclerView recyclerView;
DatabaseReference databaseReference,temp;
EditText editText;
Button btnSend;
public static String nameOFUser = "";
LinearLayoutManager linearLayoutManager;
SharedPreferences sharedPreferences;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainer);
sharedPreferences = getSharedPreferences("MyLoginPref",MODE_PRIVATE);
nameOFUser = sharedPreferences.getString("name","");
Log.e("name",nameOFUser);
recyclerView = (RecyclerView) findViewById(R.id.recyclerid);
recyclerView.setHasFixedSize(true);
linearLayoutManager= new LinearLayoutManager(this);
linearLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(linearLayoutManager);
databaseReference = FirebaseDatabase.getInstance().getReference().child("msg");
editText = (EditText) findViewById(R.id.edittextmessagetosend);
btnSend = (Button) findViewById(R.id.button41);
btnSend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
temp=databaseReference.push();
temp.child("name").setValue(nameOFUser);
temp.child("message").setValue(editText.getText().toString().trim());
editText.setText("");
recyclerView.setLayoutManager(linearLayoutManager);
}
});
}
#Override
protected void onStart() {
super.onStart();
final FirebaseRecyclerAdapter<MessageBlog,MessageHolder> firebaseRecyclerAdapter
= new FirebaseRecyclerAdapter<MessageBlog, MessageHolder>(
MessageBlog.class,R.layout.message_row,MessageHolder.class,databaseReference
) {
#Override
protected void populateViewHolder(MessageHolder viewHolder, MessageBlog model, int position) {
String name = model.getName();
viewHolder.setSender(name);
viewHolder.SetMessage(model.getMessage());
viewHolder.setDirection(name);
}
};
recyclerView.setAdapter(firebaseRecyclerAdapter);
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
recyclerView.smoothScrollToPosition(firebaseRecyclerAdapter.getItemCount());
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
The ViewHolder.java
public static class MessageHolder extends RecyclerView.ViewHolder{
View view;
LinearLayout lay,linearLayoutback;
CardView cardViewreal;
DatabaseReference mdatabase;
public MessageHolder(View itemView) {
super(itemView);
view = itemView;
linearLayoutback = (LinearLayout)
itemView.findViewById(R.id.linearlayoutback);
mdatabase=FirebaseDatabase.getInstance().getReference().child("msg");
lay = (LinearLayout) itemView.findViewById(R.id.idcardview);
cardViewreal = (CardView) itemView.findViewById(R.id.idrealcard);
}
void setDirection(final String name){
if(!name.equalsIgnoreCase(nameOFUser)) {
lay.setGravity(Gravity.LEFT);
cardViewreal.setCardBackgroundColor(Color.parseColor("#ffffff"));
} else {
lay.setGravity(Gravity.LEFT);
cardViewreal.setCardBackgroundColor(Color.parseColor("#f5f4f5"));
}
}
void setSender(String sender){
TextView tvsender = (TextView) view.findViewById(R.id.senderidtv);
tvsender.setText(sender);
}
void SetMessage(String message){
TextView tvmessage = (TextView) view.findViewById(R.id.messageidtv);
tvmessage.setText(message);
}
}
Check your set direction method. It always sets direction to the left
void setDirection(final String name){
if(!name.equalsIgnoreCase(nameOFUser)) {
lay.setGravity(Gravity.LEFT);
cardViewreal.setCardBackgroundColor(Color.parseColor("#ffffff"));
//linearLayoutback.setBackgroundResource(R.drawable.white);
}
else{
lay.setGravity(Gravity.LEFT);
cardViewreal.setCardBackgroundColor(Color.parseColor("#f5f4f5"));
// linearLayoutback.setBackgroundResource(R.drawable.white);
}

How can I get selecting value in FirebaseRecyclerAdapter?

I want to obtain determined value from the Realtime DB using FirebaseRecyclerAdapter. This is my class:
public class ProductAvtivity extends AppCompatActivity {
private final String TAG = "ProductActivity";
private RecyclerView mRecyclerView;
private RecyclerView.LayoutManager mLayoutManager;
private FirebaseDatabase database;
private DatabaseReference mProductReference;
private Query mMachineReference;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_avtivity);
database = FirebaseDatabase.getInstance();
mProductReference = database.getReference().child("products");
String idmachine = getIntent().getStringExtra("idmachine");
mMachineReference = database.getReference().child("machines").child("_id").equalTo(idmachine);
Log.d(TAG + "idmachine",idmachine);
mRecyclerView = (RecyclerView) findViewById(R.id.product_recyclerview);
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
FirebaseRecyclerAdapter<Product,ProductViewHolder> adapter =
new FirebaseRecyclerAdapter<Product, ProductViewHolder>(Product.class,R.layout.view_product,ProductViewHolder.class,mProductReference) {
#Override
protected void populateViewHolder(ProductViewHolder viewHolder, Product model, int position) {
viewHolder.productName.setText(model.name);
}
};
mRecyclerView.setAdapter(adapter);
}
public static class ProductViewHolder extends RecyclerView.ViewHolder{
TextView productName;
public ProductViewHolder(View itemView) {
super(itemView);
productName = (TextView) itemView.findViewById(R.id.product_name);
}
}
}
I tried it with this solution but it's not working because the activity crashes:
public class ProductAvtivity extends AppCompatActivity {
private final String TAG = "ProductActivity";
private RecyclerView mRecyclerView;
private RecyclerView.LayoutManager mLayoutManager;
private FirebaseDatabase database;
private DatabaseReference mProductReference;
private Query mMachineReference;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_avtivity);
database = FirebaseDatabase.getInstance();
mProductReference = database.getReference().child("products");
String idmachine = getIntent().getStringExtra("idmachine");
mMachineReference = database.getReference().child("machines").child("_id").equalTo(idmachine);
Log.d(TAG + "idmachine",idmachine);
mRecyclerView = (RecyclerView) findViewById(R.id.product_recyclerview);
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
FirebaseRecyclerAdapter<Product,ProductViewHolder> adapter =
new FirebaseRecyclerAdapter<Product, ProductViewHolder>(Product.class,R.layout.view_product,ProductViewHolder.class,mProductReference) {
#Override
protected void populateViewHolder(final ProductViewHolder viewHolder, final Product model, int position) {
mMachineReference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Machine machine = dataSnapshot.getValue(Machine.class);
for (Integer i : machine.id_prod){
if (model._id == i){
viewHolder.productName.setText(model.name);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
};
mRecyclerView.setAdapter(adapter);
}
public static class ProductViewHolder extends RecyclerView.ViewHolder{
TextView productName;
public ProductViewHolder(View itemView) {
super(itemView);
productName = (TextView) itemView.findViewById(R.id.product_name);
}
}
}
I have a list of product for all machine IDs and then I pass this from another activity where the user selects the machine. By this selection, I have to list in the recyclerview the product connected to it.

Categories

Resources