I'm new to Firebase. I'm trying to query the firebase database and display all the child objects of the results in a ListView. I've no errors but nothing is being displayed. It doesn't crash but it doesn't do anything either. Please help me out.
The contents of my database:
Here's my code for data retrieval:
imgbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
key = search.getText().toString().trim();
Firebase newRef = new Firebase("https://stockmanager-142503.firebaseio.com/Items");
Query query = newRef.orderByChild("Idno").equalTo(key);
query.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Map<String,Item> td = (HashMap<String,Item>) dataSnapshot.getValue();
List<Item> valuesToMatch = new ArrayList<Item>(td.values());
myAdapter myadapter=new myAdapter(getActivity(),valuesToMatch);
mlistView.setAdapter(myadapter);
}
#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(FirebaseError firebaseError) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(firebaseError.getMessage())
.setTitle("Error!")
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
});
Here's my adapter class:
public class myAdapter extends BaseAdapter {
private List<Item> items;
private Context mContext;
public myAdapter(Context mContext, List<Item> items) {
this.mContext=mContext;
this.items=items;
}
#Override
public int getCount() {
return 0;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
LinearLayout linearLayout;
Item item=items.get(i);
if (view == null) {
linearLayout = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.rowitem, viewGroup, false);
} else {
linearLayout = (LinearLayout)view;
}
TextView text1=(TextView)linearLayout.findViewById(R.id.text1);
TextView text2=(TextView)linearLayout.findViewById(R.id.text2);
TextView text3=(TextView)linearLayout.findViewById(R.id.text3);
TextView text4=(TextView)linearLayout.findViewById(R.id.text4);
TextView text5=(TextView)linearLayout.findViewById(R.id.text5);
text1.setText(item.getIdno());
text2.setText(item.getName());
text3.setText(item.getBrand());
text4.setText(item.getCost());
text5.setText(item.getDate());
return null;
}
}
Here is the item class:
public class Item {
private String Type;
private String Name;
private String Brand;
private String Cost;
private String Date;
private String Store;
private String Idno;
public String getName() {
return Name;
}
public String getIdno() {
return Idno;
}
public String getCost() {
return Cost;
}
public void setDate(String date) {
Date = date;
}
public String getBrand() {
return Brand;
}
public String getStore() {
return Store;
}
public String getType() {
return Type;
}
public String getDate() {
return Date;
}
}
I fixed it myself. Apparently, the query returned a list of objects and hence I replaced the Hashmap with a for loop to getchildren and add them to a List. But my listview simply wouldn't display the retreived data. Finally, I used the firebase UI List Adpater given below and that fixed it. Thanks everyone!
imgbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
key = scanContent;
Query q = dbRef.orderByChild("idno").equalTo(key);
FirebaseListAdapter<Item> adapter =new FirebaseListAdapter<Item>(getActivity(),Item.class,R.layout.rowitem,q) {
#Override
protected void populateView(View v, Item item, int position) {
TextView text1=(TextView)v.findViewById(R.id.text1);
TextView text2=(TextView)v.findViewById(R.id.text2);
TextView text3=(TextView)v.findViewById(R.id.text3);
TextView text4=(TextView)v.findViewById(R.id.text4);
TextView text5=(TextView)v.findViewById(R.id.text5);
text1.setText(item.getIdno());
text2.setText(item.getName());
text3.setText(item.getBrand());
text4.setText(item.getCost());
text5.setText(item.getDate());
solditem=item;
}
};
sell.setText("Add this result to Sold Items List?");
sList.setAdapter(adapter);
}
});
Related
i need to get bestseller values from AGS Biryani in recyler view , No idea about how to add the child values to the recyler adapter based on parent node
Model
public class Food_List {
private String itemname;
private String itemdescrp;
private long itemnewprice;
private long itemoldprice;
public String getItemname() {
return itemname;
}
public void setItemname(String itemname) {
this.itemname = itemname;
}
public String getItemdescrp() {
return itemdescrp;
}
public void setItemdescrp(String itemdescrp) {
this.itemdescrp = itemdescrp;
}
public long getItemnewprice() {
return itemnewprice;
}
public void setItemnewprice(long itemnewprice) {
this.itemnewprice = itemnewprice;
}
public long getItemoldprice() {
return itemoldprice;
}
public void setItemoldprice(long itemoldprice) {
this.itemoldprice = itemoldprice;
}
}
Food_List_ViewHolders
ViewHolders to add the value in Recyler view
public class Food_List_ViewHolders extends RecyclerView.ViewHolder {
View mView;
private Context context;
Context mContext;
String nodata;
public Food_List_ViewHolders(View itemView) {
super(itemView);
mView=itemView;
}
#SuppressLint("SetTextI18n")
public void setDetails(Context applicationContext, final String itemname, String itemdescrp,
final long itemnewprice,
final long itemoldprice
)
{
final TextView dishitemname=mView.findViewById(R.id.dishheader);
TextView dishitemnamedescrp=mView.findViewById(R.id.dishheaderdescrp);
TextView dishitemnameoldprice=mView.findViewById(R.id.itemoldprice);
TextView dishitemnamenewprice=mView.findViewById(R.id.itemnewprice);
dishitemname.setText(itemname);
dishitemnamedescrp.setText(itemdescrp);
dishitemnamenewprice.setText(applicationContext.getString(R.string.Rup) + itemnewprice);
dishitemnameoldprice.setText(applicationContext.getString(R.string.Rup) + itemoldprice);
mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View mView) {
Context context = mView.getContext();
}
});
}
}
Main Activity
Don't Know how to add Child values in DataSnapshot
mRecycleriew =findViewById(R.id.my_recycler_views);
mRecycleriew.setLayoutManager(new LinearLayoutManager(this));
mFirebaseDatabase= FirebaseDatabase.getInstance();
mRef=mFirebaseDatabase.getReference().child("restaurants").equalTo("AGS Biryani");
//DatabaseReference restaurantsRef = mRef.child("restaurants");
mRef(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()){
progressDoalog.dismiss();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
private void mRef(ValueEventListener valueEventListener) {
}
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Food_List,Food_List_ViewHolders> firebaseRecyclerAdapter=
new FirebaseRecyclerAdapter<Food_List, Food_List_ViewHolders>(
Food_List.class,
R.layout.item_child,
Food_List_ViewHolders.class,
mRef)
{
#Override
protected void populateViewHolder(Food_List_ViewHolders viewHolder, Food_List model, int position) {
viewHolder.setDetails(getApplicationContext(),model.getItemname(),model.getItemdescrp(),model.getItemnewprice(),model.getItemoldprice());
}
};
mRecycleriew.setAdapter(firebaseRecyclerAdapter);
}
Essentially you're doing:
mRef=mFirebaseDatabase.getReference().child("restaurants").equalTo("AGS Biryani");
FirebaseRecyclerAdapter<Food_List,Food_List_ViewHolders> firebaseRecyclerAdapter=
new FirebaseRecyclerAdapter<Food_List, Food_List_ViewHolders>(
Food_List.class,
R.layout.item_child,
Food_List_ViewHolders.class,
mRef)
Which means that you're showing all restaurants with a priority of AGS Biryani. That's not what you're trying to do, so you'll need to modify your ref:
mRef=mFirebaseDatabase.getReference().child("restaurants/AGS Biryani/bestsellers");
When you pass this ref into the FirebaseRecyclerAdapter, it will show all bestsellers for AGS Biryani.
I want to get data from a Firebase database in a list view. When I open the page, it appears as follows:
When I go back and open it again, it now appears like this:
The database contains the following data:
My two problems are:
It takes a lot of time to retrieve the data
More data is retrieved than expected - it is repeated in the listview.
My code:
public class CustomAdapter extends BaseAdapter{
Context c;
ArrayList<Doctor> doctors;
public CustomAdapter(Context c, ArrayList<Doctor> doctors) {
this.c = c;
this.doctors = doctors;
}
#Override
public int getCount() {
return doctors.size();
}
#Override
public Object getItem(int position) {
return doctors.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null)
{
convertView= LayoutInflater.from(c).inflate(R.layout.model,parent,false);
}
TextView nameTxt= (TextView) convertView.findViewById(R.id.nameTxt);
TextView propTxt= (TextView) convertView.findViewById(R.id.propellantTxt);
TextView descTxt= (TextView) convertView.findViewById(R.id.descTxt);
final Doctor s= (Doctor) this.getItem(position);
nameTxt.setText(s.getDoctorName());
propTxt.setText(s.getSpecialist());
descTxt.setText(s.getAdress());
//ONITECLICK
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(c,s.getDoctorName(),Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
and this for model class
public class Doctor {
String doctorName,specialist,adress;
public Doctor() {
}
public String getDoctorName() {
return doctorName;
}
public String getSpecialist() {
return specialist;
}
public String getAdress() {
return adress;
}
The Activity:
public class DoctorsActivity extends AppCompatActivity {
DatabaseReference db;
FirebaseHelper helper;
CustomAdapter adapter;
ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_doctors);
lv = (ListView) findViewById(R.id.lv);
db = FirebaseDatabase.getInstance().getReference().child("Doctor");
helper = new FirebaseHelper(db);
adapter = new CustomAdapter(this, helper.retrieve());
lv.setAdapter(adapter);
}
Firebase Helper:
public class FirebaseHelper {
DatabaseReference db;
Boolean saved;
ArrayList<Doctor> doctors = new ArrayList<>();
public FirebaseHelper(DatabaseReference db) {
this.db = db;
}
//IMPLEMENT FETCH DATA AND FILL ARRAYLIST
private void fetchData(DataSnapshot dataSnapshot) {
doctors.clear();
for (DataSnapshot ds : dataSnapshot.getChildren()) {
Doctor doctor = dataSnapshot.getValue(Doctor.class);
doctors.add(doctor);
}
}
//RETRIEVE
public ArrayList<Doctor> retrieve() {
db.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot);
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
return doctors;
}
Hello I'm using a custom adapter to assign how I handle my firebase objects and then using a dialog to to assign the variables on my fragment. I need help concerning the fact that the data's I'm assigning to is not pushing into my firebase. Can I ask why?
*EDIT ADDDED INFO CLASS AND DATABASE SCHEMA
Codes of my Adapter
public class UserTransactionAdapter extends RecyclerView.Adapter<UserTransactionAdapter.ViewHolder> {
private List<Info> mInfo;
private Callback mCallback;
private DatabaseReference userref;
public UserTransactionAdapter(Callback callback) {
mCallback = callback;
mInfo = new ArrayList<>();
userref = FirebaseDatabase.getInstance().getReference().child("Transactions");
userref.addChildEventListener(new UserChildEventListener());
}
class UserChildEventListener implements ChildEventListener{
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Info info = dataSnapshot.getValue(Info.class);
info.setKey(dataSnapshot.getKey());
mInfo.add(0,info);
notifyDataSetChanged();
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
String key = dataSnapshot.getKey();
Info updatedInfo = dataSnapshot.getValue(Info.class);
for (Info info : mInfo){
if (info.getKey().equals(key)){
info.setValues(updatedInfo);
notifyDataSetChanged();
return;
}
}
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
String key = dataSnapshot.getKey();
for(Info info: mInfo){
if (info.getKey().equals(key)){
mInfo.remove(info);
break;
}
}
notifyDataSetChanged();
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.modelinfo, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, final int position) {
final Info info = mInfo.get(position);
holder.mNameTextView.setText(info.getName());
holder.mMonthTextView.setText(info.getMonth());
holder.mPayTextView.setText(info.getPay());
holder.mUntilTextView.setText(info.getUntil());
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCallback.onEdit(info);
}
});
}
public void remove(Info info) {
//TODO: Remove the next line(s) and use Firebase instead
userref.child(info.getKey()).removeValue();
}
#Override
public int getItemCount() {
return mInfo.size();
}
public void add(Info info) {
//TODO: Remove the next line(s) and use Firebase instead
userref.push().setValue(info);
}
public void update(Info info, String newName,String newMonth,String newPay, String newUntil) {
//TODO: Remove the next line(s) and use Firebase instead
info.setName(newName);
info.setMonth(newMonth);
info.setPay(newPay);
info.setUntil(newUntil);
userref.child(info.getKey()).setValue(info);
}
public interface Callback {
public void onEdit(Info info);
}
class ViewHolder extends RecyclerView.ViewHolder {
private TextView mNameTextView;
private TextView mMonthTextView;
private TextView mPayTextView;
private TextView mUntilTextView;
public ViewHolder(View itemView) {
super(itemView);
mNameTextView = (TextView) itemView.findViewById(R.id.nameTxt);
mMonthTextView = (TextView) itemView.findViewById(R.id.monthTxt);
mPayTextView = (TextView) itemView.findViewById(R.id.payTxt);
mUntilTextView = (TextView) itemView.findViewById(R.id.untilTxt);
}
}
And codes of my Fragment
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View view = inflater.inflate(R.layout.fragment_transaction, container, false);
FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showAddEditDialog(null);
}
});
mAdapter = new com.google.firebase.ikuzou.database.UserTransactionAdapter(this);
RecyclerView view1 = (RecyclerView) view.findViewById(R.id.recycler_view);
view1.setLayoutManager(new LinearLayoutManager(getContext()));
view1.setHasFixedSize(true);
view1.setAdapter(mAdapter);
return view;
}
private void showAddEditDialog(final Info info) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle(getString(info == null ? R.string.dialog_add_title2 : R.string.dialog_edit_title2));
View view = getLayoutInflater().inflate(R.layout.dialog_info, null, false);
builder.setView(view);
final EditText nameEditText = (EditText) view.findViewById(R.id.nameEditText);
final EditText monthEditText = (EditText) view.findViewById(R.id.monthEditText);
final EditText payEditText = (EditText) view.findViewById(R.id.payEditText);
final EditText untilEditText = (EditText) view.findViewById(R.id.untilEditText);
if (info != null) {
// pre-populate
nameEditText.setText(info.getName());
monthEditText.setText(info.getMonth());
payEditText.setText(info.getPay());
untilEditText.setText(info.getUntil());
TextWatcher textWatcher = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// empty
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// empty
}
#Override
public void afterTextChanged(Editable s) {
String name = nameEditText.getText().toString();
String month = monthEditText.getText().toString();
String pay = payEditText.getText().toString();
String until = untilEditText.getText().toString();
mAdapter.update(info, name,month,pay,until );
}
};
nameEditText.addTextChangedListener(textWatcher);
monthEditText.addTextChangedListener(textWatcher);
payEditText.addTextChangedListener(textWatcher);
untilEditText.addTextChangedListener(textWatcher);
}
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (info == null) {
String name = nameEditText.getText().toString();
String month = monthEditText.getText().toString();
String pay = payEditText.getText().toString();
String until = untilEditText.getText().toString();
mAdapter.add(new Info(name, month,pay,until));
}
}
});
builder.setNegativeButton(android.R.string.cancel, null);
builder.create().show();
Can I know the errors on my codes so that I can know why this is happening? Or perhaps is it because I still haven't created a transaction node yet that's why this is happening?
EDIT
I tried to add child nodes manually and now I'm getting
DatabaseException: Can't convert object of type java.lang.String to
type error
Here is my info class codes
public class Info {
private String name,month,pay,until,key;
public Info (){
}
public Info(String name, String month, String pay, String expire) {
this.name= this.name;
this.month= this.month;
this.pay= this.pay;
this.until= this.until;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public String getMonth(){
return month;
}
public void setMonth(String month){
this.month=month;
}
public String getPay(){
return pay;
}
public void setPay(String pay){
this.pay=pay;
}
public String getUntil(){
return until;
}
public void setUntil(String until){
this.until=until;
}
#Exclude
public String getKey(){
return key;
}
public void setKey(String key){
this.key = key;
}
public void setValues(Info updatedInfo) {
this.name=updatedInfo.name;
this.month=updatedInfo.month;
this.pay=updatedInfo.pay;
this.until=updatedInfo.until;
}
My Database
You have added a childEventListner and all of these functions will only run when a child is added,removed or changed under the node "Transactions". So try adding a child to the node "Transactions".
According to the docs, child_added is triggered once for each existing child and then again every time a new child is added to the specified path.
Ref: https://firebase.google.com/docs/database/admin/retrieve-data
userref.child(info.getKey()).setValue(info);
this line should be like this.
userref.child(info.getKey()).push().setValue(info);
FIXED IT. It was a stupid mistake by me from my Info.class
Added a this.
public Info(String name, String month, String pay, String expire) {
this.name= this.name;
this.month= this.month;
this.pay= this.pay;
this.until= this.until;
Instead of
this.name= name;
this.month= month;
this.pay= pay;
this.until= until;
So solution was removing the this and simply put month,pay,until
Problem is fixed now by removing this
Hello there I have a fragment and then a listview on it. It would normally load the listview data when application is launched, but when i click the fragment again using the navigation drawer, all of it would disappear and would leave a blank page as a result.
My listview does indeed show itself but the problem is when I rotate the screen or initiate the fragment again, the listview disappears. ex. from prntscr.com/ihx3fg where the listview is visible. into prntscr.com/ihx3th where the listview disappeared.
Can I ask why this is happening?
My CustomAdapter code
public class CustomAdapter extends BaseAdapter {
Context c;
ArrayList<User1> user;
public CustomAdapter(Context c, ArrayList<User1> user) {
this.c = c;
this.user = user;
}
#Override
public int getCount() {
return user.size();
}
#Override
public Object getItem(int pos) {
return user.get(pos);
}
#Override
public long getItemId(int pos) {
return pos;
}
#Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
if(convertView==null){
convertView= LayoutInflater.from(c).inflate(R.layout.model,viewGroup,false);
}
TextView nameTxt= (TextView) convertView.findViewById(R.id.nameTxt);
TextView emailTxt= (TextView) convertView.findViewById(R.id.emailTxt);
TextView dateTxt= (TextView) convertView.findViewById(R.id.dateTxt);
final User1 user= (User1) this.getItem(position);
nameTxt.setText(user.getName());
emailTxt.setText(user.getEmail());
dateTxt.setText(user.getDate());
return convertView;
}
Firebasehelper Code
public class Firebasehelper {
DatabaseReference db;
Boolean saved=null;
ArrayList<User1> users=new ArrayList<>();
public Firebasehelper(DatabaseReference db) {
this.db = db;
}
public Boolean save(User1 user) {
if (user == null) {
saved = false;
} else {
try {
db.child("Accounts").child("Transaction").push().setValue(user);
saved = true;
} catch (DatabaseException e) {
e.printStackTrace();
saved = false;
}
}
return saved;
}
private void fetchData(DataSnapshot dataSnapshot)
{
User1 user=dataSnapshot.getValue(User1.class);
users.add(user);
}
public ArrayList<User1>retrieve(){
db.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot);
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
return users;
My Fragment Activity Code
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_member2, container, false);
lv = (ListView) view.findViewById(R.id.lv);
db= FirebaseDatabase.getInstance().getReference().child("Accounts").child("Users");
helper=new Firebasehelper(db);
adapter=new CustomAdapter(getActivity(),helper.retrieve());
lv.setAdapter(adapter);
// Inflate the layout for this fragment
return view;
}
My User Class
public User1() {
this.name=name;
this.email=email;
this.date=date;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String getEmail(){
return email;
}
public void setEmail(String email){
this.email=email;
}
public String getDate(){
return date;
}
public void setDate(String date){
this.date = date;
}
Really confused on where the problem lies in since the app is running perfectly except for the fact that the fragments listview would disappear or became blank when initiating the fragment again.
This behaviour is present because of the adapter which is disconected every time the onStop() method is called.
To solve this, you need to set the adapter again in the onStart() method.
Solved it by adding a child event listener
ex.
db.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
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/......)
)};