1

我正在使用 GridView 将动态数据加载到我的活动中:

private ArticuloAdapter articuloAdapter;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);             
    setContentView(R.layout.grid_articulos);

    // get ListaArticulos from the database

    articuloAdapter = new ArticuloAdapter(this,R.layout.lista_articulo, ListaArticulos);
    grdArticulos.setAdapter(articuloAdapter);  
}

我想在方向改变后恢复数据:

ArrayList<String> articulosBackup = new ArrayList<String>();   

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);            
    int size = grdArticulos.getChildCount();
    for(int i = 0; i < size; i++) {
        ViewGroup layout = (ViewGroup) this.grdArticulos.getChildAt(i);
        ViewGroup gridChild = (ViewGroup) layout.getChildAt(1);     

        EditText et =(EditText)gridChild.getChildAt(0).findViewById(R.id.Cantidad);           
        EditText et2 = (EditText)gridChild.getChildAt(1).findViewById(R.id.jaulas);           
        EditText et3 =(EditText)gridChild.getChildAt(2).findViewById(R.id.contenedores);          
        CheckBox cb = (CheckBox)gridChild.getChildAt(3).findViewById(R.id.manipulado);

        String dato = et.getText().toString() + ":" + et2.getText().toString() + ":" + et3.getText().toString() + ":" + String.valueOf(cb.isChecked());
        articulosBackup.add(dato);
    }
    outState.putSerializable("articulosBackup", articulosBackup);   
    outState.putInt("items", size);
}   

我将数据保存在 ArrayList 中,例如字符串,格式为:data1:data2:data3:data4。这很好用。但是现在当我尝试恢复它们时,我在注释的行中得到了 NullPointerException:

@SuppressWarnings("unchecked")
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);   

    articulosBackup = (ArrayList<String>)savedInstanceState.getSerializable("articulosBackup"); 
    items           = savedInstanceState.getInt("items");      

    if (articulosBackup != null) {          
        for(int i = 0; i < items; i++) {
            ViewGroup layout    = (ViewGroup) grdArticulos.getChildAt(i);
            ViewGroup gridChild = (ViewGroup) layout.getChildAt(1); // NULLPOINTEREXECPTION: layout variable is null

            String datos[] = articulosBackup.get(i).split(":");

            // Cantidad
            EditText et =(EditText)gridChild.getChildAt(0).findViewById(R.id.Cantidad);
            et.setText(datos[0]);

            // Jaulas
            EditText et2 = (EditText)gridChild.getChildAt(1).findViewById(R.id.jaulas);
            et2.setText(datos[1]);

            // Contenedores
            EditText et3 =(EditText)gridChild.getChildAt(2).findViewById(R.id.contenedores);
            et3.setText(datos[2]);

            //  Manipulado
            CheckBox cb = (CheckBox)gridChild.getChildAt(3).findViewById(R.id.manipulado);
            cb.setChecked(Boolean.parseBoolean(datos[3]));
        }
    } 
}   

所以我的问题是:如何在方向改变后恢复我的动态 GridView?谢谢。

4

1 回答 1

1

您的保存和恢复方法应用于小数据,例如原始值。对于像arraylist、数据库这样的大型数据集,您应该覆盖onRetainNonConfigurationInstance() 并返回对象。

注意:虽然您可以返回任何对象,但绝不应传递与 Activity 相关联的对象,例如 Drawable、Adapter、View 或与 Context 关联的任何其他对象。如果这样做,它将泄漏原始活动实例的所有视图和资源。(资源泄漏意味着您的应用程序会保留它们,并且不能对它们进行垃圾回收,因此可能会丢失大量内存)。

您应该覆盖 onRetainNonConfigurationInstance() 并返回数组列表。

有关更多信息,请查看此处的链接。http://developer.android.com/guide/topics/resources/runtime-changes.html

@Override
public Object onRetainNonConfigurationInstance() {

return arraylist;//return arraylist
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final ArrayList data = (ArrayList ) getLastNonConfigurationInstance();
if (data == null) { //if data is null populate data to arraylist.
 //do something  
}
// use arraylist
}

一旦方向改变,活动就会重新创建。所以返回 onRetainNonConfigurationInstance() 中的值。在 onCreate() 中提供返回的值。

例子:

       @Override
   public Object onRetainNonConfigurationInstance() {

   return mPostingData; 
   }

在 onCreate()

   final ArrayList data = (ArrayList) getLastNonConfigurationInstance();
      if (data == null) {
          System.out.println("data is null");
      //populate arraylist with values.
      }
      else
      {
           for(int i=0;i<data.size();i++)
           System.out.println("data is not null..."+data.get(i)); 
      }   
于 2013-04-02T09:37:25.540 回答