Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populate Spinner dynamically in android from edit text

I have one spinner in which few values are there from strings.xml and I want to populate that spinner dynamically from edit text using onclick event of Button but somehow it's not working and I am getting Force Close everytime. Any help will be appreciated.

package com.example.expense;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class Expense3 extends Activity {
    //private TextView t1;

    private Spinner spinner;
    private EditText Text;
    private ArrayAdapter<CharSequence> adapter;
    private Button addButton;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Text = (EditText)findViewById(R.id.widget4);
        addButton = (Button)findViewById(R.id.add_new);

        spinner = (Spinner) findViewById(R.id.hhj);

         adapter = ArrayAdapter.createFromResource(
                this, R.array.planets_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);





        this.addButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                addNewSpinnerItem();
         }

            });

        }

    protected void addNewSpinnerItem() {
        // TODO Auto-generated method stub
        CharSequence textHolder = "" + Text.getText().toString();
        adapter.add(textHolder);


    }
}

Logcat error:-

05-14 01:07:19.934: ERROR/AndroidRuntime(802): FATAL EXCEPTION: main
05-14 01:07:19.934: ERROR/AndroidRuntime(802): java.lang.UnsupportedOperationException
05-14 01:07:19.934: ERROR/AndroidRuntime(802):     at java.util.AbstractList.add(AbstractList.java:411)
05-14 01:07:19.934: ERROR/AndroidRuntime(802):     at java.util.AbstractList.add(AbstractList.java:432)
05-14 01:07:19.934: ERROR/AndroidRuntime(802):     at android.widget.ArrayAdapter.add(ArrayAdapter.java:178)
05-14 01:07:19.934: ERROR/AndroidRuntime(802):     at com.example.expense.Expense3.addNewSpinnerItem(Expense3.java:50)
05-14 01:07:19.934: ERROR/AndroidRuntime(802):     at com.example.expense.Expense3$1.onClick(Expense3.java:40)
05-14 01:07:19.934: ERROR/AndroidRuntime(802):     at android.view.View.performClick(View.java:2408)
05-14 01:07:19.934: ERROR/AndroidRuntime(802):     at android.view.View$PerformClick.run(View.java:8816)
05-14 01:07:19.934: ERROR/AndroidRuntime(802):     at android.os.Handler.handleCallback(Handler.java:587)
05-14 01:07:19.934: ERROR/AndroidRuntime(802):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-14 01:07:19.934: ERROR/AndroidRuntime(802):     at android.os.Looper.loop(Looper.java:123)
05-14 01:07:19.934: ERROR/AndroidRuntime(802):     at android.app.ActivityThread.main(ActivityThread.java:4627)
05-14 01:07:19.934: ERROR/AndroidRuntime(802):     at java.lang.reflect.Method.invokeNative(Native Method)
05-14 01:07:19.934: ERROR/AndroidRuntime(802):     at java.lang.reflect.Method.invoke(Method.java:521)
05-14 01:07:19.934: ERROR/AndroidRuntime(802):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-14 01:07:19.934: ERROR/AndroidRuntime(802):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-14 01:07:19.934: ERROR/AndroidRuntime(802):     at dalvik.system.NativeStart.main(Native Method)
like image 991
arsenal Avatar asked May 14 '11 01:05

arsenal


1 Answers

You are trying to add item in the adapter which is created from resources so it will give you result unsupported. To solve this issue you can create a blank List and then add items in the List from your resources. For that you can use Arrays.asList() or you can make a loop and add individual items in the List and after creating a List you will have to create a adapter using that List.

Now to add a item in the adapter of the spinner you can use the adapter.add() method to add a item inside the spinner adapter. Here is a example that explain how you can create a adapter and add item in the spinner.

adapter = new ArrayAdapter<String>(this, 
android.R.layout.simple_spinner_item, array);

Here array is a List.

And for add item in the spinner you can use the add method.

adapter.add("String to insert into spinner");
like image 141
Dharmendra Avatar answered Oct 15 '22 19:10

Dharmendra