¿À·ù³»¿ëÀº 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.
ÀΰÍÀ» ±âÁØÀ¸·Î ÇÕ´Ï´Ù.
±âº»ÀûÀ¸·Î ¾Èµå·ÎÀ̵å´Â 1thread¿¡ ÇÏÀ§ threadµé(ºÎ¸ð-ÀÚ½Ä ¾²·¹µå)·Î µ¿ÀÛÇϸç UIº¯°æÀº ¹Ýµå½Ã ºÎ¸ð, Áï ÇöÀçÀÇ mainThread¿¡ ÀÇÇØ º¯°æµÇ¾î¾ß µË´Ï´Ù.
¸ÞÀξ²·¹µå ÀÌ¿ÜÀÇ °÷¿¡¼ ´Ù¸¥ thread¸¦ »ý¼ºÇÏ¿© ÀÌ°÷¿¡¼ ó¸®ÇÏ·Á°íÇϸé 100%¿À·ù¸¦ ¹ß»ý½Ãŵ´Ï´Ù.
¿¹¸¦µé¾î thread¸¦ ÀÌ¿ëÇØ ¸®½ºÆ®¸¦ °»½ÅÇÑ´Ù°íÇսôÙ. À̶§, adapter.notifyDataSetChanged()¸¦ »ç¿ëÇÏ°Ô µÇ¸é À§ÀÇ ¿¡·¯Äڵ带 ¸¸³ª°Ô µÇ½Ç °Ì´Ï´Ù.
ÇØ°áÇÏ´Â °¡Àå ½¬¿î¹æ¹ýÀ¸·Î´Â runOnUiThread¸¦ ÀÌ¿ëÇÏ¿© ÇÏ´Â ¹æ½Ä°ú handler.post·Î ÄÁÆ®·Ñ ÇÏ´Â ¹æ½ÄÀÌ ÀÖ½À´Ï´Ù.
runOnUiThread´Â
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
adapter.notifyDataSetChanged();
}
});
handler´Â
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try{
adapter.notifyDataSetChanged();
}catch (Exception e) {
// TODO: handle exception
}
}
}, 100);
µÎ°¡Áö ¹æº÷ÀÔ´Ï´Ù.
¹°·Ð °Ü¿ì ÀÌ°ÍÀ» ¼³¸íÇÏ°íÀÚ ±ÛÀ»¾´°Ç ¾Æ´ÏÁö¸¸ ±âº»ÀûÀÎ »ç¿ëÀº À§¿Í°°´Ù´Â°ÍÀÌÁÒ.
º»·ÐÀ¸·Î µ¹¾Æ°¡¼..
ºÐ¸í ¾²·¹µå¾È¿¡¼ À§ÀÇ runOnUiThread ȤÀº Handler·Î ó¸®Çؼ °»½ÅÇصµ À§ÀÇ ¿¡·¯¸¦ ¶Ç ¹ñ´Â°æ¿ì°¡
ÀÖ½À´Ï´Ù. ¸¹Àº °³¹ßÀÚµéÀÌ ÀÚÁÖ ¹üÇÏ´Â ¿À·ùÁß¿¡ Çϳª°¡ ¹Ù·Î ÀÌ ºÎºÐÀÔ´Ï´Ù.
À§ÀÇ µÎ°¡Áö¹æ¹ýÀ¸·Î Çߴµ¥µµ ¸¸¾à À§ÀÇ ¿À·ù°¡ ¹ß»ýÇÑ´Ù¸é
´ÙÀ½À» È®ÀÎÇغ¸½Ã±â ¹Ù¶ø´Ï´Ù.
1.ListViewÀÇ °ªÀÌ º¯°æµÇ¾î ¼öÁ¤ÇÏ°í adapter.notifyDataSetChanged(); »ç¿ëÇÏ°í
¹Ù·Î ¸®½ºÆ® ½ºÅ©·ÑÀ» ¼öÁ¤ÇÔ.
2. adapter.notifyDataSetChanged();¸¦ »ç¿ëÇÏ°í ¾Æ·¡¿¡ Çڵ鷯·Î Çѹø´õ ListView½ºÅ©·Ñ º¯°æ.
1¹øÀÇ °æ¿ì´Â ¿ì¼± list.setSelection°ú adapter.notifyDataSetChanged();´Â
°°ÀÌ »ç¿ëÇÒ ¼ö ¾ø´Ù´Â°É ¸»Çص帮°Ú½À´Ï´Ù.
¹°·Ð °°ÀÌ »ç¿ëÇÒ ¼ö´Â ÀÖ½À´Ï´Ù. ´Ù¸¸ ¾îµªÅÍÀÇ º¯°æÀÌ ¾ðÁ¦³¡³ª´ÂÁöµµ ¸ð¸£´Âµ¥ .setselectionÀ» ÀÌ¿ëÇØ
½ºÅ©·ÑÀ» ¼öÁ¤Çϸé Áߺ¹Çؼ getView¸¦ ¶Ç ºÒ·¯¿Í À§ÀÇ ¿¡·¯¸¦ ¹ñ½À´Ï´Ù.
°°Àº adapter.notifyDataSetChanged();¸¦ ¿©·¯¹ø ºÎ¸£´Â°Ç »ó°ü¾ø½À´Ï´Ù. ´Ù¸¸ adapter.notifyDataSetChanged();Àº °ªÀÌ º¯°æµÇ¾îÀÖÀ»°æ¿ì ÀÛµ¿ÇÏÁö¸¸ list.setSelectionÀº
¹«Á¶°Ç ÀÛµ¿ÇÕ´Ï´Ù. Â÷ÀÌÁ¡ÀÌ Å©¹Ç·Î ¹Ýµå½Ã ±¸ºÐÇÏ¿© »ç¿ëÇϽñ⠹ٶø´Ï´Ù.
µÎ¹ø°·Î´Â runonuithread¾È¿¡ ¶ÇÇѹø handler·ÎÀÇ ÄÁÆ®·ÑÀÔ´Ï´Ù.
runon¿¡¼ adapter.notifyDataSetChanged(); ¸¦ ºÎ¸£°í handler¿¡¼ setSelection ȤÀº
¸®½ºÆ®½ºÅ©·Ñ º¯°æÀ» ÇÏ½Ç °æ¿ì ¿¡·¯°¡ ¹ß»ýµÉ¼ö ÀÖ½À´Ï´Ù.
³»¿ëÀÌ Ãß°¡µÇ°Å³ª ¼öÁ¤µÈ °ÍÀ» º¸¿©ÁÖ°í ½Í´Ù¸é ±×³É ´ÜÇѹøÀÇ adapter.notifyDataSetChanged();
¸¦ ºÎ¸£¸éµÇ°í ¸¸¾à °»½Å+ ½ºÅ©·ÑÀ» ÇÏ°í ½Í´Ù¸é ´Ü¼øÈ÷ setSelection¸¸ ºÒ·¯ÁÖ¸é µË´Ï´Ù.
¾Æ¹«ÂÉ·Ï ¸®½ºÆ®ºä °»½ÅÇÏ´Ù°¡ À§ÀÇ ¹®Á¦°¡ °è¼Ó ¹ß»ýÇÑ´Ù¸é ¾îµð¼±°¡ Áߺ¹ °»½Å ¹× ¸®½ºÆ® ½ºÅ©·Ñ ÄÁÆ®·ÑÇÏ°í
ÀÖ´Â ºÎºÐÀº ÀÚÁ¦ÇÏ½Ã¸é¼ ÀÛ¾÷ÇϽñ⠹ٶø´Ï´Ù.
* ±ÛÀ» µî·ÏÇϽǶ§ ²À ÇÊ¿äÇÑ °æ¿ì°¡ ¾Æ´Ï¸é °³ÀÎÁ¤º¸¸¦ ³²±âÁö ¸¶¼¼¿ä ^^ (¿¬¶ôó,À̸ÞÀÏÁÖ¼Ò,Áֹεî·Ï¹øÈ£ µî)