Skip to content Skip to sidebar Skip to footer

String Array In Spinner

i have an array which is populated in the spinner adapter. now i wanna change the size of the array! is it possible? help! thank u `public void classpopulate() { if (PEP.g

Solution 1:

You can use the ArrayAdapter methods clear, add, insert and/or remove to manipulate the adapter's data.

EDIT: From your comments, it sounds like you are eventually going to need to write your own custom adapter (probably by extending BaseAdapter), particularly if you want to have disabled (separator) rows, etc. You will have much more control over what gets displayed and how it looks. Search the web for "android custom list adapter" to find lots of examples of how to write one. It's not too difficult.

Nevertheless, if you want to keep using an ArrayAdapter, here's an example of removing the first element of the list (it assumes that the adapter has been saved in a member field adapter):

adapter.remove(adapter.getItem(0));

For more complicated changes (like loading a completely different set of data), you might do something like this:

String[] newData = . . .
adapter.setNotifyOnChange(false); // temporarily turn off auto-notification
adapter.clear();
adapter.addAll(newData);
adapter.notifyDataSetChanged(); // notifies and turns auto-notifications back on

It's good practice to suspend auto-notification when you are making several changes in sequence.

Solution 2:

I hope this helps you.

In your activity get references from your layout file like,

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

Then, make adapter for that

ArrayAdapter<String> adapter = newArrayAdapter<String>(this,android.R.layout.simple_spinner_item,YOUR_STRING_ARRAY);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

Then make one arrays.xml it is contains your string array.

for example,

<?xml version="1.0" encoding="utf-8"?><resources><string-arrayname="string_array"><item>1</item><item>2</item><item>3</item><item>4</item><item>5</item><item>6</item><item>7</item><item>8</item><item>9</item><item>10</item></string-array></resources>

Solution 3:

You could have an ArrayList in place of an array

ArrayList<String> classname = new ArrayList<String>;
classname.add("SELECET CLASS");

for (int i = 1; i < classdetails.size() + 1; i++) {

        classname[i].add(classdetails.get(i - 1).getClass_name() + "  "
                + classdetails.get(i - 1).getSection_name().toString());

    }

ArrayAdapter<CharSequence> adapterClasses = new ArrayAdapter<CharSequence>(
            getApplicationContext(), R.layout.spinner_item_class,
            R.id.spinnerclasstxt, classname);
    spnrClass.setAdapter(adapterClasses);
    spnrClass.setSelection(0);

Post a Comment for "String Array In Spinner"