Reputation: 1725
I am currently working on my first Android App for Uni. I am slowly getting there but I am stuck on a certain section.
The coursework requires a separate thread to that of the UIthread. My idea is the user can set their names in Name_edit.java then when they go to board.java (a different activity) the two textviews are now displaying what was entered
In the Name_edit.xml I have two edittexts.
In the board.java I have two textviews (currently set to P1 and P2 respectively). In the OnCreate() I am currently working on a handler to get the two values from (name_edit) the two edittexts and set this to the textviews. I believe this will require two handlers (one for each value). In the board.java I have done the standard findViewById.
Any help on Handlers and Threads would be helpful.
I would post my handler code but it is current changing constantly. What I am working on is
handler = new Handler() {
public void handleMessage (Message msg) {
TextViewP1.setText(msg)
}
};
Note msg is currently not set to an edittext from name_edit
Upvotes: 3
Views: 1579
Reputation: 7334
Have you tried runOnUiThread()
? UI elements can only be changed from the UI thread and your handler here runs on it's own thread.
handler = new Handler() {
public void handleMessage (Message msg) {
((Activity)context).runOnUiThread(new Runnable() {
public void run() {
TextViewP1.setText(msg);
}
});
}
}
where context
can be replaced with [class name].this
if your handler is in your Activity class.
Docs: http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable)
Upvotes: 1