Android multiple classes using same callback interface

334
March 03, 2017, at 07:50 AM

I have an Async method which is grabbing data from a database in a class. This method is being accessed from an adapter in another class. I need to use the same db helper method in two different classes. One is an activity, the other is an adapter for a fragment.

Currently, I am defining an interface within the adapter class. The reference to this interface is being passed directly through to the db method, and is then used to trigger the callback.

The issue is that the interface reference needs to be dynamic to account for multiple classes using it. I tried putting the interface in a separate class, but couldn't get that to work.

Here is my code. Any help is appreciated!

DB Helper Class

public class FbDpHelper {
    public void checkIfGameOwned(final String ASIN, String userId, final PopularAdapter.FirebaseCallback callback){  <-- issue here
        mDatabaseOwned.orderByChild("userId").equalTo(userId).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                boolean match = false;
                for(DataSnapshot child : dataSnapshot.getChildren()){
                    String gameASIN = child.child("gameASIN").getValue().toString();
                    // check if game exists
                    if(gameASIN.equals(ASIN)){
                        match = true;
                    }
                }
                callback.firebaseCallback(match);
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });
    }
}

Adapter Class

public class PopularAdapter extends RecyclerView.Adapter {

public class SpinnerSelectionListener implements AdapterView.OnItemSelectedListener, View.OnTouchListener { boolean userSelect = false; int position;

    public SpinnerSelectionListener(int position){
        this.position = position;
    }
    @Override
    public boolean onTouch(View v, MotionEvent event){
        userSelect = true;
        return false;
    }
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id){
        final String ASIN = gameDataArr.get(position).getAmazonId();
        final FirebaseDbInterface callback;
        if(userId == null){
            Toast.makeText(mContext, "You must be logged in.", Toast.LENGTH_LONG).show();
            return;
        }
        if(userSelect){
            final FirebaseDbHelper mFirebaseDbHelper = new FirebaseDbHelper();
            if(pos == 0){
                // remove from both db
                mFirebaseDbHelper.removeOwnedGame(ASIN, userId);
                mFirebaseDbHelper.removeWishlistGame(ASIN, userId);
            } else if(pos == 1){
                // check if already exists in db
                mFirebaseDbHelper.checkIfGameOwned(ASIN, userId, new FirebaseCallback() {
                    @Override
                    public void firebaseCallback(boolean match) {
                        if(match){
                            return;
                        } else {
                            // add to owned games
                            mFirebaseDbHelper.addOwnedGame(ASIN, userId);
                            // remove from wishlist
                            mFirebaseDbHelper.removeWishlistGame(ASIN, userId);
                        }
                    }
                });
            }
            userSelect = false;
        }
    }
    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {
    }

    public interface FirebaseCallback{
        void firebaseCallback(boolean match);
    }
}
READ ALSO
Error:expected / Compilation failed, see the compiler error outpot for details

Error:expected / Compilation failed, see the compiler error outpot for details

Please, I dont know what is worngI tried to find some stupid mistake, but I see nothing

270
Can Android Studio send emails?

Can Android Studio send emails?

Is there a way to make android studio send information gathered with Firebase to an email address with a list of for example in this situation accidents?

292