Parse Save - Android: save multiple rows - java

I am stuck with Parse and an OnClick button. The first click works just fine and onclick, the value is added to the database, but it seems like multiple clicks don't work and won't result in the function addPoints() below being properly ran. In fact, the Toast is printed at every click but the database is modified only once and only one row is added (instead of as many rows as many clicks)
clicker.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
addPoints(user);
}
});
}
private void addPoints(String user) {
points.put("points", 10);
points.put("business", x);
points.put("user", );;
points.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
Toast.makeText(getApplicationContext(), "10 Points added", Toast.LENGTH_SHORT).show();
}
});
Any idea how to solve this?
Thank you guys

In Parse put will update the value in the data base , it won't add the value to the existing one.
you need to get the value first from the database and then add to it 10 .

Related

How to change the text and color of the button on certain event in Firestore?

I am trying to figure out to change the text and color of the button on certain update to the firestore database. Below is the code which i used for the transaction in adapter. the below code do the work only temporary basis and I want is certain is once done the color and text of the button should be permanently changed.
db.runTransaction(new Transaction.Function<Boolean>() {
#Override
public Boolean apply(Transaction transaction) throws FirebaseFirestoreException {
DocumentSnapshot snapshot = transaction.get(likesRef);
boolean l1 = snapshot.getBoolean("l1");
if (l1 == false) {
transaction.update(likesRef, "l1", true);
// commentsViewHolder.favPostButton.setBackgroundColor(R.color.colorPrimary);
commentsViewHolder.favPostButton.setText("Let's Chat");
return l1;
} else
{
Toast.makeText(CommentActivity.this, "You already liked it", Toast.LENGTH_SHORT).show();
throw new FirebaseFirestoreException("You already liked",
FirebaseFirestoreException.Code.ABORTED);
}
}
}).addOnSuccessListener(new OnSuccessListener<Boolean>() {
#Override
public void onSuccess(Boolean result) {
//button change, button appear
// commentsViewHolder.favPostButton.setBackgroundColor(R.color.colorPrimary);
// commentsViewHolder.favPostButton.setText("Let's Chat");
Log.d(TAG, "Transaction success: " + result);
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.w(TAG, "Transaction failure.", e);
}
});
Do you mean the configuration change or activity restart by saying "reload the session"?
Activity is just a dumb pack of UI elements, if you change these during runtime and then reload the app or even just rotate the screen, all the elements are drawn as it is set in your xml layout file or in activity's onCreate method.
Activity needs a way to restore its instance.
There are multiple solutions on how to achieve this:
• saving your activity info in onSaveInstanceState and reading it in onCreate
• using SharedPreferences
• observing LiveData object

Login database issues

I am trying to fill a database with records so that when you login it will either accept you or not. For now I just have one to test it out but it doesn't seem to work. When I put in the info for ID and password and click the Button, nothing happens. Can anyone tell me what I'm missing or doing wrong? Sorry if it's obvious, but I'm just stuck right now.
https://github.com/liliycode/Login/blob/master/login%20java
Your click listener is not correct. You should define click listener for button in onCreate method. When you click button you get data from edittext and invoke method which inserts data to DB.
First of all your are comparing EditTexts to strings which is wrong
if ( loginET.equals("Liliy") && login2ET.equals("1234"))
change this to
if ( loginET.getText().toString().equals("Liliy") && login2ET.getText().toString().equals("1234"))
And make your buttonListener in onCreate(), and at click of button call your method populateData()
pressBTN.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//reading the inputs
String idET = loginET.getText().toString();
Double passET = Double.parseDouble(login2ET.getText().toString());
populateData();
}
});
Which checks for data and populates it.

How to Replace timer with turn based onClick in Android?

