Firebase data to RecyclerView shows only one item - android

I'm retrieving data from firebase and trying to display that in recycler view, every thing is fine but it only shows one item in the list (there are total 14 items)
comment if you do not understand the question
This is my main activity where i get datasnapshots from firebase
public class cakes_activity extends AppCompatActivity {
RecyclerView recyclerView;
ArrayList<Data> listdata;
private DatabaseReference mref;
private NorthAdaptor north;
private final Context context = this;
public int counter = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cakes);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
EditText editText = (EditText) findViewById(R.id.editText) ;
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//FIREBASE
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mref= FirebaseDatabase.getInstance().getReference("Cakes");
mref.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Data data = dataSnapshot.getValue(Data.class);
listdata = new ArrayList<>();
listdata.add(data);
north = new NorthAdaptor(context,listdata);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.hasFixedSize();
recyclerView.setAdapter(north);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
and the following code is my adaptor class:
public class NorthAdaptor extends RecyclerView.Adapter<NorthAdaptor.NorthHolder> {
private LayoutInflater inflater;
ArrayList<Data> northy;
public NorthAdaptor(Context context,ArrayList<Data> northy)
{
inflater=LayoutInflater.from(context);
this.northy=northy;
}
public NorthHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.each_item,parent,false);
NorthHolder holder = new NorthHolder(view);
return holder;
}
#Override
public void onBindViewHolder(NorthHolder holder, int position) {
holder.text2.setText(northy.get(position).getName());
holder.text3.setText("₹."+northy.get(position).getRS());
}
#Override
public int getItemCount() {
return northy.size();
}
class NorthHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
TextView text,text2,text3;
ImageView img;
private final Context context;
public NorthHolder(View itemview)
{
super(itemview);
context = itemView.getContext();
CardView card = (CardView) itemView.findViewById(R.id.card);
text3=(TextView)itemView.findViewById(R.id.Rs);
text2 = (TextView) itemView.findViewById(R.id.itemtext2);
img = (ImageView) itemView.findViewById(R.id.itemimage);
card.setOnClickListener(this);
}
#Override
public void onClick(View view){
Toast.makeText(
cakes_activity.this,
"Your message here",
Toast.LENGTH_SHORT
).show();
}
}
}
this is my java class to store the data received from firebase
public class Data {
private String ID,Name,image,RS;
public Data(){
}
public Data(String ID, String Name, String image, String RS) {
this.ID = ID;
this.Name = Name;
this.image= image;
this.RS = RS;
}
public String getId() {
return ID;
}
public String getRS() {
return RS;
}
public void setId(String id) {
this.ID = id;
}
public String getName() {
return Name;
}
public void setName(String Name) {
this.Name = Name;
}
public String getimage() {
return image;
}
public void setImg(String image) {
this.image = image;
}
}

listdata = new ArrayList<>();
north = new NorthAdaptor(context,listdata);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.hasFixedSize();
recyclerView.setAdapter(north);
you need to write this code in onCreate instead of onchildadded

Related

RecyclerView shows only two textview?others are not display...items are not displaying

