读取实时数据库节点

时间:2019-03-16 00:57:52

标签: java android firebase firebase-realtime-database

嗨,大家好,我想作为标题阅读以前在实时Firebase数据库中创建的单个节点,因此创建了该数据库:

Users
   ---- UID1
          ---- Email:
          ---- Fullname:
          ---- Phone:
          ---- Coins:
   ---- UID2
          ---- Email:
          ---- Fullname:
          ---- Phone:
          ---- Coins:

所以我的数据库具有如图所示的结构,我需要读取onDataChange并将数据写入TextView中。

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

    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("Utenti");
    myRef.child("Rapp Coins %").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            String coinsRapp = dataSnapshot.getValue(String.class);
            mCoins.setText(coinsRapp);
        }

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

        }
    });

我使用的这种方法不起作用,因为无法获取所需的数据。

我需要使用Coins值:在每个不同的登录用户中,因此每个登录用户都读取他的数据。

我是用来识别每个用户的UID值,我是根据他的手机号码创建的,所以每个用户都有自己的手机号码,可以将他识别为UID。

这是我的全部代码,您可以看到保存UID的数据包含在mPhone中,该数据已保存,然后通过SharedPreferences导入到此活动中。

public class PageCoins_Activity extends AppCompatActivity implements RewardedVideoAdListener {

    private static final String TAG = MainActivity.class.getName();
    private FirebaseAuth mAuth;
    private AdView mBannerTop;
    private AdView mBannerBot;
    private RewardedVideoAd mRewardedVideoAd;
    public double Coins;
    double coinsOp = 0.00;
    double coinsCl = 0.00;
    double coinssum = 0.00;

    Button mButton;
    Button mPhonebtn;
    TextView mCoinscounter;
    TextView mCoins;
    FirebaseDatabase mDatabase;
    EditText mPhoneEdt;
    TextView mPhone;

    @Override
    protected void onStart(){
        super.onStart();
        updateUI();
    }

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

        FirebaseDatabase database = FirebaseDatabase.getInstance();
        DatabaseReference myRef = database.getReference("Utenti");
        myRef.child("Rapp Coins %").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                String coinsRapp = dataSnapshot.getValue(String.class);
                mCoins.setText(coinsRapp);
            }

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

            }
        });

        mAuth = FirebaseAuth.getInstance();
        initFirebase();

        //counter CRD
        mCoinscounter = (TextView)findViewById(R.id.Textcoins);
        mButton = (Button)findViewById(R.id.btn2);
        mPhoneEdt = (EditText)findViewById(R.id.NumberPhEdt);
        mPhone = (TextView) findViewById(R.id.NumberPhTxt);
        mCoins = (TextView)findViewById(R.id.txtGen);
        mPhonebtn = (Button)findViewById(R.id.buttonPhone);

        //mPhoneEdt.setVisibility(View.GONE);
        //mPhone.setVisibility(View.VISIBLE);

        findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mRewardedVideoAd.isLoaded()) {
                    mRewardedVideoAd.show();
                }

                // Write a message to the database
                DatabaseReference myRef = mDatabase.getReference();
                myRef.child("Utenti").child(mPhone.getText().toString()).child("Rapp Coins Day %").setValue(mCoinscounter.getText().toString());
            }
        });

        mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
        mRewardedVideoAd.setRewardedVideoAdListener(this);

        //Set Orientation Portrait
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        // Banner Top View Coins
        mBannerTop = (AdView) findViewById(R.id.adViewTopcoins);
        AdRequest adRequest = new AdRequest.Builder().setRequestAgent("android_studio:ad_template").build();
        mBannerTop.loadAd(adRequest);

        // Banner Bot View Coins
        mBannerBot = (AdView) findViewById(R.id.adViewBotcoins);
        AdRequest adRequest1 = new AdRequest.Builder().setRequestAgent("android_studio:ad_template").build();
        mBannerBot.loadAd(adRequest1);

        getnumberprefs();

        //ADMob Video
        loadRewardedVideoAd();
    }

    private void getnumberprefs() {
        SharedPreferences numb = getSharedPreferences(Register_Activity.NUMB, MODE_PRIVATE);
        String numberphn = numb.getString(Register_Activity.KEY_NUMB,null);
        mPhone.setText(numberphn);
    }

    //boolean changepgcoins = true;

    public void changeNumber(View view) {

        /*if (changepgcoins == true){
            mPhoneEdt.setVisibility(View.GONE);
            mPhone.setVisibility(View.VISIBLE);
            changepgcoins = false;
        }else{
            mPhoneEdt.setVisibility(View.VISIBLE);
            mPhone.setVisibility(View.GONE);
            changepgcoins = true;
        }*/

    }

    private void initFirebase() {
        mDatabase = FirebaseDatabase.getInstance();
    }

    public void HomeClick(View view){
        Intent intenthome = new Intent(this, MainActivity.class);
        finish();
        startActivity(intenthome);
    }

    public void displayCrd (double amount){
        mCoinscounter.setText(String.format("%.2f", amount));
    }

    private void loadRewardedVideoAd() {
        mRewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917",
                new AdRequest.Builder().build());
    }

    public void logout(View view) {
        mAuth.signOut();
        updateUI();
    }

    private void updateUI() {
        FirebaseUser currentuser = mAuth.getCurrentUser();
        if(currentuser == null){
            Intent intTologin = new Intent(this, Login_Activity.class);
            finish();
            startActivity(intTologin);
        }
    }

    @Override
    public void onRewardedVideoAdLoaded() {
        Log.d(TAG, "Video Caricato");

    }

    @Override
    public void onRewardedVideoAdOpened() {

    }

    @Override
    public void onRewardedVideoStarted() {

    }

    @Override
    public void onRewardedVideoAdClosed() {
        loadRewardedVideoAd();
    }

    @Override
    public void onRewarded(RewardItem rewardItem) {
        Toast.makeText(this, " RAPp " + " COINS " + " : " + rewardItem.getAmount(), Toast.LENGTH_LONG).show();
        Coins += rewardItem.getAmount();
        displayCrd(Coins/40200*100);
    }

    @Override
    public void onRewardedVideoAdLeftApplication() {

    }

    @Override
    public void onRewardedVideoAdFailedToLoad(int i) {
        Log.d(TAG, "Caricamento Fallito");
    }

    @Override
    public void onRewardedVideoCompleted() {
        loadRewardedVideoAd();
    }

    @Override
    protected void onDestroy() {

        if (!isEmpty(mCoins)){

            String coinsopen = mCoins.getText().toString();
            String coinscounter = mCoinscounter.getText().toString();

            coinsOp = Double.parseDouble(String.format(coinsopen.replace(',', '.'), "%.2f"));
            coinsCl = Double.parseDouble(String.format(coinscounter.replace(',', '.'), "%.2f"));

            coinssum = (coinsOp + coinsCl);

            mCoinscounter.setText(String.valueOf(coinssum));

            // Write a message to the database
            DatabaseReference myRef = mDatabase.getReference();
            myRef.child("Utenti").child(mPhone.getText().toString()).child("Rapp Coins %").setValue(mCoinscounter.getText().toString());


        }else{

            // Write a message to the database
            DatabaseReference myRef = mDatabase.getReference();
            myRef.child("Utenti").child(mPhone.getText().toString()).child("Rapp Coins %").setValue(mCoinscounter.getText().toString());
        }

        super.onDestroy();
    }

    private boolean isEmpty(TextView mCoins) {
        String input = mCoins.getText().toString();
        return input.length() == 0.00;
    }

}

我真的需要有关此问题的解决方案的帮助,因为这是我需要完成的最后一件事,我不能感谢你。

{
      "Utenti" : {
        "3********4" : {
          "E-Mail" : "",
          "Full Name" : "",
          "Phone" : "",
          "Rapp Coins %" : "",
          "Rapp Coins Day %" : ""
        },
        "3********1" : {
          "E-Mail" : "",
          "Full Name" : "",
          "Phone" : "",
          "Rapp Coins %" : "",
          "Rapp Coins Day %" : ""
        },
       }
    }

这是我组成的数据库。

0 个答案:

没有答案