Reputation: 123
Help to get the data (tdate) from the thread t
in main activity
...
btn_in = (Button) findViewById(R.id.btn_insert);
btn_in.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
registerUser();
}
});
Thread t = new Thread() {
@Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(1000);
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView tdate = (TextView) findViewById(R.id.date);
long date = System.currentTimeMillis();
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd yyyy\nhh-mm-ss a");
String dateString = sdf.format(date);
tdate.setText(dateString);
}
});
}
} catch (InterruptedException e) {
}
}
};
t.start();
}
to the class registerUser...(tdate)
I am new to android and java...
anyone help me to learn.
private void registerUser() {
String datetime = tdate.getText().toString().trim().toLowerCase();
register(datetime);
}
Upvotes: 0
Views: 168
Reputation: 23
to complete with other answers you can try :
TextView tdate;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tdate = (TextView)findViewById(R.id.date);
btn_in = (Button) findViewById(R.id.btn_insert);
btn_in.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
registerUser();
}
});
createThread();
}
private void createThread(){
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
run();
}
},1000);
}
private void run(){
long date = System.currentTimeMillis();
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd yyyy\nhh-mm-ss a");
String dateString = sdf.format(date);
tdate.setText(dateString);
}
Upvotes: 0
Reputation: 634
If I'm understanding you correctly, just call the function itself with the proper arguments:
public void run() {
tdate = (TextView) findViewById(R.id.date);
date = System.currentTimeMillis();
sdf = new SimpleDateFormat("MMM dd yyyy\nhh-mm-ss a");
dateString = sdf.format(date);
tdate.setText(dateString);
registerUser(datetime); // <- Why can't you just do this?
}
And registerUser() should be like this:
private void registerUser(String datetime) {
register(datetime);
}
Another option would be to make a class that receives data and has a reference to the main activity. When you finish the thread, call a function within it, and its job would be to simply call a function on the Main Activity. It's a "receiver" class, so to speak.
Also, you should avoid using sleep(1000). Use this instead:
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Do something after 1000 ms
}
}, 1000); // specify the waiting time here
It's much cleaner. Let me know how it worked out.
Upvotes: 0
Reputation: 23
If your thread is in an other Java Class, you can create a new Interface like :
public interface OnTimeReceived {
void onTimeReceived(String time);
}
on your MainActivity , add implements to this listener, implements the method and then set the text to your textView in the callback.
Then on the thread just call "mListener.onTimeReceived(dateString)" to update your view.
ex :
public class MainActivity implements OnTimeReceived {
...
@Override
public void onTimeReceived(String date) {
mTextView.setText(date);
}
...
}
When you create your thread class, give the listener from your activity in constructor.
Upvotes: 1