how to stop a firebase counter at 0 - java

ive been using this counting method in firebase that counts up likes.
ref.child("downloads").runTransaction(new Transaction.Handler() {
#Override
public Transaction.Result doTransaction(MutableData mutableData) {
if (mutableData.getValue() != null) {
int value = mutableData.getValue(Integer.class);
if (increment) {
value++;
} else {
value--;
}
mutableData.setValue(value);
}
return Transaction.success(mutableData);
}
i have a variable in my database called downloads which is automatically set to 0 when user signs up. then when a user clicks a button they get rewarded 5 downloads for watching a short reward advert.. how can i edit this code to instead of counting up it counts down from 5 - 0 then stops?

Related

Using two exoplayer inistances for gapless playback Java

I am preparing the next video in another exoplayer instance while playing the current one for gapless playback.
But after setting player visibility gone video is not playing.
Here is my function for playing current video and initializing next
void prepare_next_media() {
int next_index = playing_index + 1;
if (next_index > videoFiles.size() - 1) {
next_index = 0;
}
if (current_view == 1) {
player1.setMediaItem(MediaItem.fromUri(videoFiles.get(next_index).getAbsolutePath()));
player1.prepare();
} else {
player.setMediaItem(MediaItem.fromUri(videoFiles.get(next_index).getAbsolutePath()));
player.prepare();
}
}
void play_next_media() {
int current_index = playing_index + 1;
if (current_index > videoFiles.size() - 1) {
current_index = 0;
}
if (current_view == 1) {
playerView.setVisibility(StyledPlayerView.GONE);
playerView1.setVisibility(StyledPlayerView.VISIBLE);
if (player1.getMediaItemCount() == 0) {
player1.setMediaItem(MediaItem.fromUri(videoFiles.get(current_index).getAbsolutePath()));
player1.prepare();
}
player1.play();
} else {
playerView.setVisibility(StyledPlayerView.VISIBLE);
playerView1.setVisibility(StyledPlayerView.GONE);
if (player.getMediaItemCount() == 0) {
player.setMediaItem(MediaItem.fromUri(videoFiles.get(current_index).getAbsolutePath()));
player.prepare();
}
player.play();
}
current_view = 1 - current_view;
playing_index = current_index;
prepare_next_media();
}
The problem is when I set playerView1.setVisibility(StyledPlayerView.GONE), and prepare it and on completing the previous one playerView.setVisibility(StyledPlayerView.VISIBLE); make it visible but the video is not playing only the image is showing but the video changes when ended.
on onPlaybackStateChanged, it shows the video is playing. I also tried with set alpha but did not work can anyone suggest me better way to solve this issue or issue of gapless playback?
Thanks in advance.

NotifyDataSetChanged isn't updating arrayList when called the second time

Why is my question getting negative marks? i do not understand. Please help.
I have 5 lists displaying in the adapter one after another. I have to delete 2 rows when user clicks on a button in the activity. (Remove two options from the list every time the button is clicked) Each list gets the button click only once.
On click of button, NotifyDataSetChanged method does not update the list in the adapter.
For now it is happening for list 1, when 2nd list comes in, the button is clicked, 2 items from arraylist are deleted but notifyDataSetChanged does not update the list in the adapter.
In Activity :
row_number = count that i get from implementing the interface mentioned in adapter
private void removeTwoFunctionality() {
button_removeTwo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (row_number == 1) {
TextArray1.remove(0);
TextArray1.remove(1);
adapter.notifyDataSetChanged();
adapter.RemoveTwoButtonClicked();
}
if (row_number == 2) {
TextArray2.remove(0);
TextArray2.remove(1);
adapter.notifyDataSetChanged();
adapter.RemoveTwoButtonClicked();
}
if (row_number == 3) {
TextArray3.remove(0);
TextArray3.remove(1);
adapter.notifyDataSetChanged();
adapter.RemoveTwoButtonClicked();
}
if (row_number == 4) {
TextArray4.remove(0);
TextArray4.remove(1);
adapter.notifyDataSetChanged();
adapter.RemoveTwoButtonClicked();
}
if (row_number == 5) {
TextArray5.remove(0);
TextArray5.remove(1);
adapter.notifyDataSetChanged();
adapter.RemoveTwoButtonClicked();
}
}
});
}
In ADAPTER onBindViewHolder:
All the conditions are satisfied when next list arrives
// list 1
if (!TextArray1.isEmpty()) {
count = 1;
holder.name.setText(TextArray1.get(position).getTitle());
}
// list 2
if (TextArray1.isEmpty() && TextArray2.size() > 0) {
count = 2;
holder.name.setText(TextArray2.get(position).getTitle());
}
// list 3
if (TextArray2.isEmpty() && TextArray3.size() > 0) {
count = 3;
holder.name.setText(TextArray3.get(position).getTitle());
}
// list 4
if (TextArray3.isEmpty() && TextArray4.size() > 0) {
count = 4;
holder.name.setText(TextArray4.get(position).getTitle());
}
// list 5
if (TextArray4.isEmpty() && TextArray5.size() > 0) {
count = 5;
holder.name.setText(TextArray5.get(position).getTitle());
}
// interface to know the count in the activity
if (mOnAnswerListener != null) {
mOnAnswerListener.getRowNumber(count);
}
Try like this
TextArray5.remove(0);
TextArray5.remove(1);
adapter.RemoveTwoButtonClicked();
adapter.notifyDataSetChanged();
You should put notifyDataSetChanged after deleting the items :
TextArray5.remove(0);
TextArray5.remove(1);
adapter.RemoveTwoButtonClicked();
adapter.notifyDataSetChanged();

TableRowSorter shows different result everytime I clicked searchButton

For my code, I had a really weird bug.
Everytime I press the Search Button, the result always different.
This is my dummy database.
I have 4 columns and 3 rows in the table :
001 Viagra 1 APL
002 Diane 2 SBF
003 Santibi 3 BSP
The first time I pressed the Search Button, the data inside table didn't change at all.
This is the screenshot of my program.
This is what happened if I pressed the Search Button the second time (the result is correct)
The third time I pressed the button, it shows an incorrect result
If I pressed the button continuously, the result keep changing back and fort from correct to incorrect one.
This is my code :`
public void searchPerformed() {
if (tSearch.getText().toString() == sch) { return; }
sch = tSearch.getText().toString();
int iColumn = cbSearch.getSelectedIndex();
TableRowSorter<TableModel> sorter = new TableRowSorter<>(model);
tbl.setRowSorter(sorter);
if (tSearch.getText().length() > 0) {
sorter.setRowFilter(RowFilter.regexFilter("(?i)" + tSearch.getText(), iColumn));
} else {
sorter.setRowFilter(null);
}
if (tbl.getRowCount() != 0) { tbl.setRowSelectionInterval(0, 0); }
else { clearText(); }
}
private void tSearchKeyPressed(java.awt.event.KeyEvent evt) {
if (evt.getKeyCode() != KeyEvent.VK_ENTER) { return; }
searchPerformed();
}
private void tSearchKeyPressed(java.awt.event.KeyEvent evt) {
if (evt.getKeyCode() != KeyEvent.VK_ENTER) { return; }
searchPerformed();
}

adding two decimals together giving truncated result when any value other than first value is not a whole number

I am attempting to create a calculator app, when I try to add 2 decimals together, I do not get the results I anticipated.
examples:
user inputs [1.1 + 1.1] and [1.1 + 1] or 2.1 is returned.
It is as if as if second value is being truncated, I am not entirely sure where this would be occurring
user inputs[2.1 + 2] and [2.1 + 2] or 4.1 is returned
user inputs [2 + 2.1] and [2 + 2] or 4 is returned
I have not used the debugger, though I have placed print line statements through out the code and I have not been able to see the issue.
Code:
information
I only attached a single number (image button) as all numbers 1-9 have identical code
variables that are not instantiated in code below have been instantiated as "" if they are a string and 0.0 if they are a double
I am using an array list called values which takes in strings, and using double.parsedouble() to convert it to a double, this occurs in the equals method
please let me know if their is anything I need to add to make this question easier to understand, and any help is appreciated
thank you!
1 button
one_button_ET.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (plus_selected) {
update_selected_buttons(); //for boolean variables and to change mathematical function button image
screen_value = "";
updateScreen("1");
System.out.println(screen_value);
values.add(screen_value);
} else {
updateScreen("1");
System.out.println(screen_value);
}
}
});
addition button
addition_button_ET.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!plus_selected) {
plus_selected = true;
addition_button_ET.setImageResource(R.drawable.addition_selected_500_500);
if(Double.parseDouble(screen_value) == current_value){
}
else
values.add(screen_value);//
} else {
plus_selected = false;
addition_button_ET.setImageResource(R.drawable.addition_button_500_500);
}
}
});
update screen
private void updateScreen(String value) { //being used for only one_button
//to reset title text
if (screen_value.equals(initial_screen_value)) {
screen_value = "0.0";
screen_TV.setTextSize(50);
}
//clears default value (0.0), allowing user to enter in a number
if (screen_value.equals("0.0")) {
screen_value = value;
screen_TV.setText(screen_value);
} else {
screen_value += value;
System.out.println(screen_value);
screen_TV.setText(screen_value);
}
}
clear button (clear button calls this method, and that is the only parameter it has)
private void resetScreen() {
screen_value = "0.0";
//values.clear();
screen_TV.setTextSize(50);
screen_TV.setText(screen_value);
}
equals button
equals_button_ET.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
current_value = 0;
sum = 0;
System.out.println(values);
for(int i = 0; i<values.size();i++) {
sum += Double.parseDouble(values.get(i));
}
current_value = sum;
screen_value = "" + sum;
screen_TV.setText(screen_value);
}
});

How to set click limit of clicking a button? click limit is equal to score over 5

How can I set a limit of clicking a button? The number of click is equal to score % 5.Example score is equal to 15 the number of click limit is equal to 3 how can I do that? my codes is not working
int score = 0;
int help = score % 5;
if (score == help) {
helpbtn.setEnabled(false);
} else {
helpbtn.setEnabled(true);
}
I put it inside of
public void onClick(View v) { }
If in the example limit is 3, then :
if(help>0)
{
//logic;
help--;
}
you can add it in else block.
I think I understand what you need now. I've made some assumptions. I added a separate button to submit answers and I added a boolean that is always true for now. I did manage to use modulo in this version though. Hope this helps.
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private int score = 0;
private int help = 0;
private boolean answerCorrect = true; // dummy set always true for now
private Button answerButton = null;
private Button hlpbtn = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
answerButton = (Button)findViewById(R.id.button_answer);
answerButton.setOnClickListener(this);
hlpbtn = (Button)findViewById(R.id.button_help);
hlpbtn.setOnClickListener(this);
hlpbtn.setEnabled(false);
}
#Override
public void onClick(View view) {
if(view.getId() == R.id.button_answer) {
if(answerCorrect) {
if((++score % 5) == 0) {
++help;
}
if((help > 0) && !hlpbtn.isEnabled()) {
hlpbtn.setEnabled(true);
}
}
Log.d("Quiz", "score = " + score + ", help = " + help);
} else if(view.getId() == R.id.button_help) {
if(--help <= 0) {
hlpbtn.setEnabled(false);
Log.d("Quiz", "help button disabled");
}
Log.d("Quiz", "help pressed, " + help + " remaining");
}
}
}
It sounds like you want 15 / 5 instead of 15 % 5.
15 / 5 == 3, whereas 15 % 5 == 0.
15/5 =3. Try this, this will help you to click a button 3 times.
%it gives remainder. Ie --->15%5=0

Categories