When user click Next button, it suppose to show next record store in database, but when I run the app, user have to click previous button to view next entry and the Next button didn't perform any activity.
public void onClick(View v) {
int buttonId = v.getId();
if (buttonId == R.id.buttonViewPrevious)
if (currentBoothArrayIndex < boothArrayList.size() - 1) {
displayBoothInformation(--currentBoothArrayIndex);
} else if (buttonId == R.id.buttonViewNext)
if (currentBoothArrayIndex < boothArrayList.size() - 1) {
displayBoothInformation(++currentBoothArrayIndex);
}
}
your if else construct is wrong. Try this:
if (buttonId == R.id.buttonViewPrevious){
if (currentBoothArrayIndex < boothArrayList.size() - 1) {
displayBoothInformation(--currentBoothArrayIndex);
}
}else if (buttonId == R.id.buttonViewNext){
if (currentBoothArrayIndex < boothArrayList.size() - 1) {
displayBoothInformation(++currentBoothArrayIndex);
}
}
Related
I am creating a matching card application. That only allows the user to select two cards per round.
Where you see the bricks are where the buttons are and so i have added them to an arraylist, but im not sure how to make it so that they can only press 2 buttons until they press the guess button where it checks if they are a match.
https://pastebin.com/65NqJ0GK
private void card1ButtonActionPerformed(java.awt.event.ActionEvent evt) {
String temp = cards.get(0);
if (temp.equals("0")) {
card1Button.setIcon(a);
} else if (temp.equals("1")) {
card1Button.setIcon(b);
} else if (temp.equals("2")) {
card1Button.setIcon(c);
} else if (temp.equals("3")) {
card1Button.setIcon(d);
} else if (temp.equals("4")) {
card1Button.setIcon(e);
} else if (temp.equals("5")) {
card1Button.setIcon(f);
} else if (temp.equals("6")) {
card1Button.setIcon(g);
} else if (temp.equals("7")) {
card1Button.setIcon(h);
}
count++;
if (count == 1) {
c1 = Integer.parseInt(temp);
change[0] = 0;
} else if (count == 2) {
c2 = Integer.parseInt(temp);
change[0] = 0;
}
}
^^Here is an example of a button code.
Could someone show me some way to do it.
I have created programmatically, 5 radio groups with 4 radio buttons each. Every radio button represents an answer to a question. I want when someone press the button, if the correct answer is checked, to set the color of the that text in green. What am i trying to do, is to know in the second OnClickListener which radio button was checked, and if it is the correct one, to make the text green. So, with this code, when i press the button, nothing happens. If i remove the condition if (CorrectAnswer == 1) and i press the button, i get this error: Attempt to invoke virtual method 'void android.widget.RadioButton.setTextColor(int)' on a null object reference. Any ideas? Here is my code:
boolean isChecked;
RadioButton checkedRadioButton;
RadioGroup radioButtonGroup;
int CorrectAnswer;
radioGroup[i].addView(answer[j]);
answer[j].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
checkedRadioButton = ((RadioButton) v);
int CorrectAnswer = Integer.parseInt(checkedRadioButton.getTag().toString());
isChecked = true;
if (isChecked) {
if (checkedRadioButton.isChecked() & CorrectAnswer == 1) {
score++;
isChecked = false;
}
}
}
});
finishButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < radioGroup.length; i++) {
int radioGroupId = radioGroup[i].getId();
radioButtonGroup = (RadioGroup) findViewById(radioGroupId);
int checkedRadioButtonId = radioButtonGroup.getCheckedRadioButtonId();
checkedRadioButton = (RadioButton) findViewById(checkedRadioButtonId);
if (CorrectAnswer == 1) {
checkedRadioButton.setTextColor(Color.GREEN);
}
for (int j = 0; j < answer.length; j++) {
radioGroup[i].getChildAt(j).setEnabled(false);
}
}
}
});
Thanks!
Do this to avoid calling methods on a null reference.
if (CorrectAnswer == 1 && checkedRadioButton != null)
EDIT:
Change the statement to this
if (checkedRadioButton != null && Integer.parseInt(checkedRadioButton.getTag().toString()) == 1)
I want to get the value that I enter from edit text to a text view *2 but the app crashes.
public void sub (View v)
{
int val=Integer.parseInt( e1.getText().toString());
if(val <= 1)
{
t1.setText("you havn't ran any kilos");
}
else if(val > 1) {
t1.setText(val*2);
}
}
}
try this.
t1.setText(val*2 + "");
because val is Integer type and setText method's param is String type.
Try
public void sub (View v)
{
int val=Integer.parseInt( e1.getText().toString());
if(val <= 1)
{
t1.setText("you havn't ran any kilos");
}
else if(val > 1) {
t1.setText(String.valueOf(val*2));
}
I have a imageView, button inside an activity, 10 pictures that are named from stage1 to stage9.
I need you help to solve some problem
I would like to use a button click to change picture that is on imageView to the next one.
I mean, I want to press on the button, and image view shows stage2 image, I press the button again and stage3 picture will be shown.
I have done this using counter var which count number of clicks and then running if statment to see which picture should go next, but it is too long and it is not possible to have more or less pictures.
I would like to know is there a way to do this, and if possible please show me how.
Thanks
code
private void changeImage(int counter) {
if (counter == 1) {
image.setImageResource(R.drawable.stage2);
} else if (counter == 2) {
image.setImageResource(R.drawable.stage3);
} else if (counter == 3) {
image.setImageResource(R.drawable.stage4);
} else if (counter == 4) {
image.setImageResource(R.drawable.stage5);
} else if (counter == 5) {
image.setImageResource(R.drawable.stage6);
} else if (counter == 6) {
image.setImageResource(R.drawable.stage7);
} else if (counter == 7) {
image.setImageResource(R.drawable.stage8);
} else if (counter == 8) {
image.setImageResource(R.drawable.stage9);
}
}
bassically this is the code that I am using right now.
It works, but if I want to do it to be more dynamic.
You'll need to tell the vm to update -> 'invalide()'.
Or use Picasso,
Picasso.with(Statics.context).load(R.drawable.stageX).error(R.drawable.error_img).resize(width, height).priority(Priority.HIGH).into(image);
Just get resource id by name, use like,
private void changeImage(int counter) {
if(counter >= 1 && counter <= 9) // Always check counter value before accessing as resource id
{
int counterValue = counter+1;
int resourceId = Resources.getSystem().getIdentifier("stage"+counterValue, "drawable", this.getPackageName()); // Use application context to get package name
image.setImageResource(resourceId);
}
}
I'm making a simple app where the user clicks a button which changes a TextView to the corresponding string, but when my first if statement is fulfilled it does not go on to fulfill the following if statement which should be.
if (index == 0 && index > -1 && index < 5) {
one.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
index++;
text1.setText("1");
}
});
This is my first if statement, it sets the TextView to "1", and then should add to the integer "index", this turns the index's value to "1", which should end this statement because it no longer qualify, which will begin the following if statement.
if (index == 1 && index > -1 && index < 5) {
one.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
text2.setText("1");
index++;
}
});
Now because of the previous if statement setting the index's variable to "1" this if statement should begin and the previous end, but this is not the case, even though the variables no longer qualify, it doesn't stop and the next if statement does not begin. It's as though the if statement is being ignored.
UPDATE.
I fixed my problem and here's what I changed the code to:
one.setOnClickListener(new View.OnClickListener(){public void onClick(View v){
if(index == 0){
text1.setText("1");
index++;
}else if(index == 1){
text2.setText("1");
index++;
}else if(index == 2){
text3.setText("1");
index++;
}else if(index == 3){
text4.setText("1");
index++;
}
}});
From your if statement
if (index == 0 && index > -1 && index < 5)
only
if (index == 0)
is enough
same as Second one..
if (index == 1)
That's not how if statements work. When the condition becomes false control flow does not suddenly leave the block and jump to some other if-block with a true condition.
Your problem is that your index++ statement is inside the onClick callback. So this code is not executed until the button gets clicked. By that time, your second if statement will long be executed. In other words:
Your first if statement is true, this adds an onClick listener to the button. Your second statement is false, this does nothing. The user clicks on the button. Now only the code inside that first callback is executed: index is increased and the text is set to "1". That is all.
[EDIT this is probably what you want]
one.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if ( index == 0 ) {
index++;
text1.setText("1");
} else if (index == 1 ) {
text2.setText("1");
index++;
}
}
});
After index == 1 or index == 0 what is the relevance of the other conditions?
Another viable solution:
if ((index > -1) && (index < 5)) {
one.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
text2.setText(index.toString());
index++;
}
});
-Obviously this depends on the logic you require specifically, if you do require another occurrence when index == 0, you would have to alter this format:
if (index == 0) {
//Whatever you want to do here... eg:
one.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
text2.setText("0");
index++;
}
});
}
else if ((index > -1) && (index < 5)) {
one.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
text2.setText(index.toString());
index++;
}
});
The reason that it doesn't work, is that the listener probably is added in "onLoad" method or in the constructor of the class.
Since your setOnClickListener is surrounded with a if statement, only the listener is only set if the statement is true. In your case the first statment is true, while the seccond statement is false.
This means that only the first listner is added, and everytime the button is clicked, the text1.setText("1") is called.
You should put your if statement inside your onClickListener, and not around the logic that sets the listener. So the following code:
if (index == 0 && index > -1 && index < 5) {
one.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
index++;
text1.setText("1");
}
});
should be
one.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (index == 0) {
index++;
// Do stuff
} else if(index == 1) {
index++;
// Do stuff
}
});
}
Every time the user hits the button 'one', the onClick is called.
Inside the onClick the statement is calculated, and the correct "Do stuff" is executed.