0

I have a firebase helper class in which I am saving the data and also have the add child listener event setup. The problem is that, after adding a child listener event, it is not firing.

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


}

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

    }
};

}

The question is should the listener be defined inside the main activity or can it be defined in another class.

megjosh
  • 101
  • 1
  • 9

1 Answers1

0

You can define the code that reads from the Firebase Database in any class you want. What is important though, is that the code gets invoked from within the correct lifecycle method of the Android activity.

Right now it seems your main activity invokes fBaseHelp.read();, which reads a CSV file but does not read any data from Firebase. The only method that you provided that reads from Firebase (retrieve()) is not called, so no data will be read from Firebase.

You'll want to call fBaseHelp.read() from your main activity too.

Note that this signature seems like a recipe for problems though:

public ArrayList<CodeDescMast> retrieve() {

Data is loaded from Firebase asynchronously. Since code execution in Android is synchronous, it seems unlike you can return the list of CodeDescMast from the retrieve method. Since it's hard to help with that without seeing the actual code, I'll instead provide a list of previous questions on the topic:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I have edited my question . now you can view the MainActivity.xmland FbaseHelper.java.I am trying to display code and description as a drop down in a spinner. A csv file contains the code and description . I read this file and store into Firebase. I want to read this data from Firebase . Till now I have been able to read the csv file and store the values into firebase. the addchild event listener is not getting called.In the onCreate of the mainactivity I am using a method in the helper class to define the listener and its implementation – megjosh Apr 19 '18 at 18:05
  • OnCreate is the lifecycle method of android that I am using to define the listener and its implementation. I dont have to return anything from the retrieve method. I will define a getter that will return this array object on demand. – megjosh Apr 19 '18 at 18:11
  • this reading of a file and storing into firebase and then retrieving from Firebase to load the spinner is a synchronous task whereas in android this loading/retrieving turns into an asynchronous task. I think I need to seperate these two tasks. Reading of files and loading into firebase will happen sepereately. Spinner values will be populated seperately. My choice of addchildeventlistener was also not correct should have used valueventlistener . The FbaseHelper class should be instantiated in the adapater rather than the mainactivity so that i will get access to adapter.notifydatachanged – megjosh Apr 21 '18 at 04:32