How to store data in session [duplicate] - java

This question already has answers here:
shopping cart for non registered users
(6 answers)
Closed 3 years ago.
I have a doubt to storing data in database without login like e-commerce app.
In back-end the data is store by userID to store data but client side is the app used without login and the product can be add to cart and add to wish list so how can that possible to store data in android.Any help or idea it will more helpful.Thanks

Find the below code
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); //0 - for private mode
Editor editor = pref.edit();
For storing the data
editor.putString("key_name", "string_value"); // Storing string
editor.commit(); // commit changes
For Retrieve
pref.getString("key_name", null); // getting String
To Clear
editor.remove("Key_name"); // will delete key name
editor.commit(); // commit changes

You can either store the temporary data in SharedPrefs or in the SQLite database using Room.
When the user adds/removes the product simply update the database or prefs and when user wants to buy move to the login screen > after successful login read the data from the database and proceed as you want.
When the user clears the cart or wishlist, clear the respective tables.

Related

How to update user data without changes into Firestore when it's checking for already existence in database?

I recently implemented unique username into my app when registering, all good far here, by the way I also need to set it to when the user is editting it's profile.
I tried to do the same thing, but I'm facing an issue here. The app don't let save the profile, because it's checking if the username's taken and, as we're already using one, it won't let us do it.
Ex.: My username is "bob", I changed my profile pic or my display name, so when I click to save, the app will do a username checking in the background and will not let me save it because the username is already taken, but the problem is that it's already my user.
I've tried to set this, but failed:
if (document.equals(setup_username.getText().toString()) || document.isEmpty()){
updateProfile();
Here's my code:
setup_progressbar.setVisibility(View.VISIBLE);
FirebaseFirestore.getInstance().collection("Users").whereEqualTo("username",setup_username.getText().toString()).get().addOnCompleteListener((task) -> {
if (task.isSuccessful()){
List<DocumentSnapshot> document = task.getResult().getDocuments();
if (document.equals(setup_username.getText().toString()) || document.isEmpty()){
updateProfile();
} else {
setup_progressbar.setVisibility(View.INVISIBLE);
setup_username.setError(getString(R.string.username_taken));
return;
}
} else {
setup_progressbar.setVisibility(View.INVISIBLE);
String error = task.getException().getMessage();
FancyToast.makeText(getApplicationContext(), error, FancyToast.LENGTH_SHORT, FancyToast.ERROR, false).show();
}
});
So how to get around this and only forbid it when I try to change my username to another that is taken? Like: "bob" to "bill", but "bill" is already taken, so it won't allow.
You'll need to have some indication in each Users document to indicate which user has claimed that specific name. Given that you store the username inside the document, ownership would typically be established by using the UID of the user as the ID of the document.
Once you have run your query to find the document for the username, you can then check the UID of the owner of that username against the currently signed in user. If the two UIDs are the same, the current user owns the username and is allowed to update the document.
Compare new username with previous username(store it in a variable while displaying user profile data), if both are same don't update it all else check for its uniqueness.
or if you don't have existing username data create relationship with that document and fetch previous username first.

How to get data from custom UID in firebase?

I have data structure like this for my android app. That can login and only see their profile. I dont make add content or do saving data to the database. So i created the data manually and the users only read and display the data.
How to make login,refer with username = "71140011" and password = "123456" which is inside real time database and the 71140011 only can see their profile only. thanks

Add value to specific key and datetime

I have an app where are few users. Each user can save item to firebase. But when a user save an item that item save under a date time child, and under user
name.
That's how my items are saved and users available
And instead of numbers (0,1,2) I want to appear date time and user name. Hope the question is ok i couldn't find any tutorial.
Here is my code :
databaseReference = FirebaseDatabase.getInstance().getReference("jsonData").child("listaVanzatoareProduse");
databaseReference.setValue(Util.getInstance().getVanzatorProduse());
Util.getInstance().getVanzatorProduse().clear();
The .child() method creates a new node if it doesn't exists. so you can simply do:
databaseReference = FirebaseDatabase.getInstance().getReference("jsonData").child("listaVanzatoareProduse").child(dateTime).child(userName);
databaseReference.setValue(Util.getInstance().getVanzatorProduse());

Saving read-only data in Sqlite for Android outside activity context

I need to have a read only database for an android application with three simple tables:
Countries
Cities ( country_id foreign key)
PhoneCountryCodes (country_id foreign key)
I have .csv files that I need to extract data from and fill in these tables. The purpose for these tables is for the android app reading purposes and data validation.
The link here shows how to add data to the managed database Sqlite for android. Yet it seems from the following code that I need a Context to instantiate the DbHelper class:
FeedReaderDbHelper mDbHelper = new FeedReaderDbHelper(getContext());
In other words, I want to add data to the database once and for all (basically Country data, cities and phone country codes), outside of the Activity context.
use getApplicationContext(); If you don't want activity context.

Why isn't the database being updated when I refresh the entity manager?

I'm currently developing a social networking site and I'm currently implementing the part where a user can change his password. I'm using the entity manager to refresh the contents of the database with the new password. The following is the code for the implementation.
final Implementation user = em.find(Implementation.class, username);
if((user!=null) && user.getPassword().equals(hash(username,oldPassword))){
user.setPassword(hash(username,newPassword));
em.refresh(user);
}else{
throw new ChangePasswordException();
}
however when I try to login again, the older password must be used, otherwise, if the new password is supplied it will tell you: passwords do not match. Does anyone know maybe why this is happening? I tried to first remove the user from the database, and then persist the new user again. However an EJB Exception was generated as the username was not unique since the user was not removed from the database.
Thanks a lot for your help
You are not saving your new password. You are overwriting your changes you have made. So refresh(user) will fetch the current state of that user and will write it into your object.
docu: Refresh the state of the instance from the database, overwriting changes made to the entity, if any.
Try to use merge or persist instead

Categories