即使我登录新用户,我的Textviews上也不会删除以前用户的数据

时间:2018-04-12 09:28:09

标签: android firebase firebase-realtime-database firebase-authentication

以前,我登录用户并在Textviews中显示其个人信息,并且它第一次正常工作,但是当我退出并输入另一个用户时,前一个用户的信息仍显示在textview中,据说他自己的个人资料。我尝试使用第三个用户登录,仍然显示第一个用户的信息。以下是我的代码..非常感谢您的帮助。

Userinformation.java

public class Userinformation  {
    private String establishmentname;
    private String address;
    private String contact;
    private String availability;
    private String price;
    private String type;
    private String condition;
    private String ownername;

    public Userinformation(){
    }

    public Userinformation (String establishmentname ,String address,String contact, String availability, String price, String type, String condition, String ownername){
        this.establishmentname=establishmentname;
        this.address=address;
        this.contact=contact;
        this.availability=availability;
        this.price=price;
        this.type=type;
        this.condition=condition;
        this.ownername=ownername;
    }

    public String getEstablishmentname() {
        return establishmentname;
    }

    public void setEstablishmentname(String establishmentname) {
        this.establishmentname = establishmentname;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getContact() {
        return contact;
    }

    public void setContact(String contact) {
        this.contact = contact;
    }

    public String getAvailability() {
        return availability;
    }

    public void setAvailability(String availability) {
        this.availability = availability;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getCondition() {
        return condition;
    }

    public void setCondition(String condition) {
        this.condition = condition;
    }

    public String getOwnername() {
        return ownername;
    }

    public void setOwnername(String ownername) {
        this.ownername = ownername;
    }
}

Profile.java

package *****************;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public class Profile extends AppCompatActivity implements     View.OnClickListener {
    ImageView userprofile;
    TextView establishmentdisplay;
    TextView addressdisplay;
    TextView contactdisplay;
    TextView availabilitydisplay;
    TextView pricedisplay;
    TextView typedisplay;
    TextView conditiondisplay;
    TextView ownernamedisplay;
    Button btnsave;

    private FirebaseAuth mAuth;
    private FirebaseDatabase mFirebaseDatabase;
    private FirebaseAuth.AuthStateListener mAuthListener;
    DatabaseReference myRef;

    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        userprofile=(ImageView) findViewById(R.id.userprofile);

        btnsave = (Button) findViewById(R.id.savechangesbtn);

        establishmentdisplay = (TextView) findViewById(R.id.establishmentdisplay);
        addressdisplay = (TextView) findViewById(R.id.addressdisplay);
        contactdisplay = (TextView) findViewById(R.id.contactdisplay);
        availabilitydisplay = (TextView) findViewById(R.id.availabilitydisplay);
        pricedisplay = (TextView) findViewById(R.id.pricedisplay);
        typedisplay = (TextView) findViewById(R.id.typedisplay);
        conditiondisplay = (TextView) findViewById(R.id.conditiondisplay);
        ownernamedisplay = (TextView) findViewById(R.id.ownernamedisplay);

        btnsave.setOnClickListener(this);

        mAuth = FirebaseAuth.getInstance();

        final FirebaseDatabase database = FirebaseDatabase.getInstance();
        final DatabaseReference user = database.getReference("HomeownerUsers");
        user.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot ds : dataSnapshot.getChildren()){
                    String establishmentname=ds.child("Establishment Name").getValue().toString();
                    String address=ds.child("Address").getValue().toString();
                    String contact=ds.child("Contact").getValue().toString();
                    String availability=ds.child("Availablility").getValue().toString();
                    String price=ds.child("Price").getValue().toString();
                    String type=ds.child("Type").getValue().toString();
                    String condition=ds.child("Conditions").getValue().toString();
                    String ownername=ds.child("Owner Name").getValue().toString();

                    establishmentdisplay.setText(establishmentname);
                    addressdisplay.setText(address);
                    contactdisplay.setText(contact);
                    availabilitydisplay.setText(availability);
                    pricedisplay.setText(price);
                    typedisplay.setText(type);
                    conditiondisplay.setText(condition);
                    ownernamedisplay.setText(ownername);
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        if(mAuth.getCurrentUser() == null){
            finish();
            startActivity(new Intent(this,Homeownerlogin.class));
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if(mAuthListener !=null){
            mAuth.removeAuthStateListener(mAuthListener);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.menulogout:
                FirebaseAuth.getInstance().signOut();
                finish();
                startActivity(new Intent(this, Homeownerlogin.class));
                Toast.makeText(Profile.this,"Log out Successfully",Toast.LENGTH_SHORT).show();
        }
        return true;
    }

    @Override
    public void onClick(View v) {
        if (v==btnsave){
            finish();
            startActivity(new Intent(Profile.this,Profile.class));
        }
    }
}

这是我的数据库:

Here is my Firebase Database

1 个答案:

答案 0 :(得分:0)

您已在整个HomeownerUsers节点上添加了firebase侦听器,然后循环搜索结果。相反,您应该在当前用户的HomeownerUsers的特定子项上添加侦听器。为了在代码中进行更多优化,您可以使用Userinformation类来检索数据,而不是单独地检索每个字段。

所以你的addValueEventListener将如下所示:

final DatabaseReference user = database.getReference("HomeownerUsers").child(UserUID);
user.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String establishmentname=dataSnapshot.child("Establishment Name").getValue().toString();
        String address=dataSnapshot.child("Address").getValue().toString();
        String contact=dataSnapshot.child("Contact").getValue().toString();
        String availability=dataSnapshot.child("Availablility").getValue().toString();
        String price=dataSnapshot.child("Price").getValue().toString();
        String type=dataSnapshot.child("Type").getValue().toString();
        String condition=dataSnapshot.child("Conditions").getValue().toString();
        String ownername=dataSnapshot.child("Owner Name").getValue().toString();

        establishmentdisplay.setText(establishmentname);
        addressdisplay.setText(address);
        contactdisplay.setText(contact);
        availabilitydisplay.setText(availability);
        pricedisplay.setText(price);
        typedisplay.setText(type);
        conditiondisplay.setText(condition);
        ownernamedisplay.setText(ownername)
    }
}

更多信息可在the firebase docs中找到。