英文:
Updating UI in Android [with queue]
问题
我有一个安卓应用程序(Java),目前有一个活动调用另一个类(Class A)。在Class A中,我有线程执行池,每秒运行一次,将值添加到队列中。
我希望从这个队列中获取值,然后使用queue.remove每秒更新我安卓活动中的一个文本视图。
如何在UI线程上每秒更新文本视图,使用来自Class A中这个队列的值?
英文:
I have and android app (java) and currently have an activity that calls a different class (Class A). in Class A i have thread executer pools that are running every second that add values to a Queue.
I want to get the values from this Queue to update a textview in my Android activity every second with queue.remove to get the value.
How can I Update the textview on the UI thread with every second with values from this queue in class a?
答案1
得分: 0
你必须将你的活动传递给那个 A 类。之后在传递的变量上调用 runOnUIThread() 函数,就像这里展示的那样。
链接:https://stackoverflow.com/a/11140429/11475673
传递的变量必须是 Activity 类,以供 runOnUiThread 函数使用。如果你想要访问 TextView,变量必须是你的活动类类型。
附注:在你的活动中使 textfield 成为公共变量。
英文:
You have to pass your activity to that A class. after that call runOnUIThread() function on that passed variable like it's here is shown.
https://stackoverflow.com/a/11140429/11475673 <br>
.The passed variable must be Activity class for the runOnUiThread function. And if you want to access the textview the variable must be your activity class type.
P.S. make textfield public variable in your activity.
答案2
得分: 0
import android.os.Handler;
import android.os.Looper;
public class UIActivity extends AppCompatActivity {
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
TextView textView = findViewById(R.id.text_view);
// defines a handler object attached to a UI-thread
handler = new ViewHandler(Looper.getMainLooper(), textView);
ClassA classA = new ClassA(this);
}
public synchronized void handleState(int state, String message) {
Message completeMessage = handler.obtainMessage(state, message);
completeMessage.sendToTarget();
}
}
ViewHandler.kt:
const val MESSAGE_CODE = 0
class ViewHandler(looper: Looper, private val textView: TextView): Handler(looper) {
override fun handleMessage(inputMessage: Message) {
val text: String = inputMessage.obj as String
when (inputMessage.what) {
MESSAGE_CODE -> textView.text = text // update TextView
// update other UI-elements
...
}
}
}
Update the TextView in ClassA:
public ClassA(UIActivity uiActivity) {
...
// other thread
queue.add(newValue) // invoke every second
String oldValue = queue.remove();
uiActivity.handleState(MESSAGE_CODE, s);
}
英文:
In addition to oto's answer, You can also use Handler/Looper:
import android.os.Handler;
import android.os.Looper;
public class UIActivity extends AppCompatActivity {
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
TextView textView = findViewById(R.id.text_view);
// defines a handler object attached to a UI-thread
handler = new ViewHandler(Looper.getMainLooper(), textView);
ClassA classA = new ClassA(this);
}
public synchronized void handleState(int state, String message) {
Message completeMessage = handler.obtainMessage(state, message);
completeMessage.sendToTarget();
}
}
ViewHandler.kt:
const val MESSAGE_CODE = 0
class ViewHandler(looper: Looper, private val textView: TextView): Handler(looper) {
override fun handleMessage(inputMessage: Message) {
val text: String = inputMessage.obj as String
when (inputMessage.what) {
MESSAGE_CODE -> textView.text = text // update TextView
// update other UI-elements
...
}
}
}
Update the TextView in ClassA:
public ClassA(UIActivity uiActivity) {
...
// other thread
queue.add(newValue) // invoke every second
String oldValue = queue.remove()
uiActivity.handleState(MESSAGE_CODE, s);
}
专注分享java语言的经验与见解,让所有开发者获益!
评论