i have facing issue like only two items are displayed others two items are not displayed they are contact and flat no this items are not displaying...i tried also removing and adding others textview but through this only two items are displayed.... Now I want to display the data on a RecyclerView which doesn't seem to work, I'm pretty sure that my code is good but something doesn't seem to work and I can't find it.i also try all the things suggested by stackoverflow but nothing can happen so that i think i need to ask...
Here the file
public class RecieptFP extends AppCompatActivity {
private RecyclerView mFirestoreList;
private FirebaseFirestore firebasefirestore;
private FirestoreRecyclerAdapter adapter;
Button Back;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reciept_fp);
Back = findViewById(R.id.btndashboard);
firebasefirestore = FirebaseFirestore.getInstance();
mFirestoreList = findViewById(R.id.firestore_list);
//query
Query query = firebasefirestore.collection("UserDataR");
//RecyclerOptions
FirestoreRecyclerOptions<RecieptModel> options = new FirestoreRecyclerOptions.Builder<RecieptModel>()
.setQuery(query, RecieptModel.class)
.build();
adapter = new FirestoreRecyclerAdapter<RecieptModel, RecieptViewHolder>(options) {
#NonNull
#Override
public RecieptViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listsingleiteam, parent, false);
return new RecieptViewHolder(view);
}
#Override
protected void onBindViewHolder(RecieptViewHolder recieptViewHolder, int i, RecieptModel recieptModel) {
recieptViewHolder.list_name.setText(recieptModel.getName());
recieptViewHolder.list_amount.setText(recieptModel.getAmount());
recieptViewHolder.list_contact.setText(recieptModel.getContact());
recieptViewHolder.list_Flatno.setText(recieptModel.getFlatNo());
}
};
//viewholder
mFirestoreList.setHasFixedSize(false);
mFirestoreList.setLayoutManager(new LinearLayoutManager(this));
mFirestoreList.setAdapter(adapter);
Back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i1 = new Intent(RecieptFP.this, Dashboard.class);
startActivity(i1);
}
});
}
private class RecieptViewHolder extends RecyclerView.ViewHolder {
private TextView list_name;
private TextView list_amount;
private TextView list_contact;
private TextView list_Flatno;
public RecieptViewHolder(#NonNull View itemView) {
super(itemView);
list_name = itemView.findViewById(R.id.list_name);
list_amount = itemView.findViewById(R.id.list_amount);
list_contact = itemView.findViewById(R.id.list_contact);
list_Flatno = itemView.findViewById(R.id.list_Flatno);
}
}
#Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
}
Recipet Model
package com.societypay;
public class RecieptModel {
private String Name;
private String Amount;
private String Contact;
private String FlatNo;
private RecieptModel(){}
private RecieptModel(String Name,String Amount,String Contact,String FlatNo){
this.Name=Name;
this.Amount=Amount;
this.Contact=Contact;
this.FlatNo=FlatNo;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getAmount() {
return Amount;
}
public void setAmount(String amount) {
Amount = amount;
}
public String getContact() {
return Contact;
}
public void setContact(String contact) {
Contact = contact;
}
public String getFlatNo() {
return FlatNo;
}
public void setFlatNo(String flatNo) {
FlatNo = flatNo;
}
}
Please use following pojo and try.
public class RecieptModel
{
private String MobileNo;
private String Amount;
private String Flat_no;
private String Name;
public String getMobileNo ()
{
return MobileNo;
}
public void setMobileNo (String MobileNo)
{
this.MobileNo = MobileNo;
}
public String getAmount ()
{
return Amount;
}
public void setAmount (String Amount)
{
this.Amount = Amount;
}
public String getFlat_no ()
{
return Flat_no;
}
public void setFlat_no (String Flat_no)
{
this.Flat_no = Flat_no;
}
public String getName ()
{
return Name;
}
public void setName (String Name)
{
this.Name = Name;
}
#Override
public String toString()
{
return "ClassPojo [MobileNo = "+MobileNo+", Amount = "+Amount+", Flat_no = "+Flat_no+", Name = "+Name+"]";
}
}

RecyclerView is not being populated with items from firebase just a blank Activity Page

I'm still relatively new to Android and firebase and trying to populate recyclerview from firebase database but for some reasons that I can't get, the view is not populated.The activity loads but all I see is a blank activity page.I'd like to populate these views using data from a Firebase Database just to get three data(name,email,phonenos) of Users.
public class TestUsersActivity extends AppCompatActivity {
public TextView mname,email,phone;
private RecyclerView recyclerView;
FirebaseAuth mAuth;
DatabaseReference mDatabase;
private FirebaseRecyclerAdapter adapter;
private LinearLayoutManager linearLayoutManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_users);
recyclerView = findViewById(R.id.listuser);
mDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
linearLayoutManager = new LinearLayoutManager(getApplicationContext());
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(linearLayoutManager);
fetch();
}
private void fetch() {
FirebaseRecyclerOptions<Users> options = new FirebaseRecyclerOptions.Builder<Users>()
.setQuery(mDatabase,Users.class)
.build();
adapter = new FirebaseRecyclerAdapter<Users, ViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull ViewHolder myViewHolder, int i, #NonNull Users users) {
myViewHolder.setName(users.getFname() + " "+ users.getLname());
myViewHolder.setEmail(users.getEmail());
myViewHolder.setphone(users.getPhonenos());
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_list_row,parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
#Override
public void onDataChanged() {
super.onDataChanged();
}
};
recyclerView.setAdapter(adapter);
adapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View itemView) {
super(itemView);
mname = itemView.findViewById(R.id.name);
email = itemView.findViewById(R.id.email);
phone = itemView.findViewById(R.id.phoneNos);
}
public void setName(String string) {
mname.setText(string);
}
public void setEmail(String string) {
email.setText(string);
}
public void setphone(String string){
phone.setText(string);
}
}
}
Below is my modelclass that i want to get just the name,email and phone nos from.
public class Users {
private String fname;
private String lname;
private String email;
private String phonenos;
private String jobTitle;
private String dateCreated;
private String lastlogin;
public Users(){
}
public Users(String fname, String lname, String email, String phonenos, String jobTitle, String dateCreated, String lastlogin) {
this.fname = fname;
this.lname = lname;
this.email = email;
this.phonenos = phonenos;
this.jobTitle = jobTitle;
this.dateCreated = dateCreated;
this.lastlogin = lastlogin;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhonenos() {
return phonenos;
}
public void setPhonenos(String phonenos) {
this.phonenos = phonenos;
}
public String getJobTitle() {
return jobTitle;
}
public void setJobTitle(String jobTitle) {
this.jobTitle = jobTitle;
}
public String getDateCreated() {
return dateCreated;
}
public void setDateCreated(String dateCreated) {
this.dateCreated = dateCreated;
}
public String getLastlogin() {
return lastlogin;
}
public void setLastlogin(String lastlogin) {
this.lastlogin = lastlogin;
}
}
I don't know if you must pass a query instead of a database reference
make the query a member variable
......
private Query query;
......
do this:
query = FirebaseDatabase.getInstance().getReference().child("Users");
and pass it like this to options
FirebaseRecyclerOptions<Users> options = new FirebaseRecyclerOptions.Builder<Users>()
.setQuery(query,Users.class)
.build();
ALSO MAKE SURE YOUR FIREBASE SECURITY RULES ARE NOT SET TO READ FALSE

com.google.firebase.database.DatabaseException: Failed to convert value of type java.util.ArrayList to String

I'm doing a search in my firebase database (real time database), but I'm having these errors in return, could someone help me fix it?
this search is being done through an application I have the following error
PID: 15597
com.google.firebase.database.DatabaseException: Failed to convert value of type java.util.ArrayList to String
MAINACTIVITY
public class SalasActivity extends AppCompatActivity {
private EditText mSearchField;
private ImageButton mSearchBtn;
private RecyclerView mResultList;
private DatabaseReference mUserDatabase;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_salas);
mUserDatabase = FirebaseDatabase.getInstance().getReference("grupos");
mSearchField = (EditText) findViewById(R.id.search_field);
mSearchBtn = (ImageButton) findViewById(R.id.search_btn);
mResultList = (RecyclerView) findViewById(R.id.result_list);
mResultList.setHasFixedSize(true);
mResultList.setLayoutManager(new LinearLayoutManager(this));
mSearchBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String searchText = mSearchField.getText().toString();
firebaseUserSearch(searchText);
}
});
}
private void firebaseUserSearch(String searchText) {
Toast.makeText(SalasActivity.this, "Started Search", Toast.LENGTH_LONG).show();
Query firebaseSearchQuery = mUserDatabase.orderByChild("nome").startAt(searchText).endAt(searchText + "\uf8ff");
FirebaseRecyclerAdapter<Users, UsersViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Users, UsersViewHolder>(
Users.class,
R.layout.list_layout,
UsersViewHolder.class,
firebaseSearchQuery
) {
#Override
protected void populateViewHolder(UsersViewHolder viewHolder, Users model, int position) {
viewHolder.setDetails(getApplicationContext(), model.getName(), model.getStatus(), model.getImage());
}
};
mResultList.setAdapter(firebaseRecyclerAdapter);
}
// View Holder Class
public static class UsersViewHolder extends RecyclerView.ViewHolder {
View mView;
public UsersViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setDetails(Context ctx, String userName, String userStatus, String userImage){
TextView user_name = (TextView) mView.findViewById(R.id.name_text);
TextView user_status = (TextView) mView.findViewById(R.id.status_text);
ImageView user_image = (ImageView) mView.findViewById(R.id.profile_image);
user_name.setText(userName);
user_status.setText(userStatus);
Glide.with(ctx).load(userImage).into(user_image);
USERS.JAVA
public class Users {
public String nome, membros, id;
public Users(){
}
public String getName() {
return nome;
}
public void setName(String name) {
this.nome = name;
}
public String getImage() {
return membros;
}
public void setImage(String image) {
this.membros = image;
}
public String getStatus() {
return id;
}
public void setStatus(String status) {
this.id = status;
}
public Users(String name, String image, String status) {
this.nome = name;
this.membros = image;
this.id = status;
}
}
DATABASE
DATABASE IMAGE

Recyclerview not retrieving data

RecyclerView messageList;
List<Messages> messagesList = new ArrayList<>();
private LinearLayoutManager linearLayoutManager;
private MessageAdapter mAdapter;
mAdapter = new MessageAdapter(messagesList);
messageList = (RecyclerView) findViewById(R.id.messageList);
linearLayoutManager = new LinearLayoutManager(this);
messageList.setHasFixedSize(true);
messageList.setLayoutManager(linearLayoutManager);
messageList.setAdapter(mAdapter);
private void fetchMessages() {
rootRef.child("Messages").child(MessageSenderId).child(MessageRecieverId).addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Messages message = dataSnapshot.getValue(Messages.class);
messagesList.add(message);
mAdapter.notifyDataSetChanged();
Log.d("Tag", String.valueOf(dataSnapshot.getValue(Messages.class)));
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Adapter
public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MessageViewHolder>{
private List<Messages> messagesList;
private FirebaseAuth mAuth;
public MessageAdapter (List<Messages>messagesList)
{
this.messagesList = messagesList;
}
public class MessageViewHolder extends RecyclerView.ViewHolder{
public TextView messageText;
public MessageViewHolder(View view)
{
super(view);
messageText = (TextView)view.findViewById(R.id.message_text);
}
}
#Override
public int getItemCount()
{
return messagesList.size();
}
#Override
public MessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
View V = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_chat_custom,parent,false);
mAuth = FirebaseAuth.getInstance();
return new MessageViewHolder(V);
}
#SuppressLint("ResourceType")
#Override
public void onBindViewHolder(MessageViewHolder holder, int position) {
String current_user_id = mAuth.getInstance().getCurrentUser().getUid();
Messages messages = messagesList.get(position);
String from_user = messages.getFrom();
// if (from_user!=null && from_user.equals(current_user_id)){
// holder.messageText.setBackgroundResource(R.drawable.text_background1);
// holder.messageText.setTextColor(Color.red(R.color.red));
// }else {
// holder.messageText.setBackgroundResource(R.drawable.text_background2);
// holder.messageText.setTextColor(Color.red(R.color.black));
// }
holder.messageText.setText(messages.getMessage());
}
}
Messages Class
public class Messages {
private String from;
private String message;
private String type;
private boolean seen;
private long time;
public Messages(){};
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public boolean isSeen() {
return seen;
}
public void setSeen(boolean seen) {
this.seen = seen;
}
public long getTime() {
return time;
}
public void setTime(long time) {
this.time = time;
}
}
When i debugged i got this output
04-28 18:17:57.537 21546-21546/com.appmaster.akash.messageplus D/Tag: com.appmaster.akash.messageplus.Messages#461a55
04-28 18:17:57.538 21546-21546/com.appmaster.akash.messageplus D/Tag: com.appmaster.akash.messageplus.Messages#c9d4c6a
04-28 18:17:57.545 21546-21546/com.appmaster.akash.messageplus D/Tag: com.appmaster.akash.messageplus.Messages#3e055b
04-28 18:17:57.546 21546-21546/com.appmaster.akash.messageplus D/Tag: com.appmaster.akash.messageplus.Messages#f6c31f8
04-28 18:17:57.548 21546-21546/com.appmaster.akash.messageplus D/Tag: com.appmaster.akash.messageplus.Messages#61b06d1
04-28 18:17:57.549 21546-21546/com.appmaster.akash.messageplus D/Tag: com.appmaster.akash.messageplus.Messages#c3bc536
04-28 18:17:57.550 21546-21546/com.appmaster.akash.messageplus D/Tag: com.appmaster.akash.messageplus.Messages#94d9c37
Screenshot https://ibb.co/e9ui8c
IDS
final DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
MessageSenderId = mAuth.getCurrentUser().getUid();
MessageRecieverId = getIntent().getStringExtra("Recievers_Id");
Before i had a listview and it was fine... the logic was correct and so was the method so all the data was fetched and displayed in the listview... then i decided to change it to recyclerview and the methods in the recyclerview adapter had been changed so im not sure if everything is correct... please help me out
RecyclerView messageList;
ArrayList<Messages> messagesList = new ArrayList<>();
private LinearLayoutManager linearLayoutManager;
private MessageAdapter mAdapter;
private Messages messages;
mAdapter = new MessageAdapter(fetchMessage(),getMessages());
messageList = (RecyclerView) findViewById(R.id.messageList);
linearLayoutManager = new LinearLayoutManager(this);
messageList.setHasFixedSize(true);
messageList.setLayoutManager(linearLayoutManager);
messageList.setAdapter(mAdapter);
private ArrayList<Messages>fetchMessages() {rootRef.child("Messages").child(MessageSenderId).child(MessageRecieverId).addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
messages = dataSnapshot.getValue(Messages.class);
messagesList.add(messages);
mAdapter.notifyDataSetChanged();
Log.d("Tag", String.valueOf(dataSnapshot.getValue(Messages.class)));
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
return messgesList
}
private Messages getMessage(){
return messages;
}
Then in your adapter
public class MessageAdapter extends
RecyclerView.Adapter<MessageAdapter.MessageViewHolder>{
private List<Messages> messagesList;
private FirebaseAuth mAuth;
private Messages messages;
public MessageAdapter (List<Messages>messagesList, Messsages messages)
{
this.messagesList = messagesList;
this.messages = messages;
}
public class MessageViewHolder extends RecyclerView.ViewHolder{
public TextView messageText;
public MessageViewHolder(View view)
{
super(view);
messageText = (TextView)view.findViewById(R.id.message_text);
}
}
#Override
public int getItemCount()
{
return messagesList.size();
}
#Override
public MessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
View V=LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_chat_custom, parent,false);
mAuth = FirebaseAuth.getInstance();
return new MessageViewHolder(V);
}
#SuppressLint("ResourceType")
#Override
public void onBindViewHolder(MessageViewHolder holder, int position) {
String current_user_id = mAuth.getInstance().getCurrentUser().getUid();
messages = messagesList.get(position);
String from_user = messages.getFrom();
// if (from_user!=null && from_user.equals(current_user_id)){
//
holder.messageText.setBackgroundResource(R.drawable.text_background1);
// holder.messageText.setTextColor(Color.red(R.color.red));
// }else {
//
holder.messageText.setBackgroundResource(R.drawable.text_background2);
//
holder.messageText.setTextColor(Color.red(R.color.black));
// }
holder.messageText.setText(messages.getMessage());
}
}

