1
votes

In Firebase database i have a directory called Global_downloads then inside there are a few children.

child.("Casting apps") which i can locate with in my app code using a String value called AppType.

the next child.("All cast") is the child i need. i can get it into firebase by using an onitem click with my listview. which then sends it to firebase in the form of a child.

but how can i locate the name for the child(Allcast) progmatically? so i can then get the number of downloads?

enter image description here

here is my code for my child listener

@Override
public void onChildAdded(final com.firebase.client.DataSnapshot dataSnapshot, String s) {
String counter = dataSnapshot.child("Global_downloads").child(Apptype).
            child("I need this child").child("downloads").getValue(String.class);
    Downloadscount.add(counter); 

String[] arr3 = Downloadscount.toArray(new String[Downloadscount.size()]);

the rest of the items in the constructor are for other items in my listview

///my custom adapter where it returns the info to my listview
apkData = new dataListAdapter(mContext, arr, arr1, arr2,  mrootRef, Apptype,arr3);
  mlv.setAdapter(apkData);
        apkData.notifyDataSetChanged();


  mlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {


        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {


            final Firebase ref = mrootRef.getRef();

//here is where i assign the name to firebase in my counter class which works
            apkNames = AppNameList.get(i).toString();  //this is the name i want to send back to the top and use as a Child name.or can i get the child name another way.
 gc = new Global_counter(ref,Apptype,apkNames);
            gc.App_DownLoadCounter();

this is my listview there are more items on my list other than Allcast. but all cast is the only item downloaded. if more items are pressed it adds that name to the list too. the text view you can see is the downloads im trying to add

enter image description here

2

2 Answers

1
votes

To get the coresponding item that was downloaded, please use the following line of code inside onItemClick() method and then just use it inside your DatabaseReference.

String downloadName = (String) adapterView.getItemAtPosition(i);

Assuming that Global_downloads node is a direct child of your Firebase root and the value of downloads is of type Integer, to get the number of downloads please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference downloadsRef = rootRef.child("Global_downloads").child(Apptype).child(downloadName).child("downloads");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        int downloads = dataSnapshot.getValue(Integer.class);
        Log.d("TAG", downloads);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
downloadsRef.addListenerForSingleValueEvent(eventListener);

Your output will be:

0
0
votes

In your situation, the key is Apptype and the value is downloads. I'm assuming Global_dowbloads under your root reference.

To get all the downloads with app name, you need to read the data as key->value

Like this

DatabaseReference mRef = FirebaseDatabase.getInstance().getReference();
        mRef.child("Global_downloads").child(Apptype)
                .addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        for(DataSnapshot snapshot: dataSnapshot.getChildren()){
                            Log.v("app_name", snapshot.getKey());
                            Log.v("app_downloads", snapshot.getValue(String.class));
                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });