[問題] java.lang.IllegalStateException
各位前輩大家好,小弟接觸android沒多久還是位新手,
目前在做作品時遇到了一個瓶頸,是在ListView.setAdapter()
短時間內大量重複更新時跳出以下錯誤:
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. [in ListView(2131296313, class android.widget.ListView) with
Adapter(class com.example.avalon.PlayerAdapter)]
以下是小弟寫的Adapter的程式碼:
public class PlayerAdapter extends BaseAdapter {
private LayoutInflater playersInflater;
public List<Player> playerslist;
public PlayerAdapter(Context context, List<Player> playerslist) {
// TODO 自動產生的建構子 Stub
if(playerslist!=null){
playersInflater=LayoutInflater.from(context);
this.playerslist=playerslist;
PlayerAdapter.this.notifyDataSetChanged();
}
}
@Override
public int getCount() {
// TODO 自動產生的方法 Stub
return (playerslist==null)? 0 : playerslist.size();
}
@Oerride
public Object getItem(int position) {
// TODO 自動產生的方法 Stub
return playerslist.get(position);
}
@Override
public long getItemId(int position) {
// TODO 自動產生的方法 Stub
return playerslist.indexOf(getItem(position));
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO 自動產生的方法 Stub
if(playerslist.size()!=0){
try{
convertView=playersInflater.inflate
(R.layout.players_list_item, null);
ImageView iv_Players_Status=(ImageView)
convertView.findViewById(R.id.IV_Players_Status);
TextView tv_Players_Order=(TextView)
convertView.findViewById(R.id.TV_Players_Order);
TextView tv_Players_Anonymous=(TextView)
convertView.findViewById(R.id.TV_Players_Anonymous);
iv_Players_Status.setImageResource
(playerslist.get(position).getStatus());
tv_Players_Order.setText(String.valueOf
(playerslist.get(position).getOrder()).toString());
tv_Players_Anonymous.setText
(playerslist.get(position).getAnonymity());
}catch(Exception e){
e.printStackTrace();
Log.e("text","PlayerAdapter="+e.toString());
}
return convertView;
}else{
convertView=playersInflater.inflate(R.layout.null_layout, null);
return convertView;
}
}
}
這幾天爬文有看到getCount()內寫成
return (playerslist==null)? 0 : playerslist.size();
還有在更新資料時呼叫notifyDataSetChanged()通知系統更新Adapter
可是在大量更新時app還是會崩潰跳出此錯誤
有聽人說可以在更新時隱藏按鈕,到更新完後在顯示按鈕
但是小弟是寫socket連伺服器,所以可以多人更新所以以上方法可能無法解決
還希望各位可以協助解決,謝謝~
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.175.134.228
※ 文章網址: https://www.ptt.cc/bbs/AndroidDev/M.1444146266.A.E50.html
推
10/07 01:20, , 1F
10/07 01:20, 1F
→
10/07 01:20, , 2F
10/07 01:20, 2F
→
10/07 01:20, , 3F
10/07 01:20, 3F
→
10/07 01:28, , 4F
10/07 01:28, 4F
→
10/07 01:28, , 5F
10/07 01:28, 5F
→
10/07 01:28, , 6F
10/07 01:28, 6F
→
10/07 12:37, , 7F
10/07 12:37, 7F
→
10/07 12:38, , 8F
10/07 12:38, 8F
→
10/07 12:41, , 9F
10/07 12:41, 9F
→
10/07 12:43, , 10F
10/07 12:43, 10F
→
10/07 12:47, , 11F
10/07 12:47, 11F
→
10/07 12:48, , 12F
10/07 12:48, 12F
→
10/07 12:48, , 13F
10/07 12:48, 13F
→
10/07 13:03, , 14F
10/07 13:03, 14F
→
10/07 18:36, , 15F
10/07 18:36, 15F
→
10/07 18:37, , 16F
10/07 18:37, 16F
→
10/07 18:37, , 17F
10/07 18:37, 17F
→
10/07 22:04, , 18F
10/07 22:04, 18F
→
10/07 22:05, , 19F
10/07 22:05, 19F
→
10/07 22:06, , 20F
10/07 22:06, 20F
→
10/07 22:06, , 21F
10/07 22:06, 21F
→
10/07 22:10, , 22F
10/07 22:10, 22F
→
10/07 22:11, , 23F
10/07 22:11, 23F
→
10/07 22:11, , 24F
10/07 22:11, 24F
→
10/07 23:01, , 25F
10/07 23:01, 25F
→
10/07 23:02, , 26F
10/07 23:02, 26F
→
10/07 23:04, , 27F
10/07 23:04, 27F
→
10/07 23:04, , 28F
10/07 23:04, 28F
→
10/07 23:06, , 29F
10/07 23:06, 29F
→
10/07 23:06, , 30F
10/07 23:06, 30F
→
10/07 23:16, , 31F
10/07 23:16, 31F
→
10/07 23:16, , 32F
10/07 23:16, 32F
→
10/07 23:49, , 33F
10/07 23:49, 33F
→
10/07 23:51, , 34F
10/07 23:51, 34F
→
10/07 23:51, , 35F
10/07 23:51, 35F
→
10/07 23:54, , 36F
10/07 23:54, 36F
→
10/07 23:57, , 37F
10/07 23:57, 37F
→
10/07 23:58, , 38F
10/07 23:58, 38F
→
10/07 23:58, , 39F
10/07 23:58, 39F