Button supposed to click 5 times through an string array, stop 5 secs and then continue(clicking 5 times) CONSISTENTLY, ERROR

Button supposed to click 5 times through an string array, stop 5 secs and then continue(clicking 5 times) CONSISTENTLY, ERROR



My problem is I cant get my button to consistently click 5 times through a string array which is displayed in a TextView ,
whenever the maxclicks(5) and currentnumber get to 5 it stops working , Ive been trying to create if conditions to work around it, well somehow I have to manipulate my currentnumber to NOT be 5 because IF maxclicks == currentnumber my button is enabled.



In the Code below it stops just afte the first time of clicking 5 times.



so here is the Code :


public class MainActivity extends AppCompatActivity


int currentnumber = 0;
int mod = 5;
TextView display = findViewById(R.id.tx);
Handler handler = new Handler();
int delay = 5000;
int maxclicks = list.length;



@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);



Resources res = getResources();
final String list = res.getStringArray(R.array.xyz);
final Button next_button = findViewById(R.id.next_btn);


((TextView) findViewById(R.id.tx)).setText(list[currentnumber]);

next_button.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
if(currentnumber == maxclicks)
currentnumber = 0;

if (currentnumber % mod == 0)
next_button.setEnabled(false);
handler.postDelayed(new Runnable()
@Override
public void run()
//the button will unlock after the delay specified
next_button.setEnabled(true);
currentnumber++;


, delay);

else
display.setText(list[currentnumber]);
currentnumber++;


);









Welcome to SO. we need to know what does "ERROR" mean for you, in this specific case.
– Kling Klang
Aug 22 at 18:43






It's not clear exactly what you are trying to do. setting maxclicks to 4 doesn't do much since the next line will then set it to 5.
– theblitz
Aug 22 at 18:46





So with Error I mean that my Button wont click 5 times if I start clicking at the 6th string (after the timeout I put to the button) and I well I dont really know why but I assume its because the number in the array and maxclicks is the same Sorry im bad at explaining what I basically want is my button to always click 5 times through an string array each time after its timed out and up again(which I did already).
– Karlay
Aug 22 at 18:50






In your code snippet, currentNumber will always stay zero. On the other hand maxclicks will never be zero: you can't change it from 5 to 4 since 5 != 0, so in fact maxclicks will always be 5. Maybe there is a typo or two in the code you posted?
– 0X0nosugar
Aug 22 at 18:55





I think you're looking for something like "currentNumber++" in your onClick() method. I'm making a lot of assumptions, but I'm guessing you're trying to allow for 5 clicks, but then disable the button after that?
– Caerulius
Aug 22 at 19:01




1 Answer
1



welcome to SO :) I did my best to understand on your explanation,so this is my solution for your problem and don't forget you can make your vars global to avoid final and one element array thing:


public class MainActivity extends AppCompatActivity
private int currentnumber,mod,delay,Curclicks;
private TextView display;
private Handler handler;
private Button next_button;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);

//Binding
display = findViewById(R.id.tx);
next_button = findViewById(R.id.next_button);
//getResources
Resources res = getResources();

//getting the data ready
String list = "1","2","3","4","5","6","7";
//assign vars
handler = new Handler();
currentnumber = 0;
Curclicks=0;
mod = 5;
delay = 5000;
//initial view
next_button.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)

if(currentnumber == list.length)
currentnumber = 0;

if (Curclicks == mod-1)
next_button.setEnabled(false);
display.setText(list[currentnumber]);
currentnumber++;


handler.postDelayed(new Runnable()
@Override
public void run()
//the button will unlock after the delay specified
next_button.setEnabled(true);
Curclicks = 0;

, delay);

else
display.setText(list[currentnumber]);
currentnumber++;


Curclicks++;



);






Comments are not for extended discussion; this conversation has been moved to chat.
– Samuel Liew
Aug 26 at 2:04






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)