i want the Activity must show Progress Dialog while fetching data from Firebase database.it is not showing anything and gets crashed. here is my code below
public class profilemain extends AppCompatActivity {
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private ProgressDialog progressDialog;
DatabaseReference mref =
FirebaseDatabase.getInstance().getReference("users");
ListView mlistview;
ArrayList<String> arrayList=new ArrayList<>();
ArrayAdapter<String> arrayAdapter;
private BottomNavigationView.OnNavigationItemSelectedListener
mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
progressDialog.show();
startActivity(new
Intent(profilemain.this,profilemain.class));
return true;
case R.id.navigation_dashboard:
Toast.makeText(profilemain.this,"hi
hello",Toast.LENGTH_SHORT).show();
return true;
case R.id.navigation_notifications:
Toast.makeText(profilemain.this,"hi
hello",Toast.LENGTH_SHORT).show();
return true;
case R.id.navigation_signout:
Toast.makeText(profilemain.this,"You have successfully
Signed out",Toast.LENGTH_SHORT).show();
mAuth.signOut();
return true;
}
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profilemain);
progressDialog.setMessage("loading");
progressDialog.setTitle("database is");
mlistview=(ListView) findViewById(R.id.listview);
Firebase.setAndroidContext(this);
DatabaseReference ref = FirebaseDatabase.getInstance()
.getReference("users");
//mref=new Firebase("https://stark-1dffd.firebaseio.com/users");
arrayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayList);
mlistview.setAdapter(arrayAdapter);
mref.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String value= dataSnapshot.getValue(String.class);
arrayList.add(value);
arrayAdapter.notifyDataSetChanged();
//
//
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
if (firebaseAuth.getCurrentUser()==null){
startActivity(new
Intent(profilemain.this,MainActivity.class));
}
}
};
}
#Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
Process: com.food.sheenishere.stark, PID: 16408
com.google.firebase.database.DatabaseException: Failed to convert value of type
java.util.HashMap to String
at com.google.android.gms.internal.zg.zzb(Unknown Source)
at com.google.android.gms.internal.zg.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at com.food.sheenishere.stark.home$1.onChildAdded(home.java:49)
at com.google.android.gms.internal.px.zza(Unknown Source)
at com.google.android.gms.internal.vj.zzHX(Unknown Source)
at com.google.android.gms.internal.vp.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
07-15 20:49:51.645 16408-16435/com.food.sheenishere.stark W/DynamiteModule:
Local module descriptor class for com.google.firebase.auth not found.
The problem in your code is that your are trying to use show(), setMessage("loading") and setTitle("database is") methods on a ProgressDialog object that have never been initialized.
In order to solve your problem you need to use the code below:
ProgressDialog progressDialog = new ProgressDialog(this);
It will solve your problem for sure.
Your should initialize progress dialog.. bellow like
private ProgressDialog pDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
pDialog = new ProgressDialog(profilemain.this);
pDialog.setMessage("loading..");
}
Related
I am developing a blogging app for android and I am using firebase for the backend. The app is working fine but the when I scroll down to the last post, the app crashes. I have working on finding the solution from a quite a long but couldn't possibly find any.
Here is the activity which is facing the problem
public class UserActivity extends AppCompatActivity {
private RecyclerView mStatusView;
private DatabaseReference mDatabase;
private DatabaseReference mDatabaseUsers;
private DatabaseReference mDatabaseLike;
private FirebaseAuth mFirebaseAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
String userID = "";
private Boolean exit = false;
private Boolean mProcessLike = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_main);
mFirebaseAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
if (firebaseAuth.getCurrentUser() == null) {
Intent loginIntent = new Intent(UserActivity.this, LoginActivity.class);
loginIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(loginIntent);
}
}
};
mDatabase = FirebaseDatabase.getInstance().getReference().child("Status");
mDatabaseUsers = FirebaseDatabase.getInstance().getReference().child("Users");
mDatabaseLike = FirebaseDatabase.getInstance().getReference().child("Likes");
mDatabaseUsers.keepSynced(true);
mDatabaseLike.keepSynced(true);
mDatabase.keepSynced(true);
mStatusView = findViewById(R.id.status_list);
mStatusView.setHasFixedSize(true);
mStatusView.setLayoutManager(new LinearLayoutManager(this));
}
#Override
protected void onStart() {
super.onStart();
checkUserExists();
mFirebaseAuth.addAuthStateListener(mAuthListener);
// Creating a Firebase recycle Adapter
FirebaseRecyclerAdapter<Status, StatusViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<Status, StatusViewHolder>(
Status.class,
R.layout.status_row,
StatusViewHolder.class,
mDatabase
) {
#Override
protected void populateViewHolder(StatusViewHolder viewHolder, Status model, int position) {
final String postKey = getRef(position).getKey();
viewHolder.setTitle(model.getTitle());
viewHolder.setDescription((model.getDescription()));
viewHolder.setImage(getApplicationContext(), model.getImage());
viewHolder.mNumberOfLikesTextView.setVisibility(View.GONE);
viewHolder.setLikeButton(postKey);
viewHolder.mLikeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mProcessLike = true;
mDatabaseLike.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (mProcessLike) {
if (dataSnapshot.child(postKey).hasChild(mFirebaseAuth.getCurrentUser().getUid())) {
mDatabaseLike.child(postKey).child(mFirebaseAuth.getCurrentUser().getUid()).removeValue();
mProcessLike = false;
Toast.makeText(UserActivity.this, "Like removed", Toast.LENGTH_SHORT).show();
} else {
mDatabaseLike.child(postKey).child(mFirebaseAuth.getCurrentUser().getUid()).setValue("RandomValue");
mProcessLike = false;
Toast.makeText(UserActivity.this, "Liked", Toast.LENGTH_SHORT).show();
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
}
};
mStatusView.setAdapter(firebaseRecyclerAdapter);
}
private void checkUserExists() {
try {
userID = mFirebaseAuth.getCurrentUser().getUid();
} catch (NullPointerException e) {
e.printStackTrace();
}
mDatabaseUsers.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (!dataSnapshot.hasChild(userID)) {
Intent registerIntent = new Intent(UserActivity.this,
RegisterActivity.class);
registerIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(registerIntent);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
// Setting up Recycler View
public static class StatusViewHolder extends RecyclerView.ViewHolder {
View mView;
ImageButton mLikeButton;
TextView mNumberOfLikesTextView;
DatabaseReference mDataBaseLike;
FirebaseAuth mAuth;
public StatusViewHolder(View itemView) {
super(itemView);
mView = itemView;
mLikeButton = itemView.findViewById(R.id.like_button);
mNumberOfLikesTextView = itemView.findViewById(R.id.number_of_likes_text_view);
mDataBaseLike = FirebaseDatabase.getInstance().getReference().child("Likes");
mAuth = FirebaseAuth.getInstance();
mDataBaseLike.keepSynced(true);
}
public void setLikeButton(final String postKey) {
mDataBaseLike.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.child(postKey).hasChild(mAuth.getCurrentUser().getUid())) {
mLikeButton.setImageResource(R.drawable.ic_thumb_up_blue_24dp);
} else {
mLikeButton.setImageResource(R.drawable.ic_thumb_up_black_24dp);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
// Setting the title
public void setTitle(String title) {
TextView statusTitle = mView.findViewById(R.id.status_title_textView);
statusTitle.setText(title);
}
public void setDescription(String desc) {
TextView statusDescription = mView.findViewById(R.id.status_description_textView);
statusDescription.setText(desc);
}
public void setImage(Context context, String image) {
ImageView statusImage = mView.findViewById(R.id.status_image);
Picasso.with(context).load(image).into(statusImage);
}
}
}
And here is the log for the problem I'm getting
FATAL EXCEPTION: main
Process: com.example.akash.iametderick, PID: 12384
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.Long to type com.example.akash.iametderick.Status
at com.google.android.gms.internal.zzdzr.zzb(Unknown Source:842)
at com.google.android.gms.internal.zzdzr.zza(Unknown Source:0)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source:10)
at com.firebase.ui.database.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:22)
at com.firebase.ui.database.ObservableSnapshotArray.getObject(ObservableSnapshotArray.java:160)
at com.firebase.ui.database.CachingObservableSnapshotArray.getObject(CachingObservableSnapshotArray.java:40)
at com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:180)
at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:217)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6482)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6515)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5458)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5724)
at android.support.v7.widget.GapWorker.prefetchPositionWithDeadline(GapWorker.java:285)
at android.support.v7.widget.GapWorker.flushTaskWithDeadline(GapWorker.java:342)
at android.support.v7.widget.GapWorker.flushTasksWithDeadline(GapWorker.java:358)
at android.support.v7.widget.GapWorker.prefetch(GapWorker.java:365)
at android.support.v7.widget.GapWorker.run(GapWorker.java:396)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
I've been trying to solve the problem from quite a long but couldn't find any solution.
This is my status node:
Status {
-L10rFvy6N-t7RiF7FJ6 {
date: ...;
description: ...;
image: ...;
title: ...''
}
date: ...;
description: ...;
image: ...;
title: ...''
}
The issue is with your data saved on firebase.
Your Node Status is a Object and you may have saved its value as long for that particular node in your database.
So check you database and compare it with other node . Specially the node which is having the problem will be in last as you said when I scroll down to the last pos so check your last node
I am using firebase database where I am saving data manually and retrieving data in the app and that is working well but I want that when user clicks the listview item that must open another activity and within that must retrieve another table.
Here is below my code
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private ProgressDialog progressDialog;
DatabaseReference mref =
FirebaseDatabase.getInstance().getReference("users");
ListView mlistview;
ArrayList<String> arrayList=new ArrayList<>();
ArrayAdapter<String> arrayAdapter;
private BottomNavigationView.OnNavigationItemSelectedListener
mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
progressDialog.show();
startActivity(new
Intent(profilemain.this,profilemain.class));
return true;
case R.id.navigation_dashboard:
Toast.makeText(profilemain.this,"hi
hello",Toast.LENGTH_SHORT).show();
return true;
case R.id.navigation_notifications:
Toast.makeText(profilemain.this,"hi
hello",Toast.LENGTH_SHORT).show();
return true;
case R.id.navigation_signout:
Toast.makeText(profilemain.this,"You have successfully
Signed out",Toast.LENGTH_SHORT).show();
mAuth.signOut();
return true;
}
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profilemain);
progressDialog.setMessage("loading");
progressDialog.setTitle("database is");
mlistview=(ListView) findViewById(R.id.listview);
Firebase.setAndroidContext(this);
DatabaseReference ref = FirebaseDatabase.getInstance()
.getReference("users");
//mref=new Firebase("https://stark-1dffd.firebaseio.com/users");
arrayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayList);
mlistview.setAdapter(arrayAdapter);
mref.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String value= dataSnapshot.getValue(String.class);
arrayList.add(value);
arrayAdapter.notifyDataSetChanged();
//
//
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
if (firebaseAuth.getCurrentUser()==null){
startActivity(new
Intent(profilemain.this,MainActivity.class));
}
}
};
}
#Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
add an ItemClickListener on your ListView
mlistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent in = new Intent(MyActivity.this, DetailActivity.class);
in.putExtra("data", arrayList.get(position);// add the selected String from the ListView
startActivity(in);
}
});
and read the data from the other activity:
String data = getIntent().getStringExtra("data");
I'm trying to JUnit test this class:
public class WeekListActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
private ArrayList<String> weekList = new ArrayList<>();
private ArrayAdapter<String> adapter;
ListView weekListView;
Button AddWeekButton;
EditText InsertWeekEditText;
String weekNumber;
String subjectName;
String subjectCode;
User user;
DatabaseReference mDatabase;
DatabaseReference mRef;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_week_list);
FirebaseApp.initializeApp(this);
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
Intent moveToDetailIntent = this.getIntent();
subjectName = moveToDetailIntent.getExtras().getString("Subject");
subjectCode = moveToDetailIntent.getExtras().getString("Fagkode");
mDatabase = FirebaseDatabase.getInstance().getReference().child("Studentfag").child(subjectCode).child("Week");
mRef = FirebaseDatabase.getInstance().getReference().child("Users").child(firebaseUser.getUid()).child("User info");
weekListView = (ListView) findViewById(R.id.WeekListView);
AddWeekButton = (Button) findViewById(R.id.AddWeekButton);
InsertWeekEditText = (EditText) findViewById(R.id.InsertWeek);
String userID = firebaseUser.getUid();
mRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
user = dataSnapshot.getValue(User.class);
if (user.isStudent){
View a = weekListView;
a.setMinimumHeight(80);
View b = AddWeekButton;
b.setVisibility(View.GONE);
View c = InsertWeekEditText;
c.setVisibility(View.GONE);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
adapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1, weekList);
weekListView.setAdapter(adapter);
weekListView.setOnItemClickListener(this);
AddWeekButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
weekNumber= InsertWeekEditText.getText().toString();
mDatabase.child(weekNumber).child("id").setValue(weekNumber);
}
});
mDatabase.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String week = dataSnapshot.getKey().toString();
weekList.add("Week: " + week);
adapter.notifyDataSetChanged();
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
//Urelevante metoder for oss.
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {}
#Override
public void onCancelled(DatabaseError databaseError) {}
});
}
The problem is that when I build a new activity in my setup method it complains because this sentence:
mRef = FirebaseDatabase.getInstance().getReference().child("Users").child(firebaseUser.getUid()).child("User info");
is not able to build, when we don't have a firebase user.
Therefore I tried to Mock a firebaseuser in my testclass. The question is, how can I tell the class that it should use the mocked firebaseuser in onCreate? Is there a way to "send" the mocked object over? Thanks!
The beginning of my setup method:
#Before
public void setUp() throws Exception {
Intent i = new Intent();
i.putExtra("Subject", "Matematikk 1");
i.putExtra("Fagkode", "TMA4100");
FirebaseUser mockFirebaseUser = mock(FirebaseUser.class);
when(mockFirebaseUser.getUid()).thenReturn("uTZpVPPz8NT2LOvP4ufjs1L6r3P2");
Activity activity = Robolectric.buildActivity(WeekListActivity.class).withIntent(i).create().get();
}
As usual, I suggest everybody to not mix presentation and storage code. And this is a question for another topic.
And here the trick how you can achieve what you want.
First, extract method for Firebase initialisation and providing FirebaseAuth:
#VisibleForTest
#NonNull
FirebaseAuth initAndReturnFirebaseAuth() {
FirebaseApp.initializeApp(this);
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
}
Second, create test activity and override this method:
public class TestWeekListActivity extends WeekListActivity {
#Override
#NonNull
FirebaseAuth initAndReturnFirebaseAuth() {
FirebaseAuth authMock = mock(FirebaseAuth.class);
when(authMock.getCurrentUser()).thenReturn(mockFirebaseUser);
return authMock;
}
}
And then use test activity in test instead of you real activity.
Hope it helps!
I'm using Firebase to populate my RecyclerView, but the problem is that every time I open the app, I need to swipe down to refresh and then the list of items is available.
Here is my code
public class MainActivity extends AppCompatActivity {
private static final int RC_PHOTO_PICKER = 2;
DatabaseReference db;
FirebaseHelper helper;
MyAdapter adapter;
RecyclerView rv;
EditText nameEditTxt,grpTxt,descTxt,linkTxt;
FirebaseAuth.AuthStateListener authListener;
SwipeRefreshLayout swipeRefresh;
Uri downloadUrl;
String Admin_code;
FirebaseStorage mfirebaseStorage;
private StorageReference mEventPhotoReference;
FloatingActionButton fab;
SharedPreferences sharedPreferences;
ProgressBar spinner;
static boolean calledAlready=false;
public MyAdapter adapter1;
#RequiresApi(api = Build.VERSION_CODES.KITKAT)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
spinner = (ProgressBar)findViewById(R.id.progressBar);
spinner.setVisibility(View.GONE);
mfirebaseStorage=FirebaseStorage.getInstance();
if(!calledAlready){
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
calledAlready=true;
}
swipeRefresh=(SwipeRefreshLayout)findViewById(R.id.swiperefresh);
//SETUP RECYCLER
rv = (RecyclerView) findViewById(R.id.rv);
rv.setLayoutManager(new LinearLayoutManager(this));
//INITIALIZE FIREBASE DB
db= FirebaseDatabase.getInstance().getReference();
mEventPhotoReference=mfirebaseStorage.getReference().child("Event Photos");
helper=new FirebaseHelper(db);
//ADAPTER
adapter=new MyAdapter(this,helper.retrieve());
rv.setAdapter(adapter);
adapter.notifyDataSetChanged();
swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
rv.setAdapter(adapter);
swipeRefresh.setRefreshing(false);
}
});
sharedPreferences= PreferenceManager.getDefaultSharedPreferences(this);
Admin_code=sharedPreferences.getString(getString(R.string.Admin_code),getString(R.string.Admin_default_value));
Log.e("MainActivity","" + Admin_code);
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
displayInputDialog();
}
});
fab.setVisibility(View.GONE);
showBtn();
}
#RequiresApi(api = Build.VERSION_CODES.KITKAT)
#Override
protected void onResume() {
showBtn();
super.onResume();
}
#RequiresApi(api = Build.VERSION_CODES.KITKAT)
public void showBtn(){
Admin_code=sharedPreferences.getString(getString(R.string.Admin_code),getString(R.string.Admin_default_value));
if(Objects.equals(Admin_code, "28011996")){
fab.setVisibility(View.VISIBLE);
}
else
fab.setVisibility(View.GONE);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_logout) {
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(MainActivity.this, LoginActivity.class));
return true;
}
if(id==R.id.settings){
Intent settingsIntent=new Intent(this,Settings.class);
startActivity(settingsIntent);
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if ((requestCode& 0xffff) == RC_PHOTO_PICKER && resultCode == RESULT_OK) {
Uri selectedImageUri = data.getData();
StorageReference photoRef=mEventPhotoReference.child(selectedImageUri.getLastPathSegment());
photoRef.putFile(selectedImageUri)
.addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// When the image has successfully uploaded, we get its download URL
downloadUrl = taskSnapshot.getDownloadUrl();
Toast.makeText(MainActivity.this,"Photo selected successfully",Toast.LENGTH_LONG).show();
}
}).addOnFailureListener(this, new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(MainActivity.this,"there was a problem uploading photo",Toast.LENGTH_LONG).show();
}
});
}
}
//DISPLAY INPUT DIALOG
private void displayInputDialog()
{
Dialog d=new Dialog(this);
d.setTitle("Save To Firebase");
d.setContentView(R.layout.input_dialog);
nameEditTxt= (EditText) d.findViewById(R.id.nameEditText);
grpTxt= (EditText) d.findViewById(R.id.propellantEditText);
descTxt= (EditText) d.findViewById(R.id.descEditText);
Button saveBtn= (Button) d.findViewById(R.id.saveBtn);
Button photoBtn=(Button)d.findViewById(R.id.photoBtn);
linkTxt = (EditText) d.findViewById(R.id.linkEditText);
photoBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent=new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
startActivityForResult(Intent.createChooser(intent, "Complete action using"), RC_PHOTO_PICKER);
}
});
//SAVE
saveBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//GET DATA
String name=nameEditTxt.getText().toString();
String propellant=grpTxt.getText().toString();
String desc=descTxt.getText().toString();
String link=linkTxt.getText().toString();
Long tsLong = System.currentTimeMillis()/1000;
String ts = tsLong.toString();
//SET DATA
Spacecraft s=new Spacecraft();
s.setName(name);
s.setPropellant(propellant);
s.setDescription(desc);
s.setLink(link);
s.setImageUrl(downloadUrl.toString());
s.setTimestamp(ts);
//SIMPLE VALIDATION
if(name != null && name.length()>0)
{
//THEN SAVE
if(helper.save(s))
{
//IF SAVED CLEAR EDITXT
nameEditTxt.setText("");
grpTxt.setText("");
descTxt.setText("");
linkTxt.setText("");
downloadUrl=null;
adapter=new MyAdapter(MainActivity.this,helper.retrieve());
rv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}else
{
Toast.makeText(MainActivity.this, "Name Must Not Be Empty", Toast.LENGTH_SHORT).show();
}
}
});
d.show();
}
After Searching for a while, I found out that I should use notifyDataSetChanged(); but it doesn't work.
Here is my helper class where I'm fetching data:
public class FirebaseHelper {
DatabaseReference db;
Boolean saved=null;
ArrayList<Spacecraft> spacecrafts=new ArrayList<>();
public FirebaseHelper(DatabaseReference db) {
this.db = db;
}
//WRITE IF NOT NULL
public Boolean save(Spacecraft spacecraft)
{
if(spacecraft==null)
{
saved=false;
}else
{
try
{
db.child("Spacecraft").push().setValue(spacecraft);
saved=true;
}catch (DatabaseException e)
{
e.printStackTrace();
saved=false;
}
}
return saved;
}
//IMPLEMENT FETCH DATA AND FILL ARRAYLIST
private void fetchData(DataSnapshot dataSnapshot)
{
spacecrafts.clear();
for (DataSnapshot ds : dataSnapshot.getChildren())
{
Spacecraft spacecraft=ds.getValue(Spacecraft.class);
spacecrafts.add(spacecraft);
}
}
//READ THEN RETURN ARRAYLIST
public ArrayList<Spacecraft> retrieve() {
db.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot);
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
fetchData(dataSnapshot);
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
return spacecrafts;
}
It'll be great if someone could help me out here. This is the last problem in my project.
The problem is due to the adapter not knowing about the data that has been fetched asynchronously. Let's try to look at your code step-by-step:
adapter = new MyAdapter(this, helper.retrieve());
helper.retrieve() will initiate an asynchronous request to fetch data from firebase and immediately returns an empty ArrayList (as the data from firebase hasn't reached the app yet).
rv.setAdapter(adapter);
This line sets the adapter to recyclerView, which has an empty arraylist as data, so it won't show anything on the UI.
After the data is received from firebase, ArrayList<Spacecraft> spacecrafts is updated with new data. But the adapter is not notified about this new data. Ideally, adapter.notifyDataSetChanged() should be called after new data is put into the array list.
Hence, when you swipe to refresh after the initial call, the adapter is set to recycler view, leading to an internal call to notifyDataSetChanged(), which in turn loads the new data on to the UI.
Easiest solution is to notify the adapter when new data is received from Firebase. Below is the pseudo code for it -
Create an interface OnFirebaseDataChanged and add a method void dataChanged(); to it.
MainAcitivty should implement this interface and the implementation of that method should call adapter.notifyDataSetChanged()
helper = new FirebaseHelper(db); should be changed to helper = new FirebaseHelper(db, this);
FirebaseHelper's constructor should have a 2nd parameter of type OnFirebaseDataChanged and should save it in a field called dataChangeListener.
Add a line at the end of fetchData method in FirebaseHelper dataChangeListener.dataChanged();
The ideal solution would be to architect the code differently, but that topic is out of scope of this question.
I'm getting error on getUid()
final String user_id = mAuth.getCurrentUser().getUid();
and this my activity code :
public class MainActivity extends AppCompatActivity {
private RecyclerView postList;
private DatabaseReference mDatabase;
private DatabaseReference mDatabaseUsers;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
if (firebaseAuth.getCurrentUser() == null){
Intent loginIntent = new Intent(MainActivity.this, LoginActivity.class);
loginIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(loginIntent);
}
}
};
mDatabase = FirebaseDatabase.getInstance().getReference().child("Blog");
mDatabaseUsers = FirebaseDatabase.getInstance().getReference().child("Users");
mDatabase.keepSynced(true);
mDatabaseUsers.keepSynced(true);
postList = (RecyclerView) findViewById(R.id.post_list);
postList.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setReverseLayout(true);
layoutManager.setStackFromEnd(true);
postList.setLayoutManager(layoutManager);
}
#Override
protected void onStart() {
super.onStart();
CheckUserExist();
mAuth.addAuthStateListener(mAuthListener);
FirebaseRecyclerAdapter<Blog, BlogViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Blog, BlogViewHolder>(
Blog.class,
R.layout.post_row,
BlogViewHolder.class,
mDatabase
) {
#Override
protected void populateViewHolder(BlogViewHolder viewHolder, Blog model, int position) {
viewHolder.setTitle(model.getTitle());
viewHolder.setDesc(model.getDescription());
viewHolder.setImage(getApplicationContext(), model.getImage());
}
};
postList.setAdapter(firebaseRecyclerAdapter);
}
private void CheckUserExist() {
final String user_id = mAuth.getCurrentUser().getUid();
mDatabaseUsers.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (!dataSnapshot.hasChild(user_id)){
Intent SetupIntent = new Intent(MainActivity.this, SetupActivity.class);
SetupIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(SetupIntent);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
public static class BlogViewHolder extends RecyclerView.ViewHolder{
View mView;
public BlogViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setTitle(String title){
TextView post_title = (TextView) mView.findViewById(R.id.post_title);
post_title.setText(title);
}
public void setDesc(String desc){
TextView post_desc = (TextView) mView.findViewById(R.id.post_desc);
post_desc.setText(desc);
}
public void setImage(final Context ctx, final String image){
final ImageView post_image = (ImageView) mView.findViewById(R.id.post_img);
// Picasso.with(ctx).load(image).into(post_image);
Picasso.with(ctx).load(image).networkPolicy(NetworkPolicy.OFFLINE).into(post_image, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
Picasso.with(ctx).load(image).into(post_image);
}
});
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_add){
startActivity(new Intent(MainActivity.this, PostActivity.class));
}
if (item.getItemId() == R.id.action_logout){
logout();
}
return super.onOptionsItemSelected(item);
}
private void logout() {
mAuth.signOut();
}
}
The problem happens when you try to call mAuth.getCurrentUser().getUid(). Apparently there is no current user at the time, so that call fails.
Instead use:
private void CheckUserExist() {
if (mAuth.getCurrentUser() != null) {
final String user_id = mAuth.getCurrentUser().getUid();
mDatabaseUsers.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (!dataSnapshot.hasChild(user_id)){
Intent SetupIntent = new Intent(MainActivity.this, SetupActivity.class);
SetupIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(SetupIntent);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
In general, you'll want to use an auth state listener for this, similar to the one you already have in onCreate.
In fact, it seems to me that both could easily be folded into a single listener: one that starts the login activity if the user isn't signed in and optionally registers the user with the database if they are.
The code for that:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDatabase = FirebaseDatabase.getInstance().getReference().child("Blog");
mDatabaseUsers = FirebaseDatabase.getInstance().getReference().child("Users");
mDatabase.keepSynced(true);
mDatabaseUsers.keepSynced(true);
postList = (RecyclerView) findViewById(R.id.post_list);
postList.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setReverseLayout(true);
layoutManager.setStackFromEnd(true);
postList.setLayoutManager(layoutManager);
}
#Override
protected void onStart() {
super.onStart();
CheckUserExist();
FirebaseAuth.getInstance().addAuthStateListener(new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user == null){
// The user is not signed in, start login flow
Intent loginIntent = new Intent(MainActivity.this, LoginActivity.class);
loginIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(loginIntent);
}
else {
// The user is signed in, register them in database
String user_id = user.getUid();
mDatabaseUsers.child(user_id).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (!dataSnapshot.exists()){
Intent SetupIntent = new Intent(MainActivity.this, SetupActivity.class);
SetupIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(SetupIntent);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore onCancelled
}
});
}
}
});
FirebaseRecyclerAdapter<Blog, BlogViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Blog, BlogViewHolder>(
Blog.class,
R.layout.post_row,
BlogViewHolder.class,
mDatabase
) {
#Override
protected void populateViewHolder(BlogViewHolder viewHolder, Blog model, int position) {
viewHolder.setTitle(model.getTitle());
viewHolder.setDesc(model.getDescription());
viewHolder.setImage(getApplicationContext(), model.getImage());
}
};
postList.setAdapter(firebaseRecyclerAdapter);
}
I also fixed the way you check if the user already exists in the database. Retrieving all users to then client-side check if one of the keys exists is a waste of bandwidth that will become especially evident as your number of users grows. The code I wrote checks precisely whether this user's UID already exists in the database, which is the minimum data you can check.
It is because you are not initializing mauth.
remove onstart method and copy the code to oncreate. oncreate will be
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
if (firebaseAuth.getCurrentUser() == null){
Intent loginIntent = new Intent(MainActivity.this, LoginActivity.class);
loginIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(loginIntent);
}
}
};
mDatabase = FirebaseDatabase.getInstance().getReference().child("Blog");
mDatabaseUsers = FirebaseDatabase.getInstance().getReference().child("Users");
mDatabase.keepSynced(true);
mDatabaseUsers.keepSynced(true);
postList = (RecyclerView) findViewById(R.id.post_list);
postList.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setReverseLayout(true);
layoutManager.setStackFromEnd(true);
postList.setLayoutManager(layoutManager);
CheckUserExist();
mAuth.addAuthStateListener(mAuthListener);
FirebaseRecyclerAdapter<Blog, BlogViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Blog, BlogViewHolder>(
Blog.class,
R.layout.post_row,
BlogViewHolder.class,
mDatabase
) {
#Override
protected void populateViewHolder(BlogViewHolder viewHolder, Blog model, int position) {
viewHolder.setTitle(model.getTitle());
viewHolder.setDesc(model.getDescription());
viewHolder.setImage(getApplicationContext(), model.getImage());
}
};
postList.setAdapter(firebaseRecyclerAdapter);
}