ListView 加载数据时 触摸报错

问题起因:

在做一个从sd卡中加载数据显示在ListView中,由于数据可能比较多,考虑到用户体验,就使用AsyncTask来异步加载,数据一条一条的添加至ListView中.

开始数据比较少的时候,基本上瞬间完成所有加载,没发现这个问题,后来把测试数据添加至一百来条,问题就出现了,当我的数据还在加载的时候,

用手在ListView上做Touch操作的时候,程序报错,报错信息如下:

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. [in ListView(2131427383, class android.widget.ListView) with Adapter(class com.yinchi.waveform.PatientSelectAdapter)]

 

走的弯路:

开始以为是由于多线程引起的,于是使用synchronized给ListView加上同步锁,测试后,问题依旧

在ListView刷新前后使用listview.setVisibility(View.GONE)和listview.setVisibility(View.VISIBLE)也不行

 

解决方法:

后来最后发现了有人说是IME和界面冲突造成的,就解释了这么一句话,反正我是不太懂,解决代码如如下:

  Activity activity = (Activity) mContext;
  View currentFocus = activity.getCurrentFocus();
  if (currentFocus != null) {
   currentFocus.clearFocus();
  }

代码放在适配器的getView方法的开始

哎,问题是解决了,但是知其然,不知其所以然啊,希望有朋友能给出具体的说法

 

posted @ 2013-10-25 09:09  trenail  阅读(402)  评论(1编辑  收藏  举报