如何更新 UI inside volley response listener ?需要 runOnUiThread 吗?还是 Onresponse 自己控制 UI 线程?

How to Update UI inside volley response listener ? is runOnUiThread needed? or does Onresponse takes the control to UI Thread by itself?

在使用 volley 库时,如果我想更新响应侦听器中的列表视图适配器,是否应该使用 runOnUiThread 来完成?或者它已经在 UiThread 中了?

当适配器尝试修改视图对象时,您可能会遇到以下异常:

android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

因此,为了安全起见,请执行以下操作:

geyActivity().runOnUiThread(new Runnable() {

        @Override
        public void run() {
            adapter.notifyDataSetChanged();
        }
    });

从截击响应调用处理程序

@Override
public void onResponse(String response) {
     Log.d("Response is: ", response);
     rui = response; // i put response to public String variable rui
     myHandler.post(updateRunnable);
     parseJSON(response);
}

并在 onCreate 中 Activity

myHandler = new Handler();

updateRunnable = new Runnable() {
    public void run() {
       //call the activity method that updates the UI
       updateUI();
    }
};

以及您在 updateUI 上的更新

public void updateUI(){
    t.setText(rui);
}

并确保您的小部件 ID 确实存在。

<TextView
    android:id="@+id/konten"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"