how to retrieve all data from firebase and display on listview android?

I am making an android app ,which is get order from customers,but i faced a problem . I am trying to Retrieve Data from Firebase and display in a list view. I can get the data back from Firebase but when it displays in the listview it just displays one data in many times. I want to be displayed on one line for each record. Can anyone see where i am going wrong??
Database Image
ListView Image
OrderHistory
public class OrderHistory
{
String ammount,photoId,trxId,name,copy,photoSize,date;
public OrderHistory(String name,String photoId,String trxId,String copy,String photoSize,String ammount,String date)
{
this.name = name;
this.ammount = ammount;
this.photoId = photoId;
this.copy = copy;
this.photoSize = photoSize;
this.trxId = trxId;
this.date = date;
}
public String getAmmount() {
return ammount;
}
public void setAmmount(String ammount) {
this.ammount = ammount;
}
public String getPhotoId() {
return photoId;
}
public void setPhotoId(String photoId) {
this.photoId = photoId;
}
public String getTrxId() {
return trxId;
}
public void setTrxId(String trxId) {
this.trxId = trxId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCopy() {
return copy;
}
public void setCopy(String copy) {
this.copy = copy;
}
public String getPhotoSize() {
return photoSize;
}
public void setPhotoSize(String photoSize) {
this.photoSize = photoSize;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
OrderHistoryAdapter
public class OrderHistoryAdapter extends BaseAdapter {
private List<OrderHistory> orderHistories;
Context context;
public OrderHistoryAdapter(Context context, List<OrderHistory> myOrderInformations) {
this.context = context;
this.orderHistories = myOrderInformations;
}
#Override
public int getCount() {
return orderHistories.size();
}
#Override
public Object getItem(int position) {
return orderHistories.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.show_my_order_history, parent, false);
final TextView txtName, txtdate, txtPhotoId, trxId,txtAmount,txtPhotoSize,txtCopy;
txtName = (TextView)view.findViewById(R.id.txtName);
txtdate = (TextView)view.findViewById(R.id.txtDate);
txtPhotoId = (TextView)view.findViewById(R.id.txtPhotoId);
trxId = (TextView)view.findViewById(R.id.txtTrx);
txtAmount = (TextView)view.findViewById(R.id.txtAmount);
txtPhotoSize = (TextView)view.findViewById(R.id.txtSize);
txtCopy = (TextView)view.findViewById(R.id.txtCopy);
txtName.setText(orderHistories.get(position).getName());
txtdate.setText(orderHistories.get(position).getDate());
txtPhotoId.setText(orderHistories.get(position).getPhotoId());
trxId.setText(orderHistories.get(position).getTrxId());
txtAmount.setText(orderHistories.get(position).getAmmount());
txtCopy.setText(orderHistories.get(position).getCopy());
txtPhotoSize.setText(orderHistories.get(position).getPhotoSize());
return view;
}
}
OrderHistoryList
public class OrderHistoryList extends AppCompatActivity
{
private DatabaseReference databaseReference;
private List<OrderHistory> orderHistories;
private static String phoneNumber;
private ListView listView;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_my_order);
Firebase.setAndroidContext(this);
listView = (ListView)findViewById(R.id.listView);
getAllOrderFromFirebase();
}
private void getAllOrderFromFirebase()
{
orderHistories = new ArrayList<>();
databaseReference = FirebaseDatabase.getInstance().getReference("order");
String phone = getIntent().getExtras().getString("phone");
databaseReference.orderByChild("phone").equalTo(phone).addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String amount, photoId, trxId, name, copy, photoSize, date;
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
name = snapshot.child("name").getValue(String.class);
photoId = snapshot.child("photoId").getValue(String.class);
amount = snapshot.child("totalAmount").getValue(String.class);
trxId = snapshot.child("trxId").getValue(String.class);
photoSize = snapshot.child("photoSize").getValue(String.class);
date = snapshot.child("date").getValue(String.class);
copy = snapshot.child("totalCopy").getValue(String.class);
orderHistories.add(new OrderHistory(name, photoId, trxId, copy, photoSize, amount, date));
}
OrderHistoryAdapter adapter;
adapter = new OrderHistoryAdapter(OrderHistoryList.this, orderHistories);
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
I guess your problem is by the way you refer to your data so instead of this
databaseReference = FirebaseDatabase.getInstance().getReference("order");
use this
databaseReference = FirebaseDatabase.getInstance().getReference().child("order");
and you didn't use a query object to query your database reference
so now you don't query directly from databaseReference like the way you did it
instead you do this:
Query query=databaseReference.orderByChild("phone").equalTo(phone);
once you have a query that fits you now add on child listener and continue the rest of your code:
query.addChildEventListener(new ChildEventListener() {
//the rest of your code goes here(on child added/changed/......)
)};

Categories

Resources