Skip to content Skip to sidebar Skip to footer

How To Populate A Spinner Which Is Dynamically Generated On Click Of Button In Android

I was searching for how to add/Remove Layout Dynamically on click of button in android and found this. I used the codes from above mentioned tutorial and designed a page which gene

Solution 1:

Edit: You don't need your extra activity. You simply need one activity, that will display your layout, and update it on your button click.

publicclassTestActivityextendsActivity {

    Button btnDisplay;
    ImageButton btnAdd;
    LinearLayout container;

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        container = findViewById(R.id.linearLayoutForm);
        btnAdd = (ImageButton) findViewById(R.id.btnAdd);
        btnDisplay = (Button) findViewById(R.id.btnDisplay);

        btnAdd.setOnClickListener(addListener);
        //TODO: btnDisplay
    }

    /*
     * We define our OnClickListener that will act when we click on the btn.
     */
    View.OnClickListeneraddListener=newView.OnClickListener() {
        @OverridepublicvoidonClick(View v) {
            finalLinearLayoutnewView= (LinearLayout) getLayoutInflater().inflate(R.layout.rowdetail, null);

            newView.setLayoutParams(newLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

            ImageButtonbtnRemove= (ImageButton) newView.findViewById(R.id.btnRemove);
            btnRemove.setOnClickListener(newView.OnClickListener() {

                @OverridepublicvoidonClick(View v) {
                    container.removeView(newView);
                }
            });

            container.addView(newView);

            //Now we load your data into your spinnerSpinners= newView.findViewById(R.id.spinner1);
            try {
                loadSpinnerData(s);
            } catch (IOException e) {
                //TODO: catch exception
                e.printStackTrace();
            }

        }
    };

    /*
     * This function is supposed to load the data into the given spinner.
     * It would be better to load the data an other way, i.e.: using ASyncTask
     */privatevoidloadSpinnerData(Spinner s)throws IOException {

        // database handlerDBHelperdb=newDBHelper(getApplicationContext());

        // Spinner Drop down elements
        List<String> products = db.getAllProducts();

        // Creating adapter for spinner
        ArrayAdapter<String> dataAdapter = newArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, products);

        // Drop down layout style - list view with radio button
        dataAdapter
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // attaching data adapter to spinner
        s.setAdapter(dataAdapter);

    }
}

I haven't tested this code, so you might have to tweak it so it matches your needs, but I think the main idea is there. Your activity's onCreate inflates your layout. In there you set your button, and save information about your "container".

On click on your add button, your simply inflate your new layout, and set your spinner data using your loadSpinnerData(s);, which loads the data from the database into your spinner.

Note that it is not a good way of getting the information from the database. Doing so can block the UI thread as retrieving lots of information can be time consuming. It's better to use a loader, or an asynctask to do so. I can redirect you to Vogella tutorials that explains very well (and is very easy to understand) how to manage a database efficiently.

Post a Comment for "How To Populate A Spinner Which Is Dynamically Generated On Click Of Button In Android"