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 text view, RadioGroup, CheckBox and Spinner dynamically by dummy JSON data and creating the on click 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*


15 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);
      • Volkan
        Hi, i have same problem, same error this. When added 1 or more checkbox.
        and my code is
        Spinner spinner = (Spinner) allViewInstance.get(noOfViews);
        but logcat give an error.
        java.lang.ClassCastException: android.widget.Spinner cannot be cast to android.widget.CheckBox
        • anchit (Moderator)

          This is casting exception and this has something to do with your code only.

          Please do check the code more aptly.

        • Dhruv Chauhan
          Did you solved this problem? I am having this error from 2 days but i was focusing on other elements and all work done except this multiple checkboxes classcastexception, please let me know how to solve this. It would be really appreciating for your help, if you can’t reply here, please mail me on [email protected]
          • anchit (Moderator)
            Hey Dhruv,
            Can you please share the code that you are developing using this blog so that we can check and fix the error that you are facing.
  • 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

    We've bought a Shopware Extension which is a hybrid App, which we can now offer in the Google Play Store and the IOS App Store. I'am still impressed how fast and how sharp the support team leads us through every process of the App-Store clarification. Very fast and useful response.

    Markus Walter

    Markus Walter

    Founder, Das Apartment Living

    Talk to Sales

    Global

    Live Chat
    Start a Project


      Message Sent!

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

      Back to Home