重启应用程序后,动态创建的按钮具有相同的标签?

时间:2014-02-12 05:50:33

标签: android sharedpreferences dynamically-generated

我点击它时使用predefined button生成new buttons。生成新按钮后,我希望change their label因为我正在使用EditText在对话框中定义,弹出onLongClick新生成的按钮。要存储所有生成的按钮及其标签,我正在使用Shared preferences。但问题是重新启动后所有生成的按钮都有相同的标签。

code in mainactivity-----
SharedPreferences prefs=null;
String key;
int btncount = 15;

code in onCreate method----
prefs = PreferenceManager.getDefaultSharedPreferences(this);
btncount=prefs.getInt("count", 0);
LinearLayout ll = (LinearLayout)findViewById(R.id.layout1);
for(int i=0;i<btncount;i++)
    {
        LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);       
        final Button myButton = new Button(this);
        myButton.getId();
        myButton.setText(prefs.getString(key+myButton.getId(),"New"));
        myButton.setOnLongClickListener(new OnLongClickListener() {
            public boolean onLongClick(View arg0)
                {
                    AlertDialog lbldialog = new AlertDialog.Builder(context).create();
                    final EditText input = new EditText(MainActivity.this);                 
                    lbldialog.setView(input);
                    lbldialog.setButton(DialogInterface.BUTTON_POSITIVE, "Change", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) 
                                {
                                myButton.setText(input.getText());
                                    Editor edit = prefs.edit();
                                    edit.putString(key+myButton.getId(), myButton.getText().toString());
                                    edit.commit();
                                }
                        });

                lbldialog.show();   
        return true;  
        }
});
ll.addView(myButton, lp);}

Code to create button-----
if(v == btnaddnew)                      
{
        final Button btn1 = new Button(this);
        btn1.setText("New");
        btn1.setId(btncount);
        btn1.setOnClickListener(new OnClickListener () {
        @Override
        public void onClick(View v){
            reportDialog(btn1.getText().toString());
            }
        });

        btn1.setOnLongClickListener(new OnLongClickListener() {
            public boolean onLongClick(View arg0) {
                AlertDialog lbldialog = new AlertDialog.Builder(context).create();
                final EditText input = new EditText(MainActivity.this);
                lbldialog.setView(input);
                lbldialog.setButton(DialogInterface.BUTTON_POSITIVE, "Change",
                        new DialogInterface.OnClickListener() 
                {
                    public void onClick(DialogInterface dialog, int which) 
                        {
                            btn1.setText(input.getText());
                            Editor edit = prefs.edit();
                            edit.putString(key+btn1.getId(), btn1.getText().toString());
                            edit.commit();

                        }
                });

            lbldialog.show();   
        return true;  
            }
        });         
        LinearLayout ll = (LinearLayout)findViewById(R.id.layout1);
        LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);       
        ll.addView(btn1, lp);
        btncount++;
        Editor editor = prefs.edit();
        editor.putInt("count", btncount);
        editor.commit();
    }

检查一次,请为我提供适当的编辑,因为我是android的新手,我没有那么多的知识

1 个答案:

答案 0 :(得分:0)

我在你的代码中看到一个问题,

你写下以下代码:

LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);       
final Button myButton = new Button(this);
myButton.setText(prefs.getString(key+myButton.getId(),"New"));

您创建了一个新按钮,因此此按钮没有任何ID,所以您希望getId()如何?此行检索nullnull。因为getid()为空,key也为空。你需要改变那段代码。

您尝试在SP中设置按键的键,key+btn1.getId()该键为null,为什么不在btn1.getId()中使用SP作为键?

并且要检索您的标签,只需在for语句中使用i