1

im currently working on an app that downloads files using the firebase storage and firebase database to store the url linking to the storage. the fire base database tree looks a bit like this

Apps | app1 | name my app download 1 url http://link to download about About the app your downloading image image url for the app your downloading downloads = 0 | app2 (Same data as above)

i have custom base adapter which allows me to add the image url and about value. when somebody click an item in my list it downloads and installs the app you selected, by grabbing the position of the item and then grabbing the url from the database using httpget.

what id like is when they click the item it auto increments the child downloads by 1 so it acts as a download counter.

 mrootRef = new Firebase("https://admob-app-id-3020090926.firebaseio.com/Apps");
    mlv = (ListView) findViewById(R.id.lvFilmapps);
    mrootRef.child("Filmapps").addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {

            final String appname = dataSnapshot.child("name").getValue(String.class);
            final String url = dataSnapshot.child("url").getValue(String.class);
            final String apkname = dataSnapshot.child("apk").getValue(String.class);
            final String about = dataSnapshot.child("about").getValue(String.class);
            final String appimg = dataSnapshot.child("image").getValue(String.class);


            FilmArray.add(appname);
            urlList.add(url);
            ApkList.add(apkname);
            Aboutapp.add(about);
            appImage.add(appimg);

            //String[] convertion  for the BaseAdpater
            String[] arr = FilmArray.toArray(new String[FilmArray.size()]);
            String[] arr1 = Aboutapp.toArray(new String[Aboutapp.size()]);
            String[] arr2 = appImage.toArray(new String[appImage.size()]);

            mlv.setAdapter(new dataListAdapter(arr,arr1,arr2));
            new dataListAdapter(arr,arr1,arr2).notifyDataSetChanged();

            mlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {


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

                     newnamestring = ApkList.get(i).toString();
                    apkNames = FilmArray.get(i).toString();
                    new DownloadFileFromURL().execute(urlList.get(i));
                }
            });
        }
p9p studios
  • 13
  • 1
  • 1
  • 3
  • Easiest way would probably be to use AddListenerForSingleValueEvent to get the current download count, then add one to that, and save it with setValue(), if you expect people to concurrently be downloading at the same time, then you might want to look into Transaction as @TheSunshinator mentioned in his anwer. – Linxy Jan 24 '17 at 20:30

1 Answers1

3

I think you might want to look at Transaction. If reference is your FirebaseReference pointing to the number to be incremented, you can do something like this:

reference.runTransaction(new Transaction.Handler() {
    @Override
    public Transaction.Result doTransaction(MutableData mutableData) {
        if (mutableData.getValue() == null) {
            mutableData.setValue(1);
        } else {
            int count = mutableData.getValue(Integer.class);
            mutableData.setValue(count + 1);
        }
        return Transaction.success(mutableData);
    }

    @Override
    public void onComplete(DatabaseError databaseError, boolean success, DataSnapshot dataSnapshot) {
        // Analyse databaseError for any error during increment
    }
});

Note that two Transactions can't be executed at the same time, so you are sure that the count is updated properly. You can have more details here.

Sunshinator
  • 940
  • 11
  • 23
  • thanks i had seen this method actually as i was browsing the forum @TheSunshinator im going to have a look now keep you posted thanks also – p9p studios Jan 24 '17 at 22:35
  • thanks @Linxy yeah im going to look in to the method above – p9p studios Jan 24 '17 at 22:36
  • where would i point the ref? i thought if i pointed it at the child "url" it would count every time some one clicked url. which it does but it counts the over all urls. ie from every app download rather than each individual app. how could i fix this? – p9p studios Jan 24 '17 at 23:34
  • If your users are logged in, you can use their UID as a child node into node "url" and set their apps download count there (url -> userUid -> count). Then, `reference` from above code would point to url/ – Sunshinator Jan 25 '17 at 04:42
  • 1
    thanks @TheSunshinator i thought maybe something to do with the uid. i was looking at the example from fire base you posted yesterday. im thinking i may actually make a post class like they have because i do use similar functions as in the demo and i think it would generally tidy alot of my code. thanks for all your help. i will post results as if i manage to figure it properly. On a plus side so far though i now have a general download counter that counts overall downloads as a stat on my home page lol – p9p studios Jan 25 '17 at 18:03