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.getUser() == null) {
return;
}
classdetails = masterDataManager.getClassSections(PEP.getUser()
.getUsername(), getApplicationContext());
spnrClass = (Spinner) findViewById(R.id.spnrClass);
spnrSubject = (Spinner) findViewById(R.id.spnrSubject);
classname = new String[classdetails.size() + 1];
classname[0] = "SELECET CLASS";
for (int i = 1; i < classdetails.size() + 1; i++) {
classname[i] = 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);
spnrClass
.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int pos, long arg3) {
// LinearLayout layoutSpinnersubj = (LinearLayout)
// findViewById(R.id.layout_spinner);
// RelativeLayout subject_Text
// =(RelativeLayout)findViewById(R.id.layoutsubjecttext);
int selectedindex = pos;
if (selectedindex == 0) {
spnrSubject.setVisibility(View.INVISIBLE);
} else {
spnrSubject.setVisibility(View.VISIBLE);
selectedClass = classdetails.get(selectedindex - 1);
subjectpopulate(selectedClass);
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
`
You can use the
ArrayAdaptermethodsclear,add,insertand/orremoveto 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 fieldadapter):For more complicated changes (like loading a completely different set of data), you might do something like this:
It’s good practice to suspend auto-notification when you are making several changes in sequence.