2

我有一个ListFragment我想在其中保存其数据并将其onSaveInstanceState恢复的地方onCreateView,但是发生了一些奇怪的事情。

我使用此代码保存数据:

private ArrayList<TuplaFacturaWS> facturas;

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    System.out.println("save");

    //TuplaUsuario.usuario
    savedInstanceState.putString("usuario", myApplication.getUsuarioActual().getUsuario());
    //TuplaUsuario.password
    savedInstanceState.putString("password", myApplication.getUsuarioActual().getPassword());
    //TuplaUsuario.cups
    ArrayList<TuplaCupsWS> arrayCups = myApplication.getUsuarioActual().getCups();
    savedInstanceState.putInt("numCups", arrayCups.size());
    for(int cups = 0; cups < arrayCups.size(); cups++) {
        //TuplaUsuario.cups.CUPS, TuplaUsuario.cups.Tarifa, TuplaUsuario.cups.Direccion
        savedInstanceState.putString("cups"+cups, arrayCups.get(cups).getCups());
        savedInstanceState.putString("tarifa"+cups, arrayCups.get(cups).getTarifa());
        savedInstanceState.putString("direccion"+cups, arrayCups.get(cups).getDireccion());
        //TuplaUsuario.cups.facturas
        System.out.println("N facturas on save: "+arrayCups.get(cups).getFacturas().size());
        ArrayList<TuplaFacturaWS> arrayFacturas = arrayCups.get(cups).getFacturas();
        savedInstanceState.putInt("numFacturas"+cups, arrayFacturas.size());
        for(int factura = 0; factura < arrayFacturas.size(); factura++) {
            savedInstanceState.putString("factura"+factura,
                    arrayFacturas.get(factura).getFactura());
            savedInstanceState.putDouble("importe"+factura, 
                    arrayFacturas.get(factura).getImporte());
            savedInstanceState.putLong("fecha"+factura, 
                    arrayFacturas.get(factura).getFecha().getTime());
            savedInstanceState.putString("plan"+factura,    
                    arrayFacturas.get(factura).getPlan());
            savedInstanceState.putString("estado"+factura,  
                    arrayFacturas.get(factura).getEstado());
            savedInstanceState.putString("pdf"+factura, 
                    arrayFacturas.get(factura).getPdf());
            savedInstanceState.putString("anexo"+factura,   
                    arrayFacturas.get(factura).getAnexo());
        }
    }
    //TuplaUsuario.cupsActual
    savedInstanceState.putInt("cupsActual", myApplication.getUsuarioActual().getCupsActual());
}

情况一

public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
{
    View view = inflater.inflate(R.layout.activity_facturas, container, false);
    // Get the application instance
    myApplication = (MyApplication)getActivity().getApplication();
    facturas = myApplication.getFacturas();

    System.out.println("num facturas on create view: "+facturas.size());

    //...
}

会发生什么:

  • 第一次加载视图时,视图显示 5 个“facturas”。onCreateView在创建视图上打印“num facturas: 5”。
  • 改变方向时,视图显示相同的 5 个“事实”。onSaveInstanceState打印“N facturas on save: 5”,并onCreateView打印“num facturas on create view: 5”。-> 这是正确的。
  • 当再次改变方向时,视图显示没有“factura”。onSaveInstanceState打印“N facturas on save: 0”,并onCreateView打印“num facturas on create view: 0”。-> 这是错误的。

情况2

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState)
{
    View view = inflater.inflate(R.layout.activity_facturas, container, false);
    // Get the application instance
    myApplication = (MyApplication)getActivity().getApplication();

    if (savedInstanceState != null) {
            System.out.println("bundle not null");
            myApplication.nuevoUsuarioActual();
            //TuplaUsuario.usuario
            myApplication.getUsuarioActual().setUsuario(savedInstanceState.getString("usuario"));
            //TuplaUsuario.password
            myApplication.getUsuarioActual().setPassword(savedInstanceState.getString("password"));
            //TuplaUsuario.cups
            ArrayList<TuplaCupsWS> arrayCups = new ArrayList<TuplaCupsWS>();
            int numCups = savedInstanceState.getInt("numCups");
            for(int cups = 0; cups < numCups; cups++) {
                TuplaCupsWS tuplaCups = new TuplaCupsWS();
                //TuplaUsuario.cups.CUPS, TuplaUsuario.cups.Tarifa, TuplaUsuario.cups.Direccion
                tuplaCups.setCups(savedInstanceState.getString("cups"+cups));
                tuplaCups.setTarifa(savedInstanceState.getString("tarifa"+cups));
                tuplaCups.setDireccion(savedInstanceState.getString("direccion"+cups));
                //TuplaUsuario.cups.facturas
                ArrayList<TuplaFacturaWS> arrayFacturas = new ArrayList<TuplaFacturaWS>();
            int numFacturas = savedInstanceState.getInt("numFacturas"+cups);
            System.out.println("n facturas: "+numFacturas);
            for(int factura = 0; factura < numFacturas; factura++) {

                TuplaFacturaWS tuplaFactura = new TuplaFacturaWS();
                tuplaFactura.setFactura(savedInstanceState.getString("factura"+factura));
                tuplaFactura.setImporte(savedInstanceState.getDouble("importe"+factura));
                tuplaFactura.setFecha(new Date(savedInstanceState.getLong("fecha"+factura)));
                tuplaFactura.setPlan(savedInstanceState.getString("plan"+factura));
                tuplaFactura.setEstado(savedInstanceState.getString("estado"+factura));
                tuplaFactura.setPdf(savedInstanceState.getString("pdf"+factura));
                tuplaFactura.setAnexo(savedInstanceState.getString("anexo"+factura));
                arrayFacturas.add(tuplaFactura);

            }
                tuplaCups.setFacturas(arrayFacturas);
                arrayCups.add(tuplaCups);
            }
            myApplication.getUsuarioActual().setCups(arrayCups);
            //TuplaUsuario.cupsActual
            myApplication.getUsuarioActual().setCupsActual(
                    savedInstanceState.getInt("cupsActual"));

    }

    facturas = myApplication.getFacturas();

    System.out.println("num facturas on create view: "+facturas.size());

    //...
}

在这种情况下,会发生什么:

  • 第一次加载视图时,视图显示 5 个“facturas”。onCreateView在创建视图上打印“num facturas: 5”。
  • 改变方向时,视图显示相同的 5 个“事实”。onSaveInstanceState打印“N facturas on save: 5”,Bundle不为空,并onCreateView打印“n facturas: 0”和“num facturas on create view: 0”。-> 这是错误的。
  • 当再次改变方向时,也会发生同样的情况。

如果我添加

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
}

与情况 1 相同。

我找不到问题出在哪里。

4

1 回答 1

0

我看到您int numFacturas = savedInstanceState.getInt("numFacturas");在 onCreateView() 中调用它,但我看不到您在 onSaveInstanceState() 中设置该值的位置。

这将导致 numFacturas = 0,因此永远不会进入您的循环来填充数组。

于 2013-08-29T17:38:48.260 回答