I am a beginner in android, and I am making an app related to SpellBee. The problem is I have 2 buttons on screen one for next word and one for previous word. At starting a default first word shows on screen . After pressing next word for the first time it displays next word from the database list. But when i press previous button, on first click it does nothing and on second click it shows the previous word, then on clicking next word button it does nothing and on second click it moves to next word. how can i solve this twice button problem This is my XML for Buttons
Code for buttons
public void next_click(View view) {
if(iterator <= wordList.size()) {
word.setText(wordList.get(iterator));
definition.setText(definitionList.get(iterator));
usage.setText(usageList.get(iterator));
iterator++;
/*if (iterator == wordList.size()) {
Toast.makeText(Level1Activity.this, "Level 1 Completed",
Toast.LENGTH_SHORT).show();
next_word.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Level1Activity.this,
SelectLevelActivity.class);
startActivity(i);
}
});
}*/
}
}
public void previous_click(View view) {
if(iterator != 0) {
--iterator;
word.setText(wordList.get(iterator));
definition.setText(definitionList.get(iterator));
usage.setText(usageList.get(iterator));
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.msiprestige.spellbee.Level1Activity"
android:background="@drawable/bookshelf"
android:alpha="0.9">
<TextView
android:id="@+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/level_1"
android:textColor="#fff"
android:textSize="50sp"
android:visibility="visible"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/word"
android:layout_width="261dp"
android:layout_height="54dp"
android:layout_marginTop="23dp"
android:ems="10"
android:gravity="center_horizontal"
android:textColor="#fff"
android:textColorLink="#fff"
android:textSize="22sp"
android:textStyle="bold"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView10" />
<TextView
android:id="@+id/textView12"
android:layout_width="80dp"
android:layout_height="24dp"
android:layout_marginStart="4dp"
android:text="@string/definition"
android:textColor="#fff"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.36" />
<TextView
android:id="@+id/textView13"
android:layout_width="54dp"
android:layout_height="24dp"
android:layout_marginStart="16dp"
android:layout_marginTop="109dp"
android:text="@string/usage"
android:textColor="#fff"
android:textSize="18sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView12" />
<TextView
android:id="@+id/definition"
android:layout_width="277dp"
android:layout_height="106dp"
android:layout_marginEnd="20dp"
android:layout_marginTop="48dp"
android:width="0dip"
android:ems="10"
android:textColor="#fff"
android:textSize="15sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/word" />
<TextView
android:id="@+id/usage"
android:layout_width="277dp"
android:layout_height="106dp"
android:layout_marginEnd="20dp"
android:layout_marginTop="27dp"
android:width="0dip"
android:ems="10"
android:textColor="#fff"
android:textSize="15sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/definition" />
<Button
android:id="@+id/previous_word"
android:layout_width="154dp"
android:layout_height="56dp"
android:layout_marginBottom="38dp"
android:layout_marginLeft="36dp"
android:onClick="previous_click"
android:text="Previous Word"
android:focusable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent" />
<Button
android:id="@+id/next_word"
android:layout_width="154dp"
android:layout_height="56dp"
android:layout_marginBottom="36dp"
android:layout_marginRight="28dp"
android:onClick="next_click"
android:text="@string/next_word"
android:focusable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</android.support.constraint.ConstraintLayout>
I think if you change your code to this one your problem will be solved! And to know your mistake you were changing the iterator variable after setting the word to textview. and in the if for next_click it should be less than wordList.size() - 1 to avoid IndexOutOfBound exception!
public void next_click(View view) {
if(iterator < wordList.size()-1) {
iterator++;
word.setText(wordList.get(iterator));
definition.setText(definitionList.get(iterator));
usage.setText(usageList.get(iterator));
startAnotherActivity(iterator);
}
}
public void previous_click(View view) {
if(iterator != 0) {
iterator--;
word.setText(wordList.get(iterator));
definition.setText(definitionList.get(iterator));
usage.setText(usageList.get(iterator));
startAnotherActivity(iterator);
}
}
private void startAnotherActivity(int your_extra_here) {
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("key_for_extra", your_extra_here);
startActivity(intent);
}
I felt that you need to pass the iterator to another activity thus I provided an extra for you here. if you don't need it just omit that part!!!