I have created edittext dynamically when i enter the values i am unable to display values the values.If suppose i want add values that i have entered in edit text how is it possible do i need to give for loop.
Here is the code
public class NewActivity extends Activity {
LinearLayout ll;
EditText lEditText;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ll=new LinearLayout(this);
ll=(LinearLayout)findViewById(R.id.my);
Button lButton = (Button)findViewById(R.id.mybtnid);
lButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
lEditText = new EditText(getApplicationContext());
lEditText.setHint("Text Here");
lEditText.setInputType(InputType.TYPE_CLASS_NUMBER);
ll.addView(lEditText);
}
});
Button button1=(Button)findViewById(R.id.btn2);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
TextView tv = new TextView(getApplication());
int x=Integer.parseInt(lEditText.getText().toString());
tv.setText(x);
ll.addView(tv);
}
});
I am getting errors in logcat as :
AndroidRuntime(509): android.content.res.Resources$NotFoundException: String resource ID #0x1
E/AndroidRuntime(509): at android.content.res.Resources.getText(Resources.java:201)
E/AndroidRuntime(509): at android.widget.TextView.setText(TextView.java:2817)
E/AndroidRuntime(509): at com.app.NewActivity$2.onClick(NewActivity.java:44)
E/AndroidRuntime(509): at android.view.View.performClick(View.java:2408)
E/AndroidRuntime(509): at android.view.View$PerformClick.run(View.java:8816)
E/AndroidRuntime(509): at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime(509): at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(509): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(509): at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime(509): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(509): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(509): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(509): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime(509): at dalvik.system.NativeStart.main(Native Method)
Try to make changes as below to set the text in your
TextViewEDITED:
Try out below code for your dynamically adding values: