Read More

How to get data from dynamically created Views android

In this blog,
I will show, how to create the dynamic views and get the data accordingly.

In this example, I am creating some UI elements on the screen at run time. Creating textview, RadioGroup, CheckBox and Spinner dynamically by dummy JSON data and creating the onclick listener for button. After clicking on the button I will get the data from each created views  and show it.

 

How to Implement

In this example, I have dynamically created four Types of Views

Screenshot_3

In this example, I have created above view by dummy JSON Data.

I am using option_name as a Heading

and variant_name as a sub data

option_type specifies the Type of the Views which is gotten from the JSON dummy data.

And I have used an ArrayList for storing the instance of the views.

and store the current view

For getting the data

After the views have created, We have to get the data which is selected in the views.

Then first we have to get the instance of current view

and then by below code you can get the data from spinner

Here I get the variant name of the spinner view but we can get anything which we want to get from views.

Screen-Shots:

Screenshot_2016-03-16-12-18-08_testapplication.amangupta.myapplicationtest2

Screenshot_2016-03-16-12-24-10_testapplication.amangupta.myapplicationtest2

 

Get the Complete Source Code 

MainActivity.java

DummyData.java

activity_main.xml

spiner_row.xml

 

 

. . .

Comments (5)

Add Your Comment

  • Shirisha Reddy
    hai sir
    button click is not woring showing error as
    java.lang.ClassCastException: android.widget.Spinner cannot be cast to android.widget.EditText
    • Aman Gupta
      Please share the code which you are using.
    • Aman Gupta
      Its seems that you have written the code like:
      Spinner spinner = (EditText) allViewInstance.get(noOfViews);

      Instead, it should be casted properly as:
      Spinner spinner = (Spinner) allViewInstance.get(noOfViews);

  • Milan Amatya
    java.lang.ClassCastException: android.widget.Spinner cannot be cast to android.widget.CheckBox

    when another checkbox is added in the json dummy data.

    • Aman Gupta
      Its seems that you have written the code like:
      Spinner spinner = (EditText) allViewInstance.get(noOfViews);
      Instead, it should be casted properly as:
      Spinner spinner = (Spinner) allViewInstance.get(noOfViews);
  • css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close