问题
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();
}
Check it once and please provide me with proper edits as I'm new to android and I dont have that much knowledge
回答1:
i see one problem in your code,
you write bellow 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"));
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.
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
?
and for retrieving your label just use i
in for statement.
来源:https://stackoverflow.com/questions/21719730/dynamically-created-buttons-have-same-label-after-restarting-application