Reputation: 1826
public Button stb;
static int cnt=0;
public ArrayList<RadioButton> Butgrp1 = new ArrayList<RadioButton>();
Timer myt;
TimerTask t;
stb.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
myt.schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("Entering run");
Handler h=new Handler();
h.post(new Runnable() {
public void run() {
// TODO Auto-generated method stub
runOnUiThread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
Butgrp1.get(cnt).setChecked(true);
cnt=cnt+1;
if(cnt>4)
cnt=0;
if(cnt>0)
// Butgrp1.get(cnt-1).setChecked(false);
System.out.println(cnt);
}
});
}
});
//rg.getChildAt(cnt).setPressed(true);
}
},1000,2000);
I need to access a group of radio buttons on the ui and set it as checked at regular intervals, but i keep getting different errors, i realized i must use a handler, but its still not working...can anyone please tell me where i am going wrong....am a newbie and am trying out stuff to understand the working better...please help...
Upvotes: 1
Views: 734
Reputation: 1312
You can try to use your own Handler instead of Timer and timed taks.
RefreshHandler mHandler = new RefreshHandler();
With:
class RefreshHandler extends Handler
{
@Override
public void handleMessage(Message msg)
{
postYourElements();
}
public void sleep(long delayMillis)
{
this.removeMessages(0);
sendMessageDelayed(obtainMessage(0), delayMillis);
}
}
And than use the function:
private void postYourElements()
{
runOnUiThread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
Butgrp1.get(cnt).setChecked(true);
cnt=cnt+1;
if(cnt>4)
cnt=0;
if(cnt>0)
// Butgrp1.get(cnt-1).setChecked(false);
System.out.println(cnt);
}
});
mHandler.sleep(TimerIntervallInMs);
}
To start the Handler just call the postYourElements()
function under onClick Method.
I'm not sure if this works for you but you can try.
Upvotes: 1
Reputation: 12717
youractivityname.this.runOnUiThread(new Runnable() {
public void run() {
Butgrp1.get(cnt).setChecked(true);
cnt=cnt+1;
if(cnt>4)
cnt=0;
if(cnt>0)
// Butgrp1.get(cnt-1).setChecked(false);
System.out.println(cnt);
}
});
Upvotes: 0