java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive
2014-12-06 14:01
375 查看
我们知道在子线程中更新UI,会报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,这个错误相信大家都遇见过,在此记录下,写一个简单的Demo,
the UI thread,这个错误相信大家都遇见过,在此记录下,写一个简单的Demo,
package com.example.listviewdemo; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; public class MainActivity extends Activity { private ListView lv; private List<String> datas; private MyAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); adapter = new MyAdapter(); lv = (ListView) findViewById(R.id.lv); lv.setAdapter(adapter); datas = new ArrayList<String>(); new Thread(){ public void run() { for(int i=0;i<10000;i++){ datas.add("测试:"+i); adapter.setDatas(datas); adapter.notifyDataSetChanged(); } } }.start(); } class MyAdapter extends BaseAdapter{ private List<String> datas; public void setDatas(List<String> datas) { this.datas = datas; } @Override public int getCount() { return (datas!=null&&datas.size()>0)?datas.size():0; } @Override public Object getItem(int arg0) { return datas.get(arg0); } @Override public long getItemId(int arg0) { return arg0; } @Override public View getView(int arg0, View arg1, ViewGroup arg2) { TextView tv = new TextView(MainActivity.this); tv.setText(datas.get(arg0)); return tv; } }
可以看到adapter.notifyDataSetChanged();这行代码是在子线程中,所以导致上述错误,解决起来就很简单,在这不多说
相关文章推荐
- java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive
- java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive
- java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive
- java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification
- java.lang.IllegalStateException:The content of the adapter has changed but ListView did not receiv问题
- java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receiv
- java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive
- java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive
- java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive
- java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive
- java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive
- The content of the adapter has changed but ListView did not receive a notification,java.lang.IndexOu
- java.lang.IllegalStateException: The content of the adapter has changed but ListView...的问题
- The content of the adapter has changed but ListView did not receive a notification
- 处理The content of the adapter has changed but ListView did not receive a notification异常
- Android问题3-ListView Adapter(The content of the adapter has changed but ListView did not receive a n)
- The content of the adapter has changed but ListView did not receive a notification.
- The content of the adapter has changed but ListView did not receive a notification make sure the con
- The content of the adapter has changed but ListView did not receive a notification.
- The content of the adapter has changed but ListView did not receive a notification 错误的解决方案