关于数据不同步的问题
ListView
1.报错: java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make sure your adapter calls notifyDataSetChanged() when its content changes.
2. 出现时机:点击
RecyclerView
1.报错 java.lang.IndexOutOfBoundsException: Inconsistency detected
2.出现时机:滑动
复现步骤Demo:
package com.example.administrator.recycleviewmultidemo.demo;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.example.administrator.recycleviewmultidemo.R;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity {
private RecyclerView rv;
private ListView lv;
private List<Object> beans = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
beans.add(null);
beans.add(null);
beans.add(null);
beans.add(null);
beans.add(null);
beans.add(null);
beans.add(null);
beans.add(null);
beans.add(null);
beans.add(null);
beans.add(null);
beans.add(null);
beans.add(null);
doRecycleView();
// doListView();
}
private void doListView() {
lv = findViewById(R.id.lv);
lv.setAdapter(new BaseAdapter() {
@Override
public int getCount() {
return beans.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv = new TextView(MainActivity.this);
tv.setHeight(200);
tv.setText("hhhhhdddddddddddddddddddd");
return tv;
}
});
lv.postDelayed(new Runnable() {
@Override
public void run() {
beans.clear();
}
},3000);
}
private void doRecycleView() {
rv = findViewById(R.id.rv);
rv.setLayoutManager(new LinearLayoutManager(this));
rv.setAdapter(new MyRecycleAdapter());
rv.postDelayed(new Runnable() {
@Override
public void run() {
beans.clear();
}
},3000);
}
class MyRecycleAdapter extends RecyclerView.Adapter{
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
TextView tv = new TextView(MainActivity.this);
tv.setHeight(200);
tv.setText("hhhhhdddddddddddddddddddd");
return new RecyclerView.ViewHolder(tv){
};
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return beans.size();
}
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ListView
android:visibility="gone"
android:id="@+id/lv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
分别使用上面的doListView() 和doRecycleView() 可以复现(注意修改xml)
解决:1.避免数据不同步,数据改变要通知
2.深拷贝
3.不要直接改变adapter的数据,使用临时变量存储过渡