0

我用了这篇文章 ,我有一个问题。

在 PageFragment (扩展片段)中,我有一些从数据库(sqlite)加载的字段,这个片段有一个按钮,点击后更新数据库中的表。我想重新加载片段以显示更新的数据(无需滑动)。我知道 android 将状态保存在内存中,但我需要在不滑动的情况下显示更新的字段。

我尝试使用 POSITION_NONE、setOffscreenPageLimit、notifyDataSetChanged、分离、附加等。但它们都不起作用,我对它们感到困惑。

4

1 回答 1

1

一种更合适的方法是将初始更新逻辑放在单独的方法中,并在更新数据库后调用该方法。

public void onCreateView(){
    //your view creation and inflation
    //call populateUI after inflation here
}

public void onResume(){
    //or here
}

//take data from someplace and populate your UI may be from database
public void populateUI(){
  //get values and set fields 
}

public void onClick(View v){
    //the button click
    //update db
    //then
    populateUI();
}
于 2016-05-29T10:31:45.147 回答