I need that when I click on the button, the text in cenApple changes to i * 132, but when I click on it, countApple becomes 1 more, as it should be, and cenApple remains the same, but if I click on it again, cenApple already changes to i * 132.
View.OnClickListener appleListenerPlus = new View.OnClickListener() {
#Override
public void onClick(View v) {
int i = Integer.parseInt(countApple.getText().toString());
countApple.setText(String.valueOf(i + 1));
i = i;
int k = (i * 132);
String a = (String.valueOf(k));
cenApple.setText(a + " money");
}
};
why do you assigning same value again in this line: i = i;. it will stay the same...
when you change text then you should read it again or at least update i value also
int i = Integer.parseInt(countApple.getText().toString());
i++; // increment in here
countApple.setText(String.valueOf(i));
// or just read updated value again
//i = Integer.parseInt(countApple.getText().toString());
int k = (i * 132);
String a = (String.valueOf(k));
cenApple.setText(a + " money");
Related
this code work very well its generate three button because I have three elements"car" and shuffle elements
button_1 "a" , button_2 "r" , button_3 "c" , my problem is when I click on every button its typing
in edit text became "a a a"
//the problem was here (var button)
edittext.setText(edittext.getText().toString() + button.getText());
i am trying to make game guessing the word
String array[]= {"car"};
int random = (int) (Math.random() * array.clone().length);
StringBuilder word = new StringBuilder(array[random]);
for (int i = 0; i < array[random].length(); i++) {
char id = 'b';
final Button button = new Button(this); //local var
button.setId(id + i);
linearLayout.addView(button);
int randIndex = new Random().nextInt(word.length());
button.setText("" + word.charAt(randIndex));
word.deleteCharAt(randIndex);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//******the problem was here (var button)
button.setVisibility(View.INVISIBLE);
edittext.setText(edittext.getText().toString() + button.getText());
}
});
}
I have an operation in which the value of a variable changes during the following process. The following code shows this operation.
private long checkOffCode(String pChode) {
final long[] percent = {0};
APIGettingPosts apiGettingPosts = new APIGettingPosts(MainActivity.this, "get_off_code.php");
apiGettingPosts.getOffCode(new APIGettingPosts.OnOffCodeReceived() {
#Override
public void onReceived(List<Posts> posts) {
if (posts == null || posts.isEmpty()) {
// There is no value on sever...
} else {
for (int i = 0; i < posts.size(); ++i) {
// If the code in serve is equal with pCode],
// change the price value....
if (pChode.equals(posts.get(i).getCode())) {
percent[0] = Long.valueOf(posts.get(i).getPercent());
}
}
}
}
});
return percent[0];
}
The checkOffCode function receives a code, and returns a variable named percent if its value is equal to the value stored in the server.
In the event setOnClickListener of the btnPurchase button, this value is called, and using of its value, the price variable is computed.
The code for this section is as follows:
btnPurchase.setOnClickListener(new View.OnClickListener(){
long percent = 0;
#Override
public void onClick (View view){
percent = checkOffCode("MYCODE");
if (percent != 0) {
// update price value...
price = price - price * percent / 100;
Log.e("Success >>", String.valueOf(price));
} else {
Log.e("Failure >>", String.valueOf(price));
}
}
});
The problem is that when I click on the btnPurchase button for the first time, the previous value of percent [percent = 0] is calculated in the operation, but when I click on the button for the second time, the variable price is calculated with the new percent value.
The output Log cat is shown in both first and second clicks respectively as follow:
08-28 00:45:04.589 28467-28467/? E/Success >>: 125000
08-28 00:45:11.425 28467-28467/? E/Success >>: 16000
The question is: How can I calculate the value of price with the new percent value at the first time?
There is no relation with whether you click the button first or second time.
The problem is that you try to get the return value from checkOffCode directly without being sure about that onReceived has been called. You can change the code like this:
private void checkOffCode(String pChode) {
final long[] percent = {0};
APIGettingPosts apiGettingPosts = new APIGettingPosts(MainActivity.this, "get_off_code.php");
apiGettingPosts.getOffCode(new APIGettingPosts.OnOffCodeReceived() {
#Override
public void onReceived(List<Posts> posts) {
if (posts == null || posts.isEmpty()) {
// There is no value on sever...
} else {
for (int i = 0; i < posts.size(); ++i) {
// If the code in serve is equal with pCode],
// change the price value....
if (pChode.equals(posts.get(i).getCode())) {
percent[0] = Long.valueOf(posts.get(i).getPercent());
}
}
}
if (percent[0] != 0) {
// update price value...
price = price - price * percent[0] / 100;
Log.e("Success >>", String.valueOf(price));
} else {
Log.e("Failure >>", String.valueOf(price));
}
}
});
}
btnPurchase.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
checkOffCode("MYCODE");
}
});
You can try adding boolean that if firstClicked, calculate the price, so if you tap again for the second time, the calculation will not triggered. This is the example:
First make boolean variable outside the onClick function:
private Boolean isFirstClicked = false;
Then modify your onClick function:
if (!isFirstClicked) {
// update price value...
isFirstClicked = true
price = price - price * percent / 100;
Log.e("Success >>", String.valueOf(price));
} else {
Log.e("Failure >>", String.valueOf(price));
}
I am making a program of clicking the same 2 dublicate pics.
If user clicked the wrong pic after clicking the 1st pic, there will be negative value. If the two pics are same, I set those buttons to clickable(false). But my problem is, when the second pic is not same as 1st pic, I want to set clickable(true) to second pic, as well as the 1st pic but I have no idea how to get the id of 1st clicked button.
I have 16buttons and in this, I posted button1, other buttons are similar.
my code is.....
Collections.shuffle(buttonResources);
for(int i = 0; i < buttonResources.size(); i++)
{
ImageButton bt = findViewById(buttonResources.get(i));
buttons.add(bt);
bt.setBackgroundResource(pics.get(i/2));
bt.setTag(pics.get(i/2));
aaa = pics.get(i/2);
bt.setOnClickListener(this);
bt.setClickable(false);
}
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
AlphaAnimation alphaAnimation0 = new AlphaAnimation(1,0);
alphaAnimation0.setDuration(200);
alphaAnimation0.setFillAfter(true);
for(int i = 0; i < buttonResources.size(); i++)
{
ImageButton bt = findViewById(buttonResources.get(i));
buttons.add(bt);
bt.startAnimation(alphaAnimation0);
bt.setClickable(true);
}
}
}, 5000);
}
#Override
public void onClick(final View v1) {
switch (v1.getId()) {
case R.id.bt1:
AlphaAnimation alphaAnimation1 = new AlphaAnimation(0, 1);
alphaAnimation1.setDuration(300);
alphaAnimation1.setFillAfter(true);
bt1.startAnimation(alphaAnimation1);
aaa = (int) bt1.getTag();
bt1.setClickable(false);
if(bbb==0){
bt1.setClickable(false);
bbb = aaa;
}else if (bbb==aaa){
right++;
tvtext.setText("Right" + right + "wRONG" + wrong);
bbb=0;
}else{
wrong++;
tvtext.setText("Right" + right + "wRONG" + wrong);
bbb=0;
AlphaAnimation alphaAnimation = new AlphaAnimation(1,0);
alphaAnimation.setDuration(600);
alphaAnimation.setFillAfter(true);
bt1.startAnimation(alphaAnimation);
bt1.setClickable(true);
}
break;
add int bbbID=0;
then add these changes:
case R.id.bt1:
AlphaAnimation alphaAnimation1 = new AlphaAnimation(0, 1);
alphaAnimation1.setDuration(300);
alphaAnimation1.setFillAfter(true);
bt1.startAnimation(alphaAnimation1);
aaa = (int) bt1.getTag();
bt1.setClickable(false);
if(bbb==0){
bt1.setClickable(false);
bbb = aaa;
bbbID = v1.getId(); //////change
}else if (bbb==aaa){
right++;
tvtext.setText("Right" + right + "wRONG" + wrong);
bbb=0;
bbbID = 0; //////change
}else{
wrong++;
tvtext.setText("Right" + right + "wRONG" + wrong);
bbb=0;
AlphaAnimation alphaAnimation = new AlphaAnimation(1,0);
alphaAnimation.setDuration(600);
alphaAnimation.setFillAfter(true);
bt1.startAnimation(alphaAnimation);
bt1.setClickable(true);
for(int i=0;i<buttons.size();i++){ //////change
if(buttons.get(i).getId() == bbbID){ //////change
buttons.get(i).setClickable(true); //////change
}
}
}
break;
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);
}
});
So, I'm developing an app for my graduation final project and in my app I have a layout with an imageView and a textView, to set the content of these elements I receive an ArrayList as a parameter and use the setText(), and it works.
But my Arraylist has more than one element, and my big problem is that I want to change the content of the imageView and the textView when I click in a button.
The way I was thinking was that when I click the button it will increment the value and change the content of the objects for the next element inside the arrayList.
I've faced problems with global variables inside onClickListeners, but I solved them with auxiliar methods, but this time that doesn't work since I need to increment the value of the iteration inside the button event.
Can you guys please help me with suggestions or tell me what I'm I doing wrong?
I hope you understand my problem, and I'm sorry if I didn't explain me well, feel free to ask any questions and request more blocks of code if what I post isn't enough.
Thanks in advance to all you guys, i apreciate all the help you can give me!
To get the data from my db and store it into an arraylist
private ArrayList<Exercises> getExercisesSelectedPlan() {
ArrayList<ProgramExercises> arrayList_programExercises = new ArrayList<();
ArrayList<Exercises> arrayList_exercises = new ArrayList<>();
Button btnNext = (Button) findViewById(R.id.btnFinishAquecimento);
//with the id of the selected plan, select all the exercises from the plan and load them into the layout
int idProgram = getIdPlan();
DatabaseHelper databaseHelper = new DatabaseHelper(this);
SQLiteDatabase db = databaseHelper.getWritableDatabase();
String queryProgramExercises = "SELECT * FROM PROGRAM_EXERCISES WHERE PROGRAM_ID_PROGRAM = " + "'" + idProgram + "'" + " ORDER BY ORDER_EX ASC";
Cursor cursorProgramExercises = db.rawQuery(queryProgramExercises, null);
int numRows = cursorProgramExercises.getCount();
if (numRows > 0) {
cursorProgramExercises.moveToFirst();
while (numRows > 0) {
for (int i = 0; i < cursorProgramExercises.getCount(); i++) {
ProgramExercises nProgramExercises = new ProgramExercises();
nProgramExercises.setIdProgramExercises(cursorProgramExercises.getInt(0));
nProgramExercises.setOrder(cursorProgramExercises.getString(1));
nProgramExercises.setRepetition(cursorProgramExercises.getInt(2));
nProgramExercises.setIdExercises(cursorProgramExercises.getInt(3));
arrayList_programExercises.add(nProgramExercises);
numRows--;
}
}
}
//for each select result from program_exercises i have to select the exercise with the id correspondent to nProgramExercies.getidExercises
for (int i = 0; i < arrayList_programExercises.size(); i++) {
String queryExercises = "SELECT * FROM EXERCISES WHERE ID_EXERCISES = " + "'" + arrayList_programExercises.get(i).getIdExercises() + "'";
Cursor cursorExercises = db.rawQuery(queryExercises, null);
cursorExercises.moveToNext();
if (cursorExercises.getCount() > 0) {
Exercises exercises = new Exercises();
for (int j = 0; j < cursorExercises.getCount(); j++) {
exercises.setIdExercises(cursorExercises.getInt(0));
exercises.setMedia(cursorExercises.getString(1));
exercises.setDesignation(cursorExercises.getString(2));
exercises.setIdExercisesPhase(cursorExercises.getInt(3));
arrayList_exercises.add(exercises);
cursorExercises.moveToNext();
}
cursorExercises.close();
}
}
cursorProgramExercises.close();
db.close();
databaseHelper.close();
}
Where I want to change the content of the textview and increment the iteration when I click btnNext
public void prepareExercise(final ArrayList<Exercises> exercises) {
setContentView(R.layout.activity_execute_ex_warmup);
TextView txtPrepare = (TextView) findViewById(R.id.tvAquecimento);
ImageView imageExercise = (ImageView) findViewById(R.id.ivAquecimento);
Button btnNext = (Button) findViewById(R.id.btnFinishAquecimento);
txtPrepare.setText(exercises.get(1).getDesignation());
imageExercise.setImageResource(R.drawable.imgTest);
for (int i = 0; i < exercises.size(); i++) {
if (exercises.get(i).getIdExercisesPhase() == 1) {
txtPrepare.setText(exercises.get(i).getDesignation());
System.out.println("------ TEST" + exercises.get(i).getDesignation());
}
}
//Click Next Button
btnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//for (int i = 1; i < exercises.size(); i++) {
//TextView txtPrepare = (TextView) findViewById(R.id.tvAquecimento);
//txtPrepare.setText(exercises.get(finalI).getDesignation());
//}
//executeExercise(); //next Exercise
}
});
}
How I invoke the previous method
prepareExercise(getExercisesSelectedPlan());
Hopefully I'm understanding correctly.
If you just want to update the views each time the button is clicked the easiest way would be just to create a variable and store the position of the array list.
public int listIncrement = 0;
private TextView txtPrepare;
private ImageView imageExercise;
Then in your prepareExercise() method
btnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(listIncrement <= excersises.size()) {
executeExercise(excersises, listIncrement); //next Exercise
listIncrement++;
}
}
});
and then in executeExercise(ArrayList exercises, int position) //guessed method name, can be whatever you like
txtPrepare.setText(exercises.get(position).getDesignation());
imageExercise.setImageResource(R.drawable.imgTest);