android值添加应用

时间:2017-05-25 08:53:23

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

我创建了一个应用程序,用户可以在该应用程序中注册和登录。现在我想在我的应用程序中增加价值。 Demo

查看我想在我的个人资料活动中添加此内容的图片。当用户点击值1时,该数据将保存在数据库中。并且该数据将字符串替换为0到1。之后如果有任何用户点击5按钮。 +5将添加到数据库中,它将替换1到6。我想为每个用户创建它。

我的Propile活动

    package com.helploger.www.ezzeearn;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

public class ProfileActivity extends AppCompatActivity implements View.OnClickListener {
    DrawerLayout drawerLayout;
    Toolbar toolbar;
    ActionBarDrawerToggle actionBarDrawerToggle;

    //firebase auth object
    private FirebaseAuth firebaseAuth;

    //view objects
    private TextView textViewUserEmail;
    private Button buttonLogout;


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

        //Toolbar

        toolbar =(Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        drawerLayout =(DrawerLayout) findViewById(R.id.drawer_layout);

        actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close);

        drawerLayout.setDrawerListener(actionBarDrawerToggle);



        //initializing firebase authentication object
        firebaseAuth = FirebaseAuth.getInstance();

        //if the user is not logged in
        //that means current user will return null
        if(firebaseAuth.getCurrentUser() == null){
            //closing this activity
            finish();
            //starting login activity
            startActivity(new Intent(this, LoginActivity.class));
        }

        //getting current user
        FirebaseUser user = firebaseAuth.getCurrentUser();

        //initializing views
        textViewUserEmail = (TextView) findViewById(R.id.textViewUserEmail);
        buttonLogout = (Button) findViewById(R.id.buttonLogout);

        //displaying logged in user name
        textViewUserEmail.setText("Welcome "+user.getEmail());

        //adding listener to button
        buttonLogout.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_logout) {
            firebaseAuth.signOut();
            finish();
            startActivity(new Intent(this, LoginActivity.class));
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        actionBarDrawerToggle.syncState();
    }

    @Override
    public void onClick(View view) {
        //if logout is pressed
        if(view == buttonLogout){
            //logging out the user
            firebaseAuth.signOut();
            //closing activity
            finish();
            //starting login activity
            startActivity(new Intent(this, LoginActivity.class));
        }
    }
}

// 请帮我 。谢谢你们

1 个答案:

答案 0 :(得分:0)

我认为这会有所帮助

Button plusOne,plusFive;
plusOne = (Button) findViewById(R.id.btnPlusOne);
plusFive = (Button) findViewById(R.id.btnPlusFive);

plusOne.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int reputation = getUserReputationFromYourDatabase(userId); //do the procedure to get user reputation from db
            reputation+=1;
            updateUserReputation(userId,reputation) //do the procedure to update user reputation on the db
        }
    });


plusFive.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int reputation = getUserReputationFromYourDatabase(userId); //do the procedure to get user reputation from db
            reputation+=5;
            updateUserReputation(userId,reputation) //do the procedure to update user reputation on the db
        }
    });

在onCreate()方法中编写上述方法