2013-04-02 115 views
0

我在做adapter.notifyDataSetChanged()listView.invalidateViews()我得到 接到电话后,如果条件说“blabla”变为true。但是我的用户界面还没有更新。收到广播接收方意向后没有反映变化

public class UserFragment extends Fragment{ 

public class FragmentReceiver extends BroadcastReceiver { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      if(getSetting("CurrentView","").equalsIgnoreCase("blabla")){ 
       adapter.notifyDataSetChanged(); 
       listView.invalidateViews(); //this is not updating the UI. 

} 
} 

注:我试着拨打同一UserFragment.java再次更换相同的片段来更新改变,但是这虽然更新的用户界面,但在onReceive给予者除外IllegalStateException: Can not perform this action after onSaveInstanceState

我的代码的其余部分方法 -

UserFragment firstFragment = new UserFragment(); 
getSupportFragmentManager().beginTransaction() 
        .replace(R.id.headlines_fragment, firstFragment) 
        .addToBackStack(null).commit(); //IllegalStateException: Can not perform this action after onSaveInstanceState 

任何想法?

+0

使这被显示在适配器确保数据改变你叫notifydatasetchanged的适配器之前。 –

+0

是的,只有当我转到其他视图然后再次返回到此视图,但不是立即在同一视图内时,它才会发生变化。 –

+0

尝试再次调用onCreate(bundle),这是一个肮脏的技巧,但工程。 –

回答

0

试试这个

public class UserFragment extends Fragment{ 
public class FragmentReceiver extends BroadcastReceiver { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      if(getSetting("CurrentView","").equalsIgnoreCase("blabla")){ 
       adapter.notifyDataSetChanged(); 
       listView.invalidateViews(); //this is not updating the UI. 
       UserFragment.this.onCreate(savedInstanceState); 

} 
} 
+0

如何获取'savedInstanceState',因为它无法解析为该地点的变量。 –

+0

多数民众赞成如何你这样做,使一个全局变量捆绑包;然后在的onCreate \t @Override \t公共无效的onCreate(捆绑savedInstanceState){ \t \t // TODO自动生成方法存根 \t \t super.onCreate(savedInstanceState); bundle = savedInstanceState; } 然后像UserFragment.this.onCreate(bundle)一样调用onCreate; –

+0

这不是即时更新我的​​用户界面。我必须重新回来反映这些变化。 –