我正在尝试使用Firebase为Android中的血库制作一个Android应用程序。我的主页显示了一个城市捐赠者名单,联系号码。但问题是当用户登录然后返回主页时他/她可以看到该应用程序停止的donner列表。
为了更好地理解,我在这里给出了我的全班代码。谁能在这个过程中帮助我?提前谢谢。
package alik.com.pmscs;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.firebase.client.Firebase;
import com.firebase.ui.database.FirebaseListAdapter;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.util.ArrayList;
import alik.com.loader.Information;
public class HomePage extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
Intent intent;
FirebaseAuth auth;
FirebaseAuth.AuthStateListener authStateListener;
ListView listView;
ArrayList <Information> informations;
Firebase firebase;
ImageView call;
//FirebaseUser user;
FirebaseAuth.AuthStateListener listener;
GoogleSignInOptions signInOptions;
DatabaseReference reference;
NavigationView navigationView;
com.firebase.ui.database.FirebaseListAdapter <Information> adapter;
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
auth=FirebaseAuth.getInstance();
listener=new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(FirebaseAuth firebaseAuth) {
FirebaseUser user=auth.getCurrentUser();
if(user==null){
Menu menu=navigationView.getMenu();
menu.findItem(R.id.nav_signoutfrom).setVisible(false);
menu.findItem(R.id.nav_profileon).setVisible(false);
menu.findItem(R.id.nav_adddonnor).setVisible(false);
}
}
};
listView=(ListView)findViewById(R.id.listView);
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
final FirebaseDatabase database=FirebaseDatabase.getInstance();
reference=database.getReference();
reference.keepSynced(true);
adapter=new FirebaseListAdapter<Information>(
this,
Information.class,
R.layout.donorlist,
reference
) {
@Override
protected void populateView(View v, Information model, int position) {
((TextView)v.findViewById(R.id.donnor_blood)).setText(model.getBloodgroup());
((TextView)v.findViewById(R.id.donor_name)).setText(model.getName());
((TextView)v.findViewById(R.id.donor_contact)).setText(model.getContact());
((TextView)v.findViewById(R.id.donor_city)).setText(model.getCity()+", "+model.getCountry());
}
};
listView.setAdapter(adapter);
ColorDrawable colorDrawable=new ColorDrawable(Color.parseColor("#bc212b"));
getSupportActionBar().setBackgroundDrawable(colorDrawable);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home_page, 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_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
//@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_reg) {
// Handle the camera action
intent=new Intent(getApplicationContext(),RegisterAccount.class);
startActivity(intent);
//finish();
} else if (id == R.id.nav_login) {
intent=new Intent(getApplicationContext(),Login.class);
startActivity(intent);
//finish();
}
else if (id == R.id.nav_profileon) {
if(listener!=null){
Toast.makeText(getApplicationContext(),"This Feature is Comming Soon!!", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(getApplicationContext(),"Please Login First", Toast.LENGTH_LONG).show();
}
}
else if(id==R.id.nav_adddonnor){
if(listener!=null){
startActivity(new Intent(getApplicationContext(),DonnarRegistration.class));
//finish();
}
else{
Toast.makeText(getApplicationContext(),"Please Login First", Toast.LENGTH_LONG).show();
}
}
else if (id == R.id.nav_signoutfrom) {
if(listener!=null){
FirebaseAuth.getInstance().signOut();
Toast.makeText(getApplicationContext(), "Sign Out Successfully", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(getApplicationContext(),"Please Login First", Toast.LENGTH_LONG).show();
}
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
protected void onStop() {
super.onStop();
if(listener!=null){
auth.removeAuthStateListener(listener);
}
}
@Override
protected void onStart() {
super.onStart();
auth.addAuthStateListener(listener);
}
@Override
protected void onPostResume() {
super.onPostResume();
}
}
我的登录类代码在这里
package alik.com.pmscs;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
public class Login extends AppCompatActivity {
private EditText inputEmail, inputPassword;
private FirebaseAuth auth;
private ProgressBar progressBar;
private Button btnSignup, btnLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ColorDrawable colorDrawable=new ColorDrawable(Color.parseColor("#bc212b"));
getSupportActionBar().setBackgroundDrawable(colorDrawable);
getSupportActionBar().setTitle("Sign Up");
auth = FirebaseAuth.getInstance();
inputEmail = (EditText) findViewById(R.id.email);
inputPassword = (EditText) findViewById(R.id.password);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
btnSignup = (Button) findViewById(R.id.btn_signup);
btnLogin = (Button) findViewById(R.id.btn_login);
auth=FirebaseAuth.getInstance();
btnSignup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(getApplicationContext(), RegisterAccount.class);
startActivity(intent);
finish();
}
});
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = inputEmail.getText().toString();
final String password = inputPassword.getText().toString();
if (TextUtils.isEmpty(email)) {
Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
return;
}
progressBar.setVisibility(View.VISIBLE);
auth.signInWithEmailAndPassword(email, password).addOnCompleteListener(Login.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(Task<AuthResult> task) {
progressBar.setVisibility(View.GONE);
if (!task.isSuccessful()) {
// there was an error
Toast.makeText(getApplicationContext(),"There is an error",Toast.LENGTH_LONG).show();
if (password.length() < 6) {
inputPassword.setError(getString(R.string.minimum_password));
} else {
Toast.makeText(Login.this, getString(R.string.auth_failed), Toast.LENGTH_LONG).show();
}
} else {
Intent intent = new Intent(Login.this, HomePage.class);
startActivity(intent);
finish();
}
}
});
}
});
}
}
所以问题是当我登录然后尝试回到我的主页活动时它停止了。所以我需要专家的帮助来解决它。如果你帮我完成这项任务,那将是一种乐趣。
我的gradle文件:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
compile 'com.firebase:firebase-client-android:2.4.0'
compile 'com.google.firebase:firebase-auth:10.0.1'
compile 'com.google.firebase:firebase-core:10.0.1'
compile 'com.google.firebase:firebase-database:10.0.1'
compile 'com.google.firebase:firebase-storage:10.0.1'
compile 'com.firebaseui:firebase-ui-database:0.4.0'
}
apply plugin: 'com.google.gms.google-services'
答案 0 :(得分:0)
您是否尝试按以下方式更改登录活动?在这里,我正在检查用户是否已登录Login活动,如果是,我正在调用HomePage活动。
public class Login extends AppCompatActivity {
private EditText inputEmail, inputPassword;
private FirebaseAuth auth;
private ProgressBar progressBar;
private Button btnSignup, btnLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
auth = FirebaseAuth.getInstance();
if (auth.getCurrentUser() != null) {
Intent intent = new Intent(Login.this, HomePage.class);
startActivity(intent);
finish();
}
setContentView(R.layout.activity_login);
ColorDrawable colorDrawable=new ColorDrawable(Color.parseColor("#bc212b"));
getSupportActionBar().setBackgroundDrawable(colorDrawable);
getSupportActionBar().setTitle("Sign Up");
inputEmail = (EditText) findViewById(R.id.email);
inputPassword = (EditText) findViewById(R.id.password);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
btnSignup = (Button) findViewById(R.id.btn_signup);
btnLogin = (Button) findViewById(R.id.btn_login);
btnSignup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(getApplicationContext(), RegisterAccount.class);
startActivity(intent);
finish();
}
});
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = inputEmail.getText().toString();
final String password = inputPassword.getText().toString();
if (TextUtils.isEmpty(email)) {
Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
return;
}
progressBar.setVisibility(View.VISIBLE);
auth.signInWithEmailAndPassword(email, password).addOnCompleteListener(Login.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(Task<AuthResult> task) {
progressBar.setVisibility(View.GONE);
if (!task.isSuccessful()) {
// there was an error
Toast.makeText(getApplicationContext(),"There is an error",Toast.LENGTH_LONG).show();
if (password.length() < 6) {
inputPassword.setError(getString(R.string.minimum_password));
} else {
Toast.makeText(Login.this, getString(R.string.auth_failed), Toast.LENGTH_LONG).show();
}
} else {
Intent intent = new Intent(Login.this, HomePage.class);
startActivity(intent);
finish();
}
}
});
}
});
}
希望这可以满足您的要求