Read More
Read now

How to get data from dynamically created Views android

Save

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

 

 

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


11 comments

  • 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 (Moderator)
      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);
  • ankit
    not working if having more than 1 checkbox
    • anchit (Moderator)
      Hello Ankit,

      Can you please share the stacktrace of the issue you are getting ?

      • cosmas
        Doesn’t work when you add more than one check box
        • anchit (Moderator)
          please share the stacktrace of the exception you are getting.
  • Sandeep
    How we can set validations on fields?
    • anchit (Moderator)
      The validation depends on your logic.
      The same way you are getting the data, you can put a validation over there.

      Or if you want you can collect all the data first and then validate with a similar function.

  • css.php

    Big thanks to Webkul and his team for helping get Opencart 3.0.3.7 release ready!

    Deniel Kerr

    Owner and Founder. Opencart

    Talk to Sales
    Start a Project

      Message Sent!

      If you have more details or questions, you can reply to the received confirmation email.

      Back to Home