Populate Spinner dynamically in android from edit

2020-01-31 07:40发布

问题:

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)

回答1:

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");


回答2:

The problem is that ArrayAdapter.createFromResource() uses Arrays.asList() which returns an immutable list, which means you can't add any items to the adapter (that's the "unsupported" operation). Try creating an empty adapter and just add the items to that:

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


回答3:

Yeah, what they said.

package com.stackoverflow.q5999262;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

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 Main extends Activity
{
  private Spinner spinner;
  private EditText Text;
  private ArrayAdapter<String> adapter;
  private Button addButton;

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

    Text = (EditText) findViewById(R.id.widget4);
    List<String> planets = new ArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.planets_array)));
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, planets);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner = (Spinner) findViewById(R.id.hhj);
    spinner.setAdapter(adapter);
    addButton = (Button) findViewById(R.id.add_new);
    addButton.setOnClickListener(new OnClickListener()
    {
      public void onClick(View v)
      {
        addNewSpinnerItem();
      }
    });
  }

  protected void addNewSpinnerItem()
  {
    String textHolder = "" + Text.getText().toString();
    adapter.add(textHolder);
  }
}