I have an array that I want to output each index continuously and randomly here is my code :
package com.example.testtingskripsi;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
public String [] ArrayA ={"80","81","82","83","84","85"};
public TextView mViewLabel;
@Override
protected void onCreate(Bundle savedInstanceState) {
int rando =(int) ((Math.random())*ArrayA.length);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewLabel = (TextView) findViewById(R.id.textChanger);
Button startHeartbeat = (Button) findViewById(R.id.start);
Button stopHeartbeat = (Button) findViewById(R.id.stop);
startHeartbeat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
for(int mIfCounter = 0;mIfCounter<ArrayA.length-1;mIfCounter++){
mViewLabel.setText(ArrayA[mIfCounter]);
}
}
}, 1000);
}
});
}
}
the output doesn't change only showing one index and doesn't change to the next index
edit: it does work but on the output, it only shows the last index but still doesn't change
edit 2: Okay so I fixed it with new code and try some of you guys help this is new code
package com.example.testtingskripsi;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Arrays;
import java.util.List;
import java.util.Collections;
public class MainActivity extends AppCompatActivity {
public String [] ArrayA ={"80","81","82","83","84","85"};
public TextView mViewLabel;
boolean continueThread=true;
int count =0;
Thread t;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewLabel = (TextView) findViewById(R.id.textChanger);
List<String> list = Arrays.asList(ArrayA);
Collections.shuffle(list);
t = new Thread(){
@Override
public void run() {
while(continueThread){
try{
Thread.sleep(1000);
runOnUiThread(new Runnable() {
@Override
public void run() {
if(count<=6){
count++;
}else{
count--;
}
mViewLabel.setText(list.get(count));
}
});
}catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
}
public void BtnStart(View view){
t.start();;
}
public void BtnStop(View view){
continueThread=false;
}
}
it stop at end of array, so i want it to doing the loop until i pressed stop button any help