Android多个类使用相同的回调接口

时间:2017-03-02 20:21:24

标签: java android class methods interface

我有一个Async方法,它从类中的数据库中获取数据。正在从另一个类中的适配器访问此方法。我需要在两个不同的类中使用相同的db helper方法。一个是活动,另一个是片段的适配器。

目前,我正在定义适配器类中的接口。对此接口的引用将直接传递给db方法,然后用于触发回调。

问题是接口引用需要是动态的,以便考虑使用它的多个类。我尝试将接口放在一个单独的类中,但无法使其工作。

这是我的代码。任何帮助表示赞赏!

数据库助手类

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) {

            }
        });
    }
}

适配器类

公共类PopularAdapter扩展了RecyclerView.Adapter {

public class SpinnerSelectionListener实现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);
    }
}

0 个答案:

没有答案