user1480742
user1480742

Reputation: 163

Android adapter.add() crashing my application

in android application, i'm filling my spinner with some data coming from EditText object. And when i'm trying to add it with adapter.add(somestring) method it crashes, so i need help. ...here's the code

public class OptionsMenu extends Activity implements View.OnClickListener{

    Spinner users;
    EditText input;
    Button add,remove;

    public static String filename = "savedData";
    SharedPreferences sharedData;
    String stringUsers;

   ArrayAdapter<CharSequence> adapter;

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

        Create();
        sharedData = getSharedPreferences(filename, 0);    
   }

private void Create() {
    // TODO Auto-generated method stub
    users = (Spinner) findViewById(R.id.sp_op_users);
    input = (EditText) findViewById(R.id.tb_op_inputUsers);
    add = (Button) findViewById(R.id.bt_op_add);
    remove = (Button) findViewById(R.id.bt_op_remove);

    add.setOnClickListener(this);
    remove.setOnClickListener(this);

    //------------ADAPTER-----------------  
    adapter = ArrayAdapter.createFromResource(this, R.array.users,
                        android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(
                android.R.layout.simple_spinner_dropdown_item);

    users.setAdapter(adapter);
}

public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId())
    {
        case R.id.bt_op_add:
            if (!input.getText().toString().equals("")) {
                CharSequence inputData = input.getText().toString();
                adapter.add(inputData);

                adapter.notifyDataSetChanged();
                users.setAdapter(adapter);
            }
            input.setText("");
            users.setSelection(Adapter.NO_SELECTION);
            break;
        case R.id.bt_op_remove:
            break;
    }
}   

Upvotes: 0

Views: 1765

Answers (2)

David Wasser
David Wasser

Reputation: 95578

You created the adapter using createFromResource() and provided it with the data from your resource. If you do it this way the list is fixed and you cannot add to or remove elements from it. This is why it crashed when you try to call adapter.add().

If you want to have the spinner contain dynamic data, then you'll have to add all the elements to it using add() and not create it from a resource.

EDIT: Add code example

in onCreate() we create and initialize the spinner adapter

List<String> items = ... // These are your items you get from a resource or read
                         //  from a file or whatever
// Create the adapter, initializing it with the list of items, attach to spinner
adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item);
for (String item : items) {
    adapter.add(item);
}
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
users.setAdapter(adapter);

in onClick(), to add an item to the spinner:

case R.id.bt_op_add:
    if (!input.getText().toString().equals("")) {
        CharSequence inputData = input.getText().toString();
        adapter.add(inputData);
        // You shouldn't need to reset the adapter on the spinner, nor call
        //  notifyDataSetChanged() here
    }
    input.setText("");
    users.setSelection(Adapter.NO_SELECTION);
    break;

Upvotes: 1

Arun George
Arun George

Reputation: 18592

Not very sure why you are getting the error but your if condition is wrong.

Change the following line:

if (input.getText().toString() != "") 

to

if (!input.getText().toString().equals("")) 

You don't compare strings using a = sign.

EDIT

Maybe you could first get the array from the resource file and create a local version of it:

String[] usersList=getResources().getStringArray(R.array.users);

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

and use this usersList as the list of data for your adapter.

Upvotes: 2

Related Questions