I'm trying to take a Spinner created in the Designer and change it's Adaptor to an Adaptor i've created in a library.
Look at a typical exception:
I loaded the layout file containing the Spinner and then passed it to my library to have my Adapter passed to it's setAdapter() method.
I added 64 strings as spinner items to my adapter, and clicked on item 6.
My Spinner seems to have two Adapters set - the B4ASpinnerAdapter and my CustomSpinnerAdapter.
The Spinner shows my 64 items so my CustomSpinnerAdapter has obviously been set as my Spinner's Adapter.
But a click on an item causes the exception - the B4ASpinnerAdapter looks for item number 6 but has no items - so the original B4ASpinnerAdapter is still set.
I can successfully set a custom Adapter as an Adapter for a B4A ListView using a similar technique, the B4A ListView's original Adapter is no longer set, my call to setAdapter() overwrites the previous Adapter with a ListView.
BUT with a Spinner the call to setAdapter() seems to addAdapter.
I've googled a bit but found no info for such a simple question - how can i change a Spinner's Adapter?
Here's my code:
Library and demo project attached.
(Ignore the logged message 'CustomSpinnerAdapter callback not found CustomSpinnerAdapter1_GetView' it's not relevant).
Thanks.
Martin.
Look at a typical exception:
java.lang.IndexOutOfBoundsException: Invalid index 6, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at anywheresoftware.b4a.objects.SpinnerWrapper$B4ASpinnerAdapter.getItem(SpinnerWrapper.java:257)
at anywheresoftware.b4a.objects.SpinnerWrapper$B4ASpinner.setSelection(SpinnerWrapper.java:212)
at android.widget.Spinner$DropdownPopup$1.onItemClick(Spinner.java:1042)
at android.widget.AdapterView.performItemClick(AdapterView.java:299)
at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2904)
at android.widget.AbsListView$3.run(AbsListView.java:3638)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5102)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
I loaded the layout file containing the Spinner and then passed it to my library to have my Adapter passed to it's setAdapter() method.
I added 64 strings as spinner items to my adapter, and clicked on item 6.
My Spinner seems to have two Adapters set - the B4ASpinnerAdapter and my CustomSpinnerAdapter.
The Spinner shows my 64 items so my CustomSpinnerAdapter has obviously been set as my Spinner's Adapter.
But a click on an item causes the exception - the B4ASpinnerAdapter looks for item number 6 but has no items - so the original B4ASpinnerAdapter is still set.
I can successfully set a custom Adapter as an Adapter for a B4A ListView using a similar technique, the B4A ListView's original Adapter is no longer set, my call to setAdapter() overwrites the previous Adapter with a ListView.
BUT with a Spinner the call to setAdapter() seems to addAdapter.
I've googled a bit but found no info for such a simple question - how can i change a Spinner's Adapter?
Here's my code:
B4X:
Sub Process_Globals
End Sub
Sub Globals
Private ListView1 As ListView
Private Spinner1 As Spinner
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("Main")
Dim CustomSpinnerAdapter1 As CustomSpinnerAdapter
CustomSpinnerAdapter1.Initialize("CustomSpinnerAdapter1") ' raises no events yet
CustomSpinnerAdapter1.SetSpinner(Spinner1)
For i=0 To 63
CustomSpinnerAdapter1.AddItem("Item: "&i, Null)
Next
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
B4X:
public class CustomSpinnerAdapter extends BaseAdapter implements SpinnerAdapter {
@ShortName("CustomSpinnerAdapterItem")
public static final class CustomSpinnerAdapterItem{
protected final Object mTag;
protected final String mText;
public CustomSpinnerAdapterItem(String pText, Object pTag){
mTag=pTag;
mText=pText;
}
/**
* Returns the item Tag property.
*/
public Object getTag(){
return mTag;
}
/**
* Returns the item Tag property.
*/
public String getText(){
return mText;
}
}
private BA mBA;
private String mEventName;
private String mGetViewEventName;
private LayoutInflater mLayoutInflater;
private List<CustomSpinnerAdapterItem> mItems;
public CustomSpinnerAdapter(BA pBA, String pEventName){
mBA=pBA;
mEventName=pEventName;
mGetViewEventName=(pEventName+"_GetView").toLowerCase(BA.cul);
if(!mBA.subExists(mGetViewEventName)){
BA.LogError("CustomSpinnerAdapter callback not found "+pEventName+"_GetView");
}
mLayoutInflater=LayoutInflater.from(mBA.context);
mItems=new ArrayList<CustomSpinnerAdapterItem>();
}
public synchronized void AddItem(String pText, Object pTag){
mItems.add(new CustomSpinnerAdapterItem(pText, pTag));
this.notifyDataSetChanged();
}
public synchronized void Clear(){
mItems.clear();
this.notifyDataSetChanged();
}
public BA getBA(){
return mBA;
}
@Override
public synchronized int getCount() {
return mItems.size();
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
convertView=mLayoutInflater.inflate(R.layout.simple_spinner_dropdown_item, parent, false);
}
((TextView) convertView).setText(mItems.get(position).getText());
return convertView;
}
public String getEventName(){
return mEventName;
}
@Override
public synchronized Object getItem(int pPosition) {
return mItems.get(pPosition);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
convertView=mLayoutInflater.inflate(R.layout.simple_spinner_item, parent, false);
}
((TextView) convertView).setText("debug: "+mItems.get(position).getText());
return convertView;
}
}
(Ignore the logged message 'CustomSpinnerAdapter callback not found CustomSpinnerAdapter1_GetView' it's not relevant).
Thanks.
Martin.