简体   繁体   中英

how do i auto increment a value in firebase

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

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 Transaction s can't be executed at the same time, so you are sure that the count is updated properly. You can have more details here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM