【发布时间】:2017-05-25 08:53:22
【问题描述】:
我创建了一个应用程序,用户可以通过该应用程序注册和登录该应用程序。现在我正在考虑在我的应用程序中增加价值。 Demo
看看我想在我的个人资料活动中添加的图片。当用户单击值 1 时,此数据将保存在数据库中。并且该数据将字符串并替换 0 到 1 。之后,如果任何用户单击 5 按钮。 +5 将添加到数据库中,它将替换 1 到 6 。我想为每个用户创建它。
我的档案活动
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));
}
}
}
// 请帮我 。谢谢大家
【问题讨论】:
-
您能否更具体地说明您要实现的目标以及我们谈论的是哪种数据存储?
-
我想放一些按钮。按钮值为 5 和 1 。当任何用户按下按钮 5 时,它将保存在数据库中,之后如果同一用户按下 1 thn +1 将保存在数据库中,总数将为 6 。检查我的 ss。
标签: java firebase firebase-realtime-database firebase-authentication