I am trying to make a TicTacToe online multiplayer game in Android Studio using Firebase, below is the code I am using for the buttons (1 of the 9 blocks of TicTacToe). Right now I am using a timer to stop the user from clicking any other buttons after he once click the button to give the other user time to click. But I want to make it so that one user won't be able to click any button till the other user clicks a button. Is there a way to do so?
The default values of nothing & face & a1 is "zero" and default value of added is "one" whereas rface is just a string variable getting its value from "face".
//below clicking
a1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//below
//starting if condition
if (ra1.equals(nothing)){
if (rface.equals(nothing)){
a1.setBackgroundResource(R.drawable.rounded);
Firebase refChild = ref2.child("a1");
refChild.setValue("round");
refChild = ref2.child("face");
refChild.setValue("one");
}
else if (rface.equals(added)) {
a1.setBackgroundResource(R.drawable.crossed);
Firebase refChild = ref2.child("a1");
refChild.setValue("cross");
refChild = ref2.child("face");
refChild.setValue("zero");
}
//timer below
a1.setClickable(false);
a2.setClickable(false);
a3.setClickable(false);
b1.setClickable(false);
b2.setClickable(false);
b3.setClickable(false);
c1.setClickable(false);
c2.setClickable(false);
c3.setClickable(false);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
a1.setClickable(true);
a2.setClickable(true);
a3.setClickable(true);
b1.setClickable(true);
b2.setClickable(true);
b3.setClickable(true);
c1.setClickable(true);
c2.setClickable(true);
c3.setClickable(true);
}
}, 3000);
//timer above
}
}
});
I suggest having two different OnClickListeners rather than an if statement. One for Xs and the other for Os. Then you can set the listener back and forth. For example, at the end of the OnClickListener for X, it calls setOnClickListener() using the one for Os.
I finally came up with this algorithm for my solution:
online=0;
offline=1;
if (online!=offline){
offline=random;
online=offline;
if(shape=0)
{
click(set round);
}
else if(shape=1)
{
click(set cross);
}
}
where shaper is another integer which I am using as a switch.

When last string removed it causes app to crash

Ok.. Here is my problem.
I have an arraylist of stings with 10 items in it.
Then I randomize the list and display a random string in a textview.
With the last string removed the app crashes. Any tip to fix that?
Here is the code:
Collections.shuffle(learnlist);
showlearntv.setText(learnlist.get(0));
nextlearn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showlearntv.setText(learnlist.get(1));
learnlist.remove(0);
You are attempting to call get(1) on a list that may contain less than two elements.
You should add some safety checks. E.g.:
nextlearn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!learnlist.isEmpty()) {
learnlist.remove(0);
}
if (!learnlist.isEmpty()) {
showlearntv.setText(learnlist.get(0));
}
}
On the click handler, remove the item first,
learnlist.remove(0)
Then if the list has any items,
showlearntv.setText(learnlist.get(0));
Else do something else.

Android button comparing

I've got very very basic problem in Java (Android) writing. Here it is, I've got code:
public void WriteValue (View sender){
Button bt=(Button)sender;
}
WriteValue is performed, when user click button. And now I want to compare button which user clicks, with button that Id I know. Something like
if(UserButton==ClearButton) Display.setText("0");
Thanks for help
Tux:)
You could just compare ids.-
public void WriteValue (View sender) {
Button bt = (Button)sender;
if(bt.getId() == R.id.clearButtonId) {
Display.setText("0");
}
}
step 1 : make your Class implement OnClickListener
step 2 :
// global variable
Button btOne ,bTwo;
step 3 :
// in onCreate() method
btOne = (Button)findViewById(R.id.btOne);
btTwo = (Button)findViewById(R.id.btTwo);
btOne.setOnClickListener(this);
btTwo.setOnClickListener(this);
step 4 : implement method from interface
puvlic void onClick(View v)
{
if(v==btOne)
{
// do work for btone
}
if(v==btTwo)
{
// do work for btTwo
}
}
You don't need to cast the View to a Button here. You can simply get the id of the View clicked which is the param sent to the function (assuming this is an onClick() function as defined in xml or by setting the listener in Java). So it could be something like this
public void WriteValue (View sender)
{
int id = sender.getId(); // get the id of the View clicked
swith (id) // switch logic on that id
{
case (R.id clearButtonId): // if the clearButton was clicked do this
Display.setText("0");
break;
default:
Display.setText("Some other String);
break;
}
}
You could also use if/else instead of a switch but this is more readable, IMHO. This easily allows you to add more logic for other Buttons.
Also consider using Java naming conventions. According to this code, I assume Display is a TextView or EditText so it should start with a lower-case letter (ex. display).

Categories

Resources