动态创建按钮有重新启动后,应用相同的标签? [英] Dynamically created buttons have same label after restarting application?

查看:147
本文介绍了动态创建按钮有重新启动后,应用相同的标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 predefined按钮生成新的按钮当点击它。产生新的按钮后,我想改变他们的标签对我使用的EditText 在对话框中定义即弹出 onLongClick 新产生的按钮。要保存所有生成的按钮和他们的标签我使用共享preferences 。但问题是,重新启动所有生成按钮后具有同一标签上他们。

I'm using a predefined button to generate new buttons when click on it. After generating new buttons I wish to change their label for that I'm using EditText defined in dialog box which pops up onLongClick of new generated buttons. To store all the generated buttons and their label I'm using Shared preferences. But the problem is after restarting all the generated buttons have same label on them.

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();
    }

检查一次,请为我提供适当的编辑,因为我是新来的Andr​​oid和我没有那么多的知识

Check it once and please provide me with proper edits as I'm new to android and I dont have that much knowledge

推荐答案

我看到你的code的一个问题,

i see one problem in your code,

你写娄code:

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

您创建一个新的按钮,以便该按钮没有任何标识,所以你如何从希望的getId()?此行取得 nullnull 。因为的getId()为空和为空了。您需要更改code。

you create one new button so this button don't have any id so how you want getId() from that? this line retrieve nullnull. because getid() is null and key is null too. you need change that code.

您尝试使用键+ btn1.getId()的关键是你的SP按钮设置关键,你为什么不使用 btn1.getId() SP 键?

you try set Key of your button in SP with key+btn1.getId() that key is null , why you don't use btn1.getId() as key in SP ?

和检索您的标签只使用 I 在声明。

and for retrieving your label just use i in for statement.

这篇关于动态创建按钮有重新启动后,应用相同的标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