我是Android中的法国菜鸟,我有一个问题,我找不到解决方案。 事实上,我在平板电脑的轮换期间无法保存我的LinearLayouts状态。我已经尝试了很多东西,我制作了这段代码:
public class Repas extends Activity {
private LinearLayout area1, area2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.repas);
area1 = (LinearLayout) findViewById(R.id.area1);
area2 = (LinearLayout) findViewById(R.id.area2);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
final int width_screen=dm.widthPixels;
area2.setMinimumWidth(width_screen);
if( savedInstanceState != null ) {
int count_item = savedInstanceState.getInt("number_of_item");
String image = savedInstanceState.getString("Sauvegarde_repas0");
//area2.addView(image);
}
else {
TypedArray arrayResources = getResources().obtainTypedArray(
R.array.resicon);
for (int i = 0; i < arrayResources.length(); i++) {
ImageView imageView = new ImageView(this);
imageView.setImageDrawable(arrayResources.getDrawable(i));
imageView.setOnTouchListener(myOnTouchListener);
area2.addView(imageView);
}
arrayResources.recycle();
}
}
}
@Override
public void onSaveInstanceState(Bundle outState)
{
int count = area1.getChildCount();
View v = null;
outState.putInt("number_of_item", count);
for(int i=0; i<count; i++) {
v = area1.getChildAt(i);
outState.putString("Sauvegarde_repas"+i, v.toString());
}
super.onSaveInstanceState(outState);
}
我当前的问题,假设我的逻辑是正确的,我在平板电脑的轮换期间无法将我的String转换为ImageView。你能救我吗?
我还想知道我所做的是否正确:我的意思是逐一获取我的布局元素(我没有找到如何保存整个布局)?
我希望我足够清楚:如果您需要更多信息,请不要犹豫!
非常感谢你的帮助!
答案 0 :(得分:0)
我发了一个新帖子,因为我的代码正在运作......但是我有几个问题,如果你能回答我,我将非常感激。
这是我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.repas);
area1 = (LinearLayout) findViewById(R.id.area1);
area2 = (LinearLayout) findViewById(R.id.area2);
TypedArray arrayResources = getResources().obtainTypedArray(
R.array.resicon);
if( savedInstanceState != null ) {
int count_item = savedInstanceState.getInt("number_of_item");
int tab_item[] = new int[count_item];
for (int i = 0; i < count_item; i++) {
tab_item[i]=savedInstanceState.getInt("Sauvegarde_repas" + i);
}
for (int i = 0; i < arrayResources.length(); i++) {
ImageView imageView = new ImageView(this);
imageView.setImageDrawable(arrayResources.getDrawable(i));
imageView.setOnTouchListener(myOnTouchListener);
imageView.setId(i);
if (check(tab_item,i)){
area1.addView(imageView);
}
else{
area2.addView(imageView);
}
}
}
else {
for (int i = 0; i < arrayResources.length(); i++) {
ImageView imageView = new ImageView(this);
imageView.setImageDrawable(arrayResources.getDrawable(i));
imageView.setOnTouchListener(myOnTouchListener);
imageView.setId(i);
area2.addView(imageView);
}
arrayResources.recycle();
}
area1.setOnDragListener(myOnDragListener);
area2.setOnDragListener(myOnDragListener);
}
@Override
public void onSaveInstanceState(Bundle outState)
{
int count = area1.getChildCount();
View v = null;
outState.putInt("number_of_item", count);
for(int i=0; i<count; i++) {
v = area1.getChildAt(i);
outState.putInt("Sauvegarde_repas" + i, v.getId());
}
super.onSaveInstanceState(outState);
}
以下是我的讯问:
当我设置id时,它只是“i”,这是一个非常简单的数字。它似乎不像字符串“R.id.String_here”那样独特(即使我知道它是以Integer转换的),那么我怎样才能确保使用唯一的ID?
我必须一个接一个地保存我布局的孩子吗?保存整个布局是不可能的?
非常感谢你的帮助!