我正在使用GridView将数据动态加载到我的Activity:
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?感谢。
答案 0 :(得分:1)
您的保存和恢复方法应该用于原始值的小数据。对于像arraylist,data base这样的大型数据集,你应该覆盖onRetainNonConfigurationInstance()并返回该对象。
警告:虽然您可以返回任何对象,但您永远不应传递与Activity绑定的对象,例如Drawable,Adapter,View或与Context关联的任何其他对象。如果这样做,它将泄漏原始活动实例的所有视图和资源。 (资源泄漏意味着您的应用程序会保留它们并且不能进行垃圾回收,因此可能会丢失大量内存。)
你应该覆盖onRetainNonConfigurationInstance()并返回arraylist。
有关详细信息,请查看此处的链接。 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));
}