firebase addchildevent监听器没有解雇

时间:2018-04-19 06:14:11

标签: java android firebase firebase-realtime-database

我有一个firebase帮助器类,我在其中保存数据并且还设置了add child listener事件。问题是,在添加子侦听器事件后,它不会触发。

public class FbaseHelper {

DatabaseReference dbRef;
Boolean saved= null;
String codeDescJSON = null;
Gson myGson;
CodeDescMast codeDescMast;
ArrayList<CodeDescMast> cdeDescAryList;

public FbaseHelper() { }
public FbaseHelper(DatabaseReference refDb) {
    this.dbRef = refDb;
}

public void read(Context context) {

    String csvLine;

    InputStream is=null ;
    try {
        is = context.getAssets().open("a.csv") ;
        BufferedReader reader = new BufferedReader(new InputStreamReader(is)) ;
        codeDescMast = new CodeDescMast();
        while((csvLine = reader.readLine())!=null) {
           String[] row = csvLine.split(",") ;
           if (! row[1].equals("Desc")) {

               codeDescMast.setCode(Integer.valueOf(row[0]));
               codeDescMast.setDesc(row[1]);
               fBaseSave();
           }
        }

        is.close();

    } catch(IOException ioex) {
        ioex.printStackTrace();

    }
}

public Boolean fBaseSave() {
    try {
        dbRef.push().setValue(codeDescMast) ;
        saved = true;

    }catch (DatabaseException dbEx) {
        dbEx.printStackTrace();
        saved = false ;

    }

    return saved;
}

   public ArrayList<CodeDescMast> retrieve() {
    dbRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            getData(dataSnapshot,cdeDescAryList);
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            getData(dataSnapshot,cdeDescAryList);
        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }


    });

    return cdeDescAryList;
}

public void getData(DataSnapshot pDtSnpSht,ArrayList<CodeDescMast> pCdeDscMst) {

    int code;
    String desc;

    pCdeDscMst.clear();

    for(DataSnapshot dSnpSht : pDtSnpSht.getChildren()) {
        code = dSnpSht.getValue(CodeDescMast.class).getCode() ;
        desc = dSnpSht.getValue(CodeDescMast.class).getDesc() ;

        CodeDescMast cdeDscMst = new CodeDescMast(code,desc);
        pCdeDscMst.add(cdeDscMst) ;
    }


}

这是mainactivity.java

public class MainActivity extends AppCompatActivity {

Spinner spnr;
DatabaseReference dbRef;
FbaseHelper fBaseHelp;
CdeDscAdapt cdeDscAdpt;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    spnr = (Spinner) findViewById(R.id.ddlbcode);

    dbRef= FirebaseDatabase.getInstance().getReference() ;

    fBaseHelp = new FbaseHelper(dbRef);
    dbInit();

    cdeDscAdpt = new CdeDscAdapt(this, fBaseHelp.cdeDescAryList) ;
    spnr.setAdapter(cdeDscAdpt);

    spnr.setOnItemSelectedListener(spnrItmSelListnr);
}
publ

public void dbInit() {
    fBaseHelp.read(getApplicationContext());
    fBaseHelp.retrieve();
}

public AdapterView.OnItemSelectedListener spnrItmSelListnr = new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        CodeDescMast codeDesc = (CodeDescMast) parent.getItemAtPosition(position);
        int code = codeDesc.getCode();
        String desc = codeDesc.getDesc();
        Toast.makeText(MainActivity.this,"Code:"+ Integer.toString(code) + "/nDesc: "+ desc ,Toast.LENGTH_LONG).show();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
};

}

问题是应该在主要活动中定义监听器还是在另一个类中定义监听器。

1 个答案:

答案 0 :(得分:0)

您可以在所需的任何类中定义从Firebase数据库中读取的代码。但重要的是,代码在Android活动的正确生命周期方法中被调用

现在,您的主要活动似乎是fBaseHelp.read();,它会读取CSV文件但不会从Firebase中读取任何数据。您提供的唯一一种从Firebase(retrieve())读取的方法未被调用,因此不会从Firebase读取任何数据。

您也想从主要活动中致电fBaseHelp.read()

请注意,此签名似乎是一个问题的处方:

public ArrayList<CodeDescMast> retrieve() {

数据是从Firebase异步加载的。由于Android中的代码执行是同步的,因此您似乎可以从CodeDescMast方法返回retrieve列表。由于在没有看到实际代码的情况下很难提供帮助,我将提供以前关于该主题的问题列表: