Can´t finish Activity - java

I have tried className.this.finish and finishActivity() to invoke the finishing of my activity and I have initialised the activity with the following methods - startActivityForResult() or finishAndRemoveTask()
The propose of this activity is to register a device this is done when under Devices/(MAC address of the device)/Users/ it save in fire base up to 4 different notification tokens of 4 users different. So what I'm trying to do but it falling is to check if the number 1 have info, check 2 if have info, check 3 and if this have info, check the 4 if all have info the app show a message saying "you have reached the maximum number of users".But if one number available it suppose to save in that number and only in that number the notification token.
What it is happening is that when I click the button the loop never end. it send the same notification token for the four users show the mensage "you have reached the maximum number of users" and then it come back to the activity before this one but it still executing the registration code. I know this because if I deleted an user in firebase it immediately resend the info and you can see the message "you have reached the maximum number of users" again
this is the code of the activity that send the info:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_cerca);
fAuth = FirebaseAuth.getInstance();
entr = findViewById(R.id.AddButton);
mDatabase = FirebaseDatabase.getInstance().getReference();
sDatabase = FirebaseDatabase.getInstance().getReference();
aDatabase = FirebaseDatabase.getInstance().getReference();
MAC =findViewById(R.id.macCerca);
NOM = findViewById(R.id.momCerca);
configured = false;
getUserProfile();
getNotificationId();
entr.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MacCerca = MAC.getText().toString().toUpperCase();
NombreCerca = NOM.getText().toString();
//mDatabase.child("Devices").child(MacCerca).child("Id").setValue("AD:23");
mDatabase.child("Devices").child(MacCerca).child("Users").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String user1 = (String) dataSnapshot.child("1").getValue();
String user2 = (String) dataSnapshot.child("2").getValue();
String user3 = (String) dataSnapshot.child("3").getValue();
String user4 = (String) dataSnapshot.child("4").getValue();
if(user1 == null){
aDatabase.child("Devices").child(MacCerca).child("Users").child("1").setValue(token);
aDatabase.child("Devices").child(MacCerca).child("NombreCerca").setValue(NombreCerca);
aDatabase.child("Users").child(mail).child("Device").setValue(MacCerca);
AddCerca.this.finish();
}else if(user2 ==null){
aDatabase.child("Devices").child(MacCerca).child("Users").child("2").setValue(token);
aDatabase.child("Devices").child(MacCerca).child("NombreCerca").setValue(NombreCerca);
aDatabase.child("Users").child(mail).child("Device").setValue(MacCerca);
AddCerca.this.finish();
}else if(user3 ==null){
aDatabase.child("Devices").child(MacCerca).child("Users").child("3").setValue(token);
aDatabase.child("Devices").child(MacCerca).child("NombreCerca").setValue(NombreCerca);
aDatabase.child("Users").child(mail).child("Device").setValue(MacCerca);
AddCerca.this.finish();
}else if(user4 ==null){
aDatabase.child("Devices").child(MacCerca).child("Users").child("4").setValue(token);
aDatabase.child("Devices").child(MacCerca).child("NombreCerca").setValue(NombreCerca);
aDatabase.child("Users").child(mail).child("Device").setValue(MacCerca);
AddCerca.this.finish();
}else{
Toast.makeText(AddCerca.this, "Límite de usuarios registrados exedidos", Toast.LENGTH_SHORT).show();
AddCerca.this.finish();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
});
}
and I create this activity like this:
startActivity(new Intent(CercaElectrica.this, AddCerca.class));
Firebase look like this:
When it suposed to be only in the user 1, or if user 1 have some data it have to update user 2 and the same with 3 and 4

I found the error. insted of:
mDatabase.child("Devices").child(MacCerca).child("Users").addValueEventListener(new ValueEventListener() {
change for:
mDatabase.child("Devices").child(MacCerca).child("Users").addListenerForSingleValueEvent(new ValueEventListener() {

Related

How to autoincrement userID in Firestore?

So I created a booking app which sends data into booking collection taking userID as Document Name.
But when the user books again it overwrites the previous booking userID document.
What I want to do is: 1) Implement autoincrement on userID, and also 2) display the autoincremented booking too
(Notice I'm using userID because I want to keep it unique to that user only).
3) I also want to implement a limit on booking if someone can help with that.
Firestore Firebase Image:
The code to send data into BOOKING COLLECTION and creating DOCUMENT with userID:
public class bookingpage extends AppCompatActivity {
EditText mFirstnamelastname,mMobnum,mPincode,mFlatno,mArea,mLandmark,mTown,mState;
Button mBook;
String userID;
FirebaseAuth fAuth;
FirebaseFirestore fstore;
ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bookingpage);
//pickup
mFirstnamelastname=findViewById(R.id.firstlastname);
mMobnum=findViewById(R.id.mobnum);
mPincode=findViewById(R.id.pincode);
mFlatno=findViewById(R.id.flatno);
mArea=findViewById(R.id.area);
mLandmark=findViewById(R.id.landmark);
mTown=findViewById(R.id.town);
mState=findViewById(R.id.state);
mBook=findViewById(R.id.editbook);
progressBar=findViewById(R.id.progressBar4);
fAuth=FirebaseAuth.getInstance();
fstore=FirebaseFirestore.getInstance();
mBook.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//pickup
String firstname = mFirstnamelastname.getText().toString().trim();
String phoneno = mMobnum.getText().toString().trim();
String pincode = mPincode.getText().toString().trim();
String flatno = mFlatno.getText().toString().trim();
String area = mArea.getText().toString().trim();
String landmark = mLandmark.getText().toString().trim();
String town = mTown.getText().toString().trim();
String state = mState.getText().toString().trim();
progressBar.setVisibility(View.VISIBLE);
//saving data
userID=fAuth.getCurrentUser().getUid();
//creating a document reference creating a collection booking and making a new doc using user id
DocumentReference documentReference = fstore.collection("Booking").document(userID);
//creating a hashmap to send data
Map<String,Object> book = new HashMap<>();
//setting status
book.put("Status -","Active");
//pickup
book.put("a1 - Fullname",firstname);
book.put("a2 - PhoneNo",phoneno);
book.put("a3 - Pincode",pincode);
book.put("a4 - Flatno",flatno);
book.put("a5 - Area",area);
book.put("a6 - Landmark",landmark);
book.put("a7 - Town",town);
book.put("a8 - State",state);
//using the document reference to set user document
documentReference.set(book).addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Toast.makeText(bookingpage.this, "Booking Successful", Toast.LENGTH_SHORT).show();
Log.d("Tag","onSuccess: Successfully booked for "+ userID);
startActivity(new Intent(getApplicationContext(),MainActivity2.class));
finish();
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(bookingpage.this, "Error!" + e.getMessage(), Toast.LENGTH_LONG).show();
}
});
progressBar.setVisibility(View.INVISIBLE);
}
});
}
}
The code to display the booking collection's document with userID:
final DocumentReference documentReference = fstore.collection("Booking").document(userID);
documentReference.addSnapshotListener(this, new EventListener<DocumentSnapshot>() {
#Override
public void onEvent(#Nullable DocumentSnapshot documentSnapshot, #Nullable FirebaseFirestoreException e) {
//putting if else fixed crashing
if (e != null) {
Log.d("Tag", "Error:" + e.getMessage());
} else {
mStatus.setText(documentSnapshot.getString("Status -"));
mFirstnamelastname.setText(documentSnapshot.getString("a1 - Fullname"));
mMobnum.setText(documentSnapshot.getString("a2 - PhoneNo"));
mPincode.setText(documentSnapshot.getString("a3 - Pincode"));
mFlatno.setText(documentSnapshot.getString("a4 - Flatno"));
mArea.setText(documentSnapshot.getString("a5 - Area"));
mLandmark.setText(documentSnapshot.getString("a6 - Landmark"));
mTown.setText(documentSnapshot.getString("a7 - Town"));
mState.setText(documentSnapshot.getString("a8 - State"));
}
}
});
I'm a beginner please try to explain your answer so that I can understand & learn more :)
Well... you just shouldn't use the UserId's as document names in the Booking collection. It's just goes against logic and best-practices.
You should instead let Firestore create a BookingId. It would be your current booking document + a new field (String) holding the UserId of the user who made the booking.
This would be a more logical (and scalable) way.
To limit the number of bookings, you could add a field in your UserId documents (in users collection), called bookingCount (Integer). Each time a User books, check if the bookingCount >= bookingLimit (arbitrary value of your choosing).
If bookingCount < bookingLimit, then allow them to book and increment the bookingCount by 1.

Firebase Database Reference Error: No such instance field: 'mDatabaseReference'

I'm using Firebase Realtime Database to store data for a project. I'm using Android Studio and Java to create a mobile app. In this particular activity, a new user is filling out a form to sign up for the service. Once they input their info and hit the submit button, the on click handles that info and inserts it into the Database.
Here is the relevant code for that class:
public class CreateUserActivity extends AppCompatActivity {
private DatabaseReference mDatabaseReference;
private List<String> mInterestList;
private EditText mUsername;
private EditText mPassword;
private EditText mEmail;
private EditText mLocation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_user);
mDatabaseReference = FirebaseDatabase.getInstance().getReference();
mInterestList = new ArrayList<>();
mUsername = findViewById(R.id.text_username);
mPassword = findViewById(R.id.text_password);
mEmail = findViewById(R.id.text_email);
mLocation = findViewById(R.id.text_location);
}
public void onCheckboxClicked(View view) {
CheckBox checkbox = (CheckBox) view;
boolean isChecked = checkbox.isChecked();
String interest = checkbox.getText().toString();
if(isChecked) {
mInterestList.add(interest);
}else {
mInterestList.remove(interest);
}
}
public void onSaveUser(View view) {
String username = mUsername.getText().toString();
String password = mPassword.getText().toString();
String email = mEmail.getText().toString();
String location = mLocation.getText().toString();
if(TextUtils.isEmpty(username) ||
TextUtils.isEmpty(password) ||
TextUtils.isEmpty(email) ||
TextUtils.isEmpty(location) ||
mInterestList.size() == 0) {
displayToast("Please enter values for all fields");
}else {
List<String> interests = mInterestList;
User user = new User(username, password, email, location, interests, null);
mDatabaseReference.child("users").child(username).setValue(user, new DatabaseReference.CompletionListener() {
#Override
public void onComplete(#Nullable DatabaseError databaseError, #NonNull DatabaseReference databaseReference) {
if(databaseError != null) {
Log.e(GetTogetherApp.LOG_TAG, databaseError.getMessage());
}else{
displayToast("User created!");
}
}
});
}
}
As I've run through breakpoints the error seems to fire on this line:
mDatabaseReference.child("users").child(username).setValue()
Specifically once I try and enter into the setValue() method. The other two child() calls work to find the path as intended. I have the database reference set as a member variable, so I'm not sure why it seems to lose it at that point.
Here's a look at my database structure
Firebase Database Structure Pic
Error in Android Studio:
The error message in your screenshot is in the variable inspector in the Android Studio debugger. It means that where the error occurs, there is no variable mDatabaseReference so the debugger can't show its value. This is not the actual cause of the crash, merely the debugger telling you that it can't show you something you asked for.
You can find the actual cause of the problem by inspecting the InvocationTargetException e.

How to retrieve a Boolean value from Firebase to a TextView?

I'm making a shopping app and User has to subscribe to the 'Fast-delivery' option if he needs to order it faster. When the User puts a tick to the 'Fast-delivery' Checkbox, the boolean value is being uploaded to the Firebase realtime database -->
database
And I want to see if the User has subscribed to the 'Fast-delivery' option from the Admin Panel. I retrieve all the order information to a RecyclerView in the Admin Panel. I want to set a TextView as "Fast Delivery : On/Off" when the Admin views the order details from the Admin Panel.
This is what I have tried:
#Override
protected void onStart()
{
super.onStart();
ordersRef = FirebaseDatabase.getInstance().getReference().child("Orders");
FirebaseRecyclerOptions<AdminOrders> options =
new FirebaseRecyclerOptions.Builder<AdminOrders>()
.setQuery(ordersRef, AdminOrders.class)
.build();
FirebaseRecyclerAdapter<AdminOrders, AdminOrdersViewHolder> adapter =
new FirebaseRecyclerAdapter<AdminOrders, AdminOrdersViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull final AdminOrdersViewHolder holder, final int position, final #NonNull AdminOrders model)
{
holder.userName.setText("Name: "+ model.getName());
holder.userPhoneNumber.setText("Phone Number: : "+ model.getPhone());
holder.userTotalPrice.setText("Total Amount: $"+ model.getTotalAmount());
holder.userDateTime.setText("Order Time: "+ model.getDate() + model.getTime());
holder.userShippingAddress.setText("Shipping Address: "+ model.getAddress() + "," + model.getCity());
holder.showOrdersBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
String uID = getRef(position).getKey();
Intent intent = new Intent(AdminNewOrdersActivity.this, AdminUserProductsActivity.class);
intent.putExtra("uid", uID);
startActivity(intent);
}
});
ordersRef.child(userID).child("fast_delivery").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
TextView switch1 = (TextView) findViewById(R.id.switch1);
switch1.setText(String.valueOf(this));
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
And I'm getting this error
java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()
You have a NullPointerException indicating that userID is not initialized, hence this error:
Can't pass null for argument 'pathString' in child()
To solve this, please add the following line of code:
String userID = FirebaseAuth.getInstance().getCurrentUser().getUid();
Right in front of:
ordersRef.child(userID).child("fast_delivery").addValueEventListener(/* ... */);
i guess if im right.. you have created separate tree for orders where you are getting orders
create model class give name as same as childname are where you're getting boolean value of fast_delivery use that boolean value to convert it to string and hence compare that value if String.valueof(boolean)is="true" then print to textview or vise versa with off

Nodes are getting created in Firebase everytime I click a button

Thanks for helping. I am creating an Android app which is working perfectly well. But I am facing a minor problem. The application contains message functionality for the posted ads. When the user opens an ad he is greeted with two options of call or message, where if the user clicks on the message button then he is sent to the chat screen, at the time of the button press the info gets stored in firebase database but I want the info to get stored only for the first time and if the users clicks the button next time then he should be taken to the pre-generated node, not on the new one.
Below is my code for adding the new nodes on the click of the button. Please lemme know if there is any way through which I can stop nodes from getting generated on every button click:
mChat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent chatIntent = new Intent(ExpandActivity.this, ChatActivity.class);
chatIntent.putExtra("user_id", mId);
chatIntent.putExtra("user_name", mName);
chatIntent.putExtra("ad_id", user_id);
chatIntent.putExtra("cow", m);
startActivity(chatIntent);
mRootRef.child("Chat").child(uid).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
DatabaseReference user_message_push = mRootRef.child("Chat").child(uid).push();
m = user_message_push.getKey();
if (!dataSnapshot.hasChild(user_id)) {
mRootRef.child("AdvertData").child(user_id).addValueEventListener(new
ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
String image = dataSnapshot.child("image").getValue().toString();
String title = dataSnapshot.child("title").getValue().toString();
String price = dataSnapshot.child("price").getValue().toString();
Map chatAddMap = new HashMap();
chatAddMap.put("seen", false);
chatAddMap.put("timestamp", ServerValue.TIMESTAMP);
chatAddMap.put("image", image);
chatAddMap.put("title", title);
chatAddMap.put("price", price);
chatAddMap.put("chatUser", mName);
chatAddMap.put("ads", user_id);
chatAddMap.put("chatUserId", mId);
Map chatAddMap1 = new HashMap();
chatAddMap1.put("seen", false);
chatAddMap1.put("timestamp", ServerValue.TIMESTAMP);
chatAddMap1.put("image", image);
chatAddMap1.put("title", title);
chatAddMap1.put("price", price);
chatAddMap1.put("chatUser", name);
chatAddMap1.put("ads", user_id);
chatAddMap1.put("chatUserId", uid);
Map chatUserMap = new HashMap();
chatUserMap.put("Chats/" + uid + "/" + m, chatAddMap);
chatUserMap.put("Chats/" + mId + "/" + m, chatAddMap1);
mRootRef.updateChildren(chatUserMap, new DatabaseReference.CompletionListener() {
#Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
if (databaseError != null) {
Log.d("CHAT_LOG", databaseError.getMessage().toString());
}
}
});
Below is the picture of how my database looks like:
Here is the logic..! Use addListenerForSingleValueEvent. It is called only one time. And Get key only if snapshot doesn't exist
DatabaseReference user_message_Ref = mRootRef.child("Chat").child(uid);
// First check if snapshot exist()
user_message_Ref.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists())
{
// GO TO THE DESIRED ACTIVITY
navigateToYOURActivity();
}else
{
// GET KEY
String key = user_message_Ref.push().getKey();
// & UPDATE DATABASE
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
I want the info to get stored only for the first time and if the
users clicks the button next time then he should be taken to the
pre-generated node
It's probably because of addValueEventListener().
You can try using addListenerForSingleValueEvent() which does this only one time i suppose.
When you are using the following lines of code:
DatabaseReference user_message_push = mRootRef.child("Chat").child(uid).push();
m = user_message_push.getKey();
You are generating a new random id at every button click. This is what push() method does. If you want the second time you click the button to access the same reference, you should store that id in a variable and use the same variable again, not generate another one.

How to send multiple checkbox values to Firebase Database when a Button is clicked Android

I want to send multiple checkbox values to Firebase database. I took three Checkboxes in XML and a Button. I want to store selected checkbox text to Firebase when the Button is clicked and go to another Activity. This is my code, but it isn't working.
mFirstCheckBox = findViewById(R.id.firstCheckBox);
mSecondCheckBox = findViewById(R.id.secondCheckBox);
mThirdCheckBox = findViewById(R.id.thirdCheckBox);
mDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
mAuth = FirebaseAuth.getInstance();
saveButton = findViewById(R.id.saveButton);
saveButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.firstCheckBox:
if(mFirstCheckBox.isChecked()) {
String user_id = mAuth.getCurrentUser().getUid();
DatabaseReference current_user_db = mDatabase.child(user_id);
current_user_db.child("1").setValue("Compiler design");
}
break;
case R.id.secondCheckBox:
if(mSecondCheckBox.isChecked()) {
String user_id = mAuth.getCurrentUser().getUid();
DatabaseReference current_user_db = mDatabase.child(user_id);
current_user_db.child("2").setValue("Operating Systems");
}
break;
case R.id.thirdCheckBox:
if(mThirdCheckBox.isChecked()) {
String user_id = mAuth.getCurrentUser().getUid();
DatabaseReference current_user_db = mDatabase.child(user_id);
current_user_db.child("3").setValue("Software Engineering");
}
break;
}
Intent interestIntent = new Intent(HomeActivity.this, InterestsActivity.class);
interestIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(interestIntent);
}
});
This is what I want to achieve in Firebase database, but not getting. In between 'Users' and 'Values(1,2,3)' there must be user_Id. it is missing in the diagram
because I have entered them in Firebase
Please Guide me, where I am getting wrong. Tell me any other alternative to get this done, if I am completely wrong.
Thanks in advance.
First, when you are using this view.getId(), you are getting the id of the button that was clicked and not the id of your checkbox that was checked. Second, there is no need to use the following line of code in each case of your switch statement:
String user_id = mAuth.getCurrentUser().getUid();
It is enough to be used only once. In fact, there is no need to use a switch statement at all. Your code should look like this:
saveButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String user_id = mAuth.getCurrentUser().getUid();
DatabaseReference current_user_db = mDatabase.child(user_id);
if(mFirstCheckBox.isChecked()) {
current_user_db.child("1").setValue("Compiler design");
}
if(mSecondCheckBox.isChecked()) {
current_user_db.child("2").setValue("Operating Systems");
}
if(mThirdCheckBox.isChecked()) {
current_user_db.child("3").setValue("Software Engineering");
}
Intent interestIntent = new Intent(HomeActivity.this, InterestsActivity.class);
interestIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(interestIntent);
}
});
CheckBoxes only have two states - checked and unchecked, i.e. true and false.
Therefore, you're correct in saying a String isn't a suitable datatype
for holding information given by a CheckBox.
What you want to use is a boolean.
Treat CheckBoxes as having boolean values (the isChecked()
method each CheckBox has will probably come in handy),
and save them .

Categories

Resources