RecyclerView更新
RecyclerView Update
我有一个要更新的回收站视图 it.It 喜欢从服务器获取 post 数据并将其添加到数组列表并使用 notifyDataSetChanged() 更新适配器 function.but 当它想要更新时出现此错误。
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:7905)
at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1276)
at android.view.View.requestLayout(View.java:22139)
at android.view.View.requestLayout(View.java:22139)
at android.view.View.requestLayout(View.java:22139)
at android.view.View.requestLayout(View.java:22139)
at android.view.View.requestLayout(View.java:22139)
at android.view.View.requestLayout(View.java:22139)
at android.view.View.requestLayout(View.java:22139)
at android.support.v7.widget.RecyclerView.requestLayout(RecyclerView.java:3970)
at android.support.v7.widget.RecyclerView$RecyclerViewDataObserver.onChanged(RecyclerView.java:5060)
at android.support.v7.widget.RecyclerView$AdapterDataObservable.notifyChanged(RecyclerView.java:11540)
at android.support.v7.widget.RecyclerView$Adapter.notifyDataSetChanged(RecyclerView.java:6762)
at com.example.MainActivity.run(MainActivity.java:179)
at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:257)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:784)
这是我的代码
AsyncTask.execute(new Runnable() {
@Override
public void run() {
while (true) {
String name = getHtml.getUrlContent("URL" + something);
something += 1;
list.add(new Model(Model.IMAGE_TYPE, name));
adapter.notifyDataSetChanged();
}
}
});
我认为错误是由于 AsyncTask 而发生的,然后我将其更改为 runOnUiThread() 函数,但此方法首先获取所有数据,然后创建视图,因为该应用程序启动时间太长。
我能做什么?
你的方法不是最好的,可能有很多问题,但你可以使用
runOnUiThread(new Runnable() {
void run() {
adapter.notifyDataSetChanged();
}
});
其他解决方案是 class 从 AsyncTask 扩展它,覆盖 doInBackground 和 post 执行方法。
在 doInBackground() 上获取数据并更新 ui onPostExecute()
Guide: https://developer.android.com/reference/android/os/AsyncTask
我有一个要更新的回收站视图 it.It 喜欢从服务器获取 post 数据并将其添加到数组列表并使用 notifyDataSetChanged() 更新适配器 function.but 当它想要更新时出现此错误。
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:7905)
at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1276)
at android.view.View.requestLayout(View.java:22139)
at android.view.View.requestLayout(View.java:22139)
at android.view.View.requestLayout(View.java:22139)
at android.view.View.requestLayout(View.java:22139)
at android.view.View.requestLayout(View.java:22139)
at android.view.View.requestLayout(View.java:22139)
at android.view.View.requestLayout(View.java:22139)
at android.support.v7.widget.RecyclerView.requestLayout(RecyclerView.java:3970)
at android.support.v7.widget.RecyclerView$RecyclerViewDataObserver.onChanged(RecyclerView.java:5060)
at android.support.v7.widget.RecyclerView$AdapterDataObservable.notifyChanged(RecyclerView.java:11540)
at android.support.v7.widget.RecyclerView$Adapter.notifyDataSetChanged(RecyclerView.java:6762)
at com.example.MainActivity.run(MainActivity.java:179)
at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:257)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:784)
这是我的代码
AsyncTask.execute(new Runnable() {
@Override
public void run() {
while (true) {
String name = getHtml.getUrlContent("URL" + something);
something += 1;
list.add(new Model(Model.IMAGE_TYPE, name));
adapter.notifyDataSetChanged();
}
}
});
我认为错误是由于 AsyncTask 而发生的,然后我将其更改为 runOnUiThread() 函数,但此方法首先获取所有数据,然后创建视图,因为该应用程序启动时间太长。 我能做什么?
你的方法不是最好的,可能有很多问题,但你可以使用
runOnUiThread(new Runnable() {
void run() {
adapter.notifyDataSetChanged();
}
});
其他解决方案是 class 从 AsyncTask 扩展它,覆盖 doInBackground 和 post 执行方法。 在 doInBackground() 上获取数据并更新 ui onPostExecute()
Guide: https://developer.android.com/reference/android/os/AsyncTask