在android中使用SharedReference存储动态按钮标签时出错?

在android中使用SharedReference存储动态按钮标签时出错?,android,xml,android-layout,button,sharedpreferences,Android,Xml,Android Layout,Button,Sharedpreferences,在我的应用程序中,我使用预定义的按钮来动态生成按钮。使用标签Test1、Test2、Test3等创建的按钮。但在重新启动应用程序后,所有动态生成的按钮都有新的标签。 添加按钮的代码: AlertDialog.Builder addreport = new AlertDialog.Builder(MainActivity.this); addreport.setTitle("Add New Button"); LinearLayout addlayout = new

在我的应用程序中,我使用预定义的按钮来动态生成按钮。使用标签Test1、Test2、Test3等创建的按钮。但在重新启动应用程序后,所有动态生成的按钮都有新的标签。 添加按钮的代码:

AlertDialog.Builder addreport = new AlertDialog.Builder(MainActivity.this);
        addreport.setTitle("Add New Button");
        LinearLayout addlayout = new LinearLayout(MainActivity.this);
        addlayout.setOrientation(LinearLayout.VERTICAL);
        addlayout.setPadding(15,15,15,15);

        final EditText btnlabel = new EditText(MainActivity.this);
        btnlabel.setHint("Enter btn label");
        addlayout.addView(btnlabel);

        addreport.setView(addlayout);
        addreport.setNeutralButton("SUBMIT", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                    LinearLayout ll = (LinearLayout)findViewById(R.id.layout1);

                    //final Button btn = new Button(MainActivity.this);
                    btn.setText(btnlabel.getText());
                    btn.setId(i);
                    LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);       
                    ll.addView(btn, lp);
                    i++;
                    btncount++;
                    Editor edit = preferences.edit();
                    edit.putString("btn"+btn.getId(), btn.getText().toString());
                    edit.putInt("count", btncount);
                    edit.commit();
                    return; 
                    }
            });
        AlertDialog adrptdialog = addreport.create();
        adrptdialog.show();
    }   
    }
要存储的SharedReference代码:

preferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
    //btncount = preferences.getInt("count", 0);
    String labels = preferences.getString("btn"+btn.getId(), "New");
    for( i=0;i<btncount;i++)
    {
        final Button btn1 = new Button(this);
        btn1.setId(i);
        LinearLayout ll = (LinearLayout)findViewById(R.id.layout1);
        LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);       
        btn1.setText(labels);
    ll.addView(btn1, lp);   
    }   

//btncount=preferences.getIntcount,0;这一行已被注释。因此,您永远不会输入循环。您是否检查了共享首选项xml的内容?你可以使用DDMS@nikis共享首选项xml文件包含需要在应用程序中显示为输出的相同信息,但在重新启动应用程序后具有相同的信息labels@blackbelt在没有注释行的情况下尝试,但没有预期结果代码不清楚,但问题是这里的字符串标签=首选项。getStringbtn+btn.getId,New;。您确定getId的结果对应于变量i吗?其工作原理类似于一个符咒,但在添加一个按钮后,如果添加第二个按钮,则应用程序将崩溃并显示错误:指定的子级已具有父级。您必须首先调用子级的父级上的removeView。尝试在没有最终按钮的情况下构造。我更新了代码。这是一篇关于以编程方式添加视图的博客文章,应该是一个很好的开始。
preferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
int btncount = preferences.getInt("count", 0);
LinearLayout ll = (LinearLayout)findViewById(R.id.layout1);
for( i=0;i<btncount;i++)
{
    String label = preferences.getString("btn"+i, "New");
    Button btn1 = new Button(this);
    btn1.setId(i);
    LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);       
    btn1.setText(label);
    ll.addView(btn1, lp);   
}