Zohaib ZJ Zohaib ZJ - 3 years ago 70
Android Question

Add authenticated Firebase users to Listview

I want to add Firebase authenticated users (after entering correct username and password) to ListView. To get the better understanding here is the flow of my application.

Here is the image of first layout when app starts

1- When user clicks on Add Patient button the app redirects to another activity where the use must enter the correct email and password

2- After successful login the the app open the previous activity again(add patient activity) and add the logged in user to the ListView. Here is the xml of add patient

<TextView
android:layout_width="match_parent"
android:layout_height="59dp"
android:layout_alignParentTop="true"
android:id="@+id/addpatient"
android:gravity="center"
android:drawableLeft="@drawable/ic_add_circle_outline_black_48dp"
android:drawablePadding="-170dp"
android:textStyle="bold"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:text="Add Patient"
android:layout_marginTop="57dp" />

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@+id/addpatient"
android:background="#000" />

<ListView
android:id="@+id/listpatient"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_weight="1"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:longClickable="true"
android:layout_marginTop="119dp"
android:layout_alignParentBottom="true">
</ListView>




Please note that i do not want to get the list of users from Firebase Database, I'm using firebase authentication just not firebase database. here is the code of add patient activity

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_patient);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar1);
setSupportActionBar(toolbar);

auth = FirebaseAuth.getInstance();

addPatient = (TextView)findViewById(R.id.addpatient);
listPatient = (ListView)findViewById(R.id.listpatient);
arrayList = new ArrayList<String>();
if(patient == null)
{
Toast.makeText(getApplicationContext(),"No user found",Toast.LENGTH_SHORT).show();
}
else
{
patient = FirebaseAuth.getInstance().getCurrentUser();
arrayList.add(String.valueOf(patient));
}
adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList);
listPatient.setAdapter(adapter);
adapter.notifyDataSetChanged();
addPatient.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent it = new Intent(AddPatient.this,DoctorPanel_Signin.class);
startActivity(it);
}
});


Problem is the listview is not showing any data. and if i remove IF statement the app is crashing by saying attempt to invoke virtual method on null pointer reference something like that. And the other thing that i'm thinking is that if i want to add 3 users then firebaseaut.getinstance.getcurrentuser will always return the 1 user and will override the previous user again and again, please consider that too.

Answer Source

This is happening because you are verifying if patient == null before the patient object is even created. So with other words your patient object is always null. To solve this, you need to change the logic of your if statement like this:

FirebaseAuth.AuthStateListener authListener = new FirebaseAuth.AuthStateListener() {
    @Override
    public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
        FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
        if (firebaseUser != null) {
            Log.d("TAG", "You are signed in Firebase!");
            arrayList.add(String.valueOf(patient));
        } else {
            Log.d("TAG", "You are signed out from Firebase!");
        }
    }
};

Your database should look like this:

Firebase-root
     |
     --- Users
           |
           --- userUid1
           |      |
           |      --- signedInSuccessfully: false
           |
           --- userUid2
                  |
                  --- signedInSuccessfully: true //changes after signed in successfully
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download