展会信息港展会大全

android开发 解决listview出现线程更新错误问题
来源:互联网   发布日期:2015-11-26 09:36:33   浏览:1627次  

导读: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 ......

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.

是不是你在用 adapter.notifyDataSetChanged() 刷新listview的时候,如果当前listview还未来得及刷新,你就去触摸listview,就会导致跳出出错信息呢?

解决办法是,通过设置listview.setVisibility(View.VISIBLE/View.GONE)。

在 adapter.notifyDataSetChanged() 之前调用listview.setVisibility(View.GONE);在adapter.notifyDataSetChanged() 之后调用listview.setVisibility(View.VISIBLE)

public class ListViewStressTest extends ListActivity {

ArrayAdapter<String> adapter;

ListView list;

AsyncTask<Void, String, Void> task;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

this.adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);

this.list = this.getListView();

this.list.setAdapter(this.adapter);

this.task = new AsyncTask<Void, String, Void>() {

Random r = new Random();

int[] delete;

volatile boolean scroll = false;

@Override

protected void onProgressUpdate(String... values) {

if(scroll) {

scroll = false;

doScroll();

return;

}

if(values == null) {

doDelete();

return;

}

doUpdate(values);

if(ListViewStressTest.this.adapter.getCount() > 5000) {

ListViewStressTest.this.adapter.clear();

}

}

private void doScroll() {

if(ListViewStressTest.this.adapter.getCount() == 0) {

return;

}

int n = r.nextInt(ListViewStressTest.this.adapter.getCount());

ListViewStressTest.this.list.setSelection(n);

}

private void doDelete() {

int[] d;

synchronized(this) {

d = this.delete;

}

if(d == null) {

return;

}

for(int i = 0 ; i < d.length ; i++) {

int index = d[i];

if(index >= 0 && index < ListViewStressTest.this.adapter.getCount()) {

ListViewStressTest.this.adapter.remove(ListViewStressTest.this.adapter.getItem(index));

}

}

}

private void doUpdate(String... values) {

for(int i = 0 ; i < values.length ; i++) {

ListViewStressTest.this.adapter.add(values[i]);

}

}

private void updateList() {

int number = r.nextInt(30) + 1;

String[] strings = new String[number];

for(int i = 0 ; i < number ; i++) {

strings[i] = Long.toString(r.nextLong());

}

this.publishProgress(strings);

}

private void deleteFromList() {

int number = r.nextInt(20) + 1;

int[] toDelete = new int[number];

for(int i = 0 ; i < number ; i++) {

int num = ListViewStressTest.this.adapter.getCount();

if(num < 2) {

break;

}

toDelete[i] = r.nextInt(num);

}

synchronized(this) {

this.delete = toDelete;

}

this.publishProgress(null);

}

private void scrollSomewhere() {

this.scroll = true;

this.publishProgress(null);

}

@Override

protected Void doInBackground(Void... params) {

while(true) {

int what = r.nextInt(3);

switch(what) {

case 0:

updateList();

break;

case 1:

deleteFromList();

break;

case 2:

scrollSomewhere();

break;

}

try {

Thread.sleep(0);

} catch(InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

};

this.task.execute(null);

}

}

赞助本站

人工智能实验室

相关热词: 线程更新

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港