digital_pro
digital_pro

Reputation: 183

How to retrieve data depending upon which admin has logged in

My app has 5 different colleges as admin and each college has their own academic data. Same user interface and same programming logic is used for all colleges, just the data inside it is changing depending upon which college has logged in. I am not aware what kind of approach is to be used as creating different activities for different colleges having same code is not an elegant way also using inheritance doesn't help. I am using firebase to store and retrieve data. Any help or suggestion is appreciated!

Below is the code:

public class College extends AppCompatActivity {
    private DatabaseReference mDatabase;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mDatabase= FirebaseDatabase.getInstance().getReference().child("College 1");

        mDatabase.child("Alerts").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String alerts = dataSnapshot.getValue(String.class);
                textview.setText(alerts);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

    }
}

Here is the database: Here is the database where data is stored.

Upvotes: 0

Views: 43

Answers (1)

Kapil Parmar
Kapil Parmar

Reputation: 911

while login save your username locally like shared prefrence(or sqlite db ), for shared prefrence use this

SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();

 editor.putString("username", yourCollagename);

 editor.apply();

now retrive it as

String savedUsername= prefs.getString("username", null);

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
and pass that username in firebase instance 

    mDatabase= FirebaseDatabase.getInstance().getReference().child(savedUsername);

Upvotes: 1

Related Questions