Reputation: 119
i know this question is still answered in many posts. and theoretically i know the answer, but in this case i can't get it work.
I have a custom BaseAdapter
@Override
public Row getView(int position, View convertView, ViewGroup parent)
{
...
Row view = (Row) convertView;
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (view == null)
{
view = (JVxDataRow) mInflater.inflate(R.layout.row_base, null);;
...
}
...
}
return view;
}
this works fine, the row_base.xml is inflated correct, and my list view gets all entries. the row_base.xml consist only of:
<?xml version="1.0" encoding="utf-8"?>
<mh.android.ui.Row
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/datarow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:paddingRight="20dp" />
therefore i want to remove that file and instead instantiate this programmatically. my code for that - which doesn't work - is as following:
@Override
public Row getView(int position, View convertView, ViewGroup parent)
{
...
Row view = (Row) convertView;
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (view == null)
{
view = new Row(context);
...
}
...
}
return view;
}
to get it work i have to put some layoutparameters in the Row's constructor and moved the inflate from onFinishInflate to the constructor:
public Row(Context context)
{
super(context);
AbsListView.LayoutParams params = new AbsListView.LayoutParams(AbsListView.LayoutParams.WRAP_CONTENT, AbsListView.LayoutParams.WRAP_CONTENT);
this.setLayoutParams(params);
this.setPadding(20, 20, 20, 20);
this.setOrientation(LinearLayout.VERTICAL);
inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
container = inflater.inflate(R.layout.datarow3, this, true);
this.addView(container);
}
the datarow3.xml looks:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/table_row_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<mh.android.ui.DataItem
android:id="@+id/dataitem_slot1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true" >
</mh.android.ui.DataItem>
<mh.android.ui.DataItem
android:id="@+id/dataitem_slot2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true" >
</mh.android.ui.DataItem>
</LinearLayout>
the problem is in the BaseAdapter when i change from inflating the xml to instantiate programmatically, the listview is inside a LinearLayout.
Now the exception:
E/AndroidRuntime(13203): FATAL EXCEPTION: main
E/AndroidRuntime(13203): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
E/AndroidRuntime(13203): at android.widget.ListView.measureScrapChild(ListView.java:1170)
E/AndroidRuntime(13203): at android.widget.ListView.measureHeightOfChildren(ListView.java:1252)
E/AndroidRuntime(13203): at android.widget.ListView.onMeasure(ListView.java:1162)
E/AndroidRuntime(13203): at android.view.View.measure(View.java:15288)
E/AndroidRuntime(13203): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4876)
E/AndroidRuntime(13203): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
E/AndroidRuntime(13203): at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
E/AndroidRuntime(13203): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
E/AndroidRuntime(13203): at android.view.View.measure(View.java:15288)
E/AndroidRuntime(13203): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4876)
E/AndroidRuntime(13203): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
E/AndroidRuntime(13203): at android.view.View.measure(View.java:15288)
E/AndroidRuntime(13203): at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
E/AndroidRuntime(13203): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
E/AndroidRuntime(13203): at android.view.View.measure(View.java:15288)
E/AndroidRuntime(13203): at
what's going wrong there? thank's!
Upvotes: 0
Views: 1104
Reputation: 119
why are all comments are gone? The solution for my problem:
container = inflater.inflate(R.layout.datarow3, this, true);
this.addView(container);
changed to
inflater.inflate(R.layout.datarow3, this, true);
the layout do not need to be added after inflate it.
Upvotes: 1