Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display firebase child objects in listview

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: 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;
    }


}
like image 765
Judy T Raj Avatar asked Jun 16 '26 19:06

Judy T Raj


1 Answers

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);
                                      }
                                  });
like image 168
Judy T Raj Avatar answered Jun 19 '26 08:06

Judy T Raj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!