Java Question Help with layouts...

fiaful

Member
Licensed User
Longtime User
Hi there... I'm writing a library in eclipse that allows creation of custom listview, without inheriting from existing ListView class ...

the inside panel of a Scrollview is passed as a parameter to main class, and the items are created as a single View ...

the problem is that when I go to add the item, I get an exception:

B4X:
java.lang.ClassCastException: android.view.ViewGroup$LayoutParams
   at anywheresoftware.b4a.BALayout.onLayout(BALayout.java:39)
   at android.view.View.layout(View.java:7175)
   at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
   at android.widget.ScrollView.onLayout(ScrollView.java:1296)
   at android.view.View.layout(View.java:7175)
   at anywheresoftware.b4a.BALayout.onLayout(BALayout.java:40)
   at android.view.View.layout(View.java:7175)
   at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
   at android.view.View.layout(View.java:7175)
   at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
   at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
   at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
   at android.view.View.layout(View.java:7175)
   at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
   at android.view.View.layout(View.java:7175)
   at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
   at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:123)
   at android.app.ActivityThread.main(ActivityThread.java:3683)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
   at dalvik.system.NativeStart.main(Native Method)

the classes are these:

B4X:
// this is my main class... ScrollPanel is <ScrollView>.Panel
public class ASHyperList extends ViewWrapper<HyperList> 
{
...
   public void Initialize(BA pBA, String EventName, ViewGroup ScrollPanel)
   {
      super.Initialize(pBA, EventName);
      eventName = EventName;
      panel = ScrollPanel;
   }

   public void innerInitialize(BA pBA, String EventName, boolean pKeepOldObject)
   {
      if (!pKeepOldObject)
      {
         setObject(new HyperList(pBA.context));
      }

      super.innerInitialize(pBA, EventName, pKeepOldObject);
      ba = pBA;
   }

   public void Connect()
   {
      getObject().panel = panel;
   }

   public void Add(String Key, ASHyperItem Item)
   {
      getObject().addItem(Key, Item);
   }

}

public class HyperList extends View 
{
   public HyperList(Context context) 
   {
      super(context);
                // android.view.ViewGroup.LayoutParams;
      this.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
   }

   public void addItem(String Key, ASHyperItem Item)
   {
                // data is an HashMap
      data.Add(Key, Item);
      HyperListItem view = new HyperListItem(this.getContext(), Item);
      panel.addView(view);
   }

}

public class HyperListItem extends View 
{
   public HyperListItem(Context context, ASHyperItem Item) 
   {
      super(context);
      this.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
      item = Item;
   }

   @Override
   public void draw(Canvas canvas) 
   {
      super.draw(canvas);
      
                ...
   }
}

this is b4a sample code:

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

   Dim Aggiungi As Button
   Dim Lista As ScrollView
   Dim HyperList As ASHyperList
   Dim Counter As Int 
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("layASHyperListSample")
   Log ("ASHyperListSample - Lista.Panel is null = " & (Lista.Panel = Null))
   HyperList.Initialize("HyperList", Lista.Panel)
   HyperList.Connect
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Aggiungi_Click
   Dim Item As ASHyperItem
   Counter = Counter+1
   HyperList.Add(Counter, Item)
End Sub

could you tell me how to solve the problem?

thanks in advance
 

Informatix

Expert
Licensed User
Longtime User
If I read you well, these classes cannot handle complex layouts. That's wrong but I understand that you want to create a specific solution for your need.

I didn't test your code but I think there's a problem with the panel. You don't use the panel wrapper. You should replace ViewGroup with PanelWrapper in Initialize.
 
Top