简体   繁体   中英

How Can I Add A Spinner Dynamically(By Press Of A Button)?

I am new to Android Programming, I have been trying to add a spinner with a button click, my wonder is i always keep getting an error, I can not find a solid answer anywhere, Can somebody please help me? below is my code it tells me inSpinner cannot be created and that CreateFromResource() is array adapter cannot be applied.

.Java File

Button add;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_make__questionnaires);


    add = (Button) findViewById(R.id.b_Add);

add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

    LinearLayout newLinear = new LinearLayout();


    Spinner newSpinner = new Spinner(this);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.question_type_array, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource
            (android.R.layout.simple_spinner_dropdown_item);
    newSpinner.setAdapter(adapter);
    newSpinner.setOnItemSelectedListener(new CustomOnItemSelectedListener());

    newLinear.addView(newSpinner);


    setContentView(newLinear);


    RelativeLayout root_layout=(RelativeLayout)findViewById(R.id.root_layout);
    root_layout.addView(newSpinner);
}

You can directly add the Spinner in your Root Layout which is RelativeLayout in your case. I have solved your problem. I am sharing the Layout design , Java code and Screenshots here. Please leave a comment if I'm able to help you.

Layout File -->

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:id="@+id/root_layout">

      <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_margin="@dimen/medium"
        android:text="Add a Spinner"
        android:onClick="add"/>

</RelativeLayout>

strings.xml file -->

<?xml version="1.0" encoding="utf-8"?>

<string name="app_name">Hoster</string>
<string name="title_activity_main">Main</string>
<array name="spinner">
    <item> Airtel </item>
    <item> Vodafone </item>
    <item> Reliance </item>
    <item> Aircel </item>
    <item> BSNL </item>
</array>

Java Code -->

private RelativeLayout root;
private Spinner spinner;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.experimental);
    root = (RelativeLayout)findViewById(R.id.root_layout);
}

public void add(View v){
    spinner = new Spinner(this);
    ArrayAdapter<CharSequence> spinnerAdapter = ArrayAdapter.createFromResource(this,R.array.spinner,android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(spinnerAdapter);
    spinner.setLayoutParams(new ActionBar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    root.addView(spinner);
}![Added on Button Click][1]

You need to know that this in the OnClickListener class is indicating to it not the Activity class. and You need to pass the context of your activity to the (Spinner, LinearLayout, ...) constructor so to get the context of your activity you use NameOfTheActivityClass.this instead of using this directly.

I don't know your logic, but I need to mention that you can add the spinner to the main layout of the activity not inflating a new layout from scratch and setContentView() again.

btnAdd.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        LinearLayout newLinear = new LinearLayout(AddSpinnerDynamicallyActivity.this);


        Spinner newSpinner = new Spinner(AddSpinnerDynamicallyActivity.this);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                AddSpinnerDynamicallyActivity.this, R.array.question_type_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource
                (android.R.layout.simple_spinner_dropdown_item);
        newSpinner.setAdapter(adapter);


        newLinear.addView(newSpinner);


        setContentView(newLinear);
    }});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM