从firebase数据库中检索已保存的数据,并将其显示在新活动的多个文本视图中

时间:2017-08-14 15:09:06

标签: android json firebase firebase-realtime-database

package com.example.ogho.thesistorone;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
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;

import org.w3c.dom.Text;

public class OrderActivity extends AppCompatActivity {

    private FirebaseAuth mFirebaseAuth;
    private FirebaseUser mFirebaseUser;

    private String mUserId;
    DatabaseReference db;
    boolean saved = false;
    Button btnStatus;
    TextView txtCourse,txtTypOfJob,txtDuration,txtPrice,txtTopic;

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

        txtCourse=(TextView)findViewById(R.id.txtCourse);
        txtTypOfJob=(TextView)findViewById(R.id.txtTypOfJob);
        txtDuration=(TextView)findViewById(R.id.txtDuration);
        txtPrice=(TextView)findViewById(R.id.txtPrice);
        txtTopic=(TextView)findViewById(R.id.txtTopic);
        btnStatus=(Button)findViewById(R.id.btnRed);



        getSupportActionBar().setDisplayUseLogoEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setIcon(R.mipmap.thsstrn);

        mFirebaseAuth = FirebaseAuth.getInstance();
        mFirebaseUser = mFirebaseAuth.getCurrentUser();

        if (mFirebaseUser == null) {
            loadLogInView();
        } else {
            mUserId = mFirebaseUser.getUid();

            db = FirebaseDatabase.getInstance().getReference().child("Client").child(mUserId).child("Orders");


        }
        db.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {

                    Orders value = postSnapshot.getValue(Orders.class);
               /* String type = null;
                if (value != null) {
                    type = value.getType();
                }
                String duration = null;
                if (value != null) {
                    duration = value.getDuration();
                }
                String topic = null;
                if (value != null) {
                    topic = value.getTopic();
                }
                String price = null;
                if (value != null) {
                    price = value.getPrice();
                }
                String traffic = null;
                if (value != null) {
                    traffic = value.getUid();
                }*/

                    String type="Type of Order:  "+value.getType();
                    String duration="Duration:  "+value.getDuration();
                    String topic="Topic:  "+value.getTopic();
                    String price="Price:  "+value.getPrice();
                    String traffic="Status:  "+value.getUid();

                    Client K = dataSnapshot.getValue(Client.class);
                    /*String name = null;
                    if (K != null) {
                        name = K.getName();
                    }*/
                    String name="Name:  "+K.getName();

                    txtCourse.setText(name);
                    txtTypOfJob.setText(type);
                    txtDuration.setText(duration);
                    txtTopic.setText(topic);
                    txtPrice.setText(price);
                    btnStatus.setText(traffic);

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

            }
        });
    }

    private void loadLogInView() {
        Intent intent = new Intent(this, LoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        if (id == R.id.action_logout) {
            mFirebaseAuth.signOut();
            loadLogInView();
        }else if(id==R.id.edit_profile){
            Intent i = new Intent(this, MainActivity.class);
            startActivity(i);
            //startActivity(new Intent(this,MainActivity.class));
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

我正在尝试从firebase显示已保存的数据,然后在几个TextView上显示它并且它没有这样做。这是数据库类

package com.example.ogho.thesistorone;

/**
 * Created by ogho on 25/07/2017.
 */

public class Orders {

public String course;
public String level;
public String type;
public String topic;
public String duration;
public String otherInfo;
public String price;
public String uid;

public Orders(){

}

public String getCourse() {
    return course;
}
public String getLevel() {
    return level;
}
public String getType() {
    return type;
}
public String getTopic() {
    return topic;
}
public String getDuration(){
    return duration;
}
public String getOtherInfo(){
    return otherInfo;
}
public String getPrice(){
    return price;
}
public String getUid() {
    return uid;
}


public void setCourse(String course){
    this.course = course;
}
public void setLevel(String level){
    this.level = level;
}
public void setType(String type){
    this.type = type;
}
public void setTopic(String topic){
    this.topic = topic;
}
public void setDuration(String duration){
    this.duration=duration;
}
public void setPrice(String price){
    this.price=price;
}
public void setOtherInfo(String otherInfo){
    this.otherInfo=otherInfo;
}
public void setUid(String uid){
    this.uid = uid;
}

public Orders(String topic, String course, String duration, String level, String otherInfo, String price , String type, String uid) {
    this.course = course;
    this.level = level;
    this.type = type;
    this.topic = topic;
    this.duration = duration;
    this.otherInfo = otherInfo;
    this.price = price;
    this.uid = uid;
}
}

代码有什么问题?我需要帮助来检索并将数据库中的数据显示到用户界面上。

{
 "Client" : {
              "course_of_study" : "Computer Engineering",
               "institution" : "Rhodes university",
               "name" : "Ogho Enuku"
  },
  "Orders" : {
              "course" : "kfdfk",
              "duration" : "7",
              "level" : "Bachelors",
              "otherInfo" : "jddj",
              "price" : "Requested",
              "topic" : "jddj",
              "type" : "CourseWork/Assignment",
              "uid" : "Requested Quotation"
  }

以下是来自firebase实时数据库@AlexMamo

的JSON文件

0 个答案:

没有答案