问题
I'm adding views dynamically. Mean to say that, Initially there is one EditText
, Button Add
, Button Submit
.
1.OnClick of Button Add
I'm inflating new layout which has one EditText
and Button Remove
. If Button Add
is keep on pressed it will add inflated layout respectively.
2.OnClick of Button Submit
it has to Toast the EditText
values of Inflated layout.
I'm getting only the last EditText
Toast, and the Toast for rest of EditText
didn't show. how to do this?
public class MainActivity extends Activity implements OnClickListener {
Button add, submit;
ArrayList<View> viewList;
LinearLayout lin_layout;
EditText et1, et2;
String s1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = (EditText) findViewById(R.id.editText1);
add = (Button) findViewById(R.id.button_add);
submit = (Button) findViewById(R.id.bSubmit);
lin_layout = (LinearLayout) findViewById(R.id.linearLayout_view);
viewList = new ArrayList<View>();
add.setOnClickListener(this);
submit.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_add:
if (et1.length() == 0) {
Toast.makeText(getApplicationContext(),
"Filed cant be left empty", 0).show();
} else {
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(
R.layout.inflate_layout, null);
et2 = (EditText) addView.findViewById(R.id.edit);
viewList.add(addView);
lin_layout.addView(addView);
Button remove = (Button) addView
.findViewById(R.id.button_remove);
remove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
lin_layout.removeView((View) v.getParent());
viewList.remove((View) v.getParent());
}
});
}
break;
case R.id.bSubmit:
s1 = et2.getText().toString();
Toast.makeText(getApplicationContext(), "items: " + s1,Toast.LENGTH_SHORT).show();
break;
}
}
}
回答1:
The solution is very simple, Just create the instance of the editText locally, it will work :) Now, everything is going fine but the problem creates when you use to reference old edittext
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_add:
if (et1.length() == 0) {
Toast.makeText(getApplicationContext(), "Filed cant be left empty", 0).show();
} else {
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.inflate_layout, null);
EditText et2 = (EditText) addView.findViewById(R.id.edit);
viewList.add(addView);
lin_layout.addView(addView);
Button remove = (Button) addView.findViewById(R.id.button_remove);
remove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
lin_layout.removeView((View) v.getParent());
viewList.remove((View) v.getParent());
}
});
}
break;
case R.id.bSubmit:
s1 = et2.getText().toString();
Toast.makeText(getApplicationContext(), "items: " + s1, Toast.LENGTH_SHORT).show();
break;
}
}
来源:https://stackoverflow.com/questions/16666045/dynamically-adding-views-and-toasting-it