Firebase数据库无法检索值

时间:2018-10-20 14:48:53

标签: java android

我无法将多个TextView的文本更改为从Firebase实时数据库检索的值。

我的数据库如下:Database

我当前的代码是:

public class SearchResult extends AppCompatActivity {
    TextView searchResultHSPName,searchResultHSPStatus, searchResultHSPType,searchResultHSPRating;
    Button messageButton;
    private static final String TAG = "SearchResult";
    DatabaseReference rootReference;

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

        searchResultHSPName = (TextView) findViewById(R.id.hspNameSearchResultTextView);
        searchResultHSPType = (TextView) findViewById(R.id.hspTypeSearchResultTextView);
        searchResultHSPRating = (TextView) findViewById(R.id.hspRatingSearchResultTextView);
        searchResultHSPStatus = (TextView) findViewById(R.id.hspStatusSearchResultTextView);
        //take the searched name from searchFunction and query db for the user details
        Intent intent = getIntent();
        final String searchedHSPName = intent.getExtras().getString("SearchFunctionMessage");
        rootReference = FirebaseDatabase.getInstance().getReference();
        DatabaseReference userReference = rootReference.child("HSPUsers");
        userReference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    HSPUsers user = snapshot.getValue(HSPUsers.class);
                    String userName = user.HSPName;
                    if(userName == searchedHSPName){
                        searchResultHSPName.setText(userName);
                        searchResultHSPStatus.setText(user.HSPStatus);
                        searchResultHSPRating.setText(user.HSPRating);
                        searchResultHSPType.setText(user.HSPType);
                    }
                }
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });
    }

}

我的HSPUsers类是:

public class HSPUsers {
    //HSP user contains 4 fields of data UserName, Status,Type,Rating
    public String HSPName,HSPStatus, HSPType,HSPRating;

    //constructor for datasnapshot
    public HSPUsers(){
    }


    public HSPUsers(String username, String status, String type, String rating){
        this.HSPName=username;
        this.HSPStatus=status;
        this.HSPType=type;
        this.HSPRating=rating;
    }


    //get and set methods
    public String getHSPUserName() {
        return HSPName;
    }

    public void setHSPUserName(String HSPUserName) {
        this.HSPName = HSPUserName;
    }

    public String getHSPUserStatus() {
        return HSPStatus;
    }

    public void setHSPUserStatus(String HSPUserStatus) {
        this.HSPStatus = HSPUserStatus;
    }

    public String getHSPUserType() {
        return HSPType;
    }

    public void setHSPUserType(String HSPUserType) {
        this.HSPType = HSPUserType;
    }

    public String getHSPUserRating() {
        return HSPRating;
    }
}

    public String getHSPUserRating() {
        return HSPRating;
    }
}

理想情况下,我想将textViews更改为用户的Name / Status / Rating / Type。但是,文本视图没有变化,我只剩下UnchangedTextViews

在将TextViews更改为HSPName / rating / Status / type方面是否会提出建议?

预先感谢您的任何建议。

0 个答案:

没有答案