I have tried everything but I don't get it to work!
I have an object, mObject
, which itself contains an arrayList
of objects which I want to display with a TextView
below mObject.getName()
. How do I accomplish this, the problem is that the sub ArrayList
can vary in size.
As I don't know how many items it will return, I can't put it in my ListView
Layout, so I need to do it programatically.
How do I do it ??
EDIT: more info...
In my "holder.linearLayout
" I want a TextView
, one for each item returned from mObject.getSets()
, it could be one, it could be 10. Thats why I can't put in my llSets ListView
layout file, I don't know how many TextView
s to put in.
EDIT:
Found a solotion: In the LinearLayout in my ListView layout. I just looped through all items in the sub ArrayList and added them like this:
holder.mLinearLayout.removeAllViews();
for (Set s : mExercise.getSets()) {
TextView mSetTextView = new TextView(mContext);
mSetTextView.setText("Text " + s.getText());
holder.mLinearLayout.addView(mSetTextView);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
CustomObject mObject = CustomObjects.get(position);
if (convertView == null) {
convertView = mLayoutInflater.inflate(R.layout.lv_exercise, null);
holder = new ViewHolder();
holder.tvEName = (TextView) convertView.findViewById(R.id.tvEName);
// I Want to add a new TextView to this linyearLayout for each item returned by mObject.getSets();
holder.linearLayout = (LinearLayout) convertView.findViewById(R.id.llSets);
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.tvEName.setText(mObject.getName());
return convertView;
}
The llSets ListView Layout:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="10dp">
<TableRow android:layout_marginBottom="5dp">
<TextView
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="2"
android:text="Name"
android:gravity="center"
android:id="@+id/tvEName"
android:textSize="22sp"
android:fadingEdge="horizontal"
android:ellipsize="end"
android:singleLine="true"/>
</TableRow>
<TableRow>
<LinearLayout
android:id="@+id/llSets"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<!-- I want textviews here, one for each item returned from mObject.getSets()
As I don't know how many items it will return, i need to add them programatically, I guess ?.
-->
</LinearLayout>
</TableRow>
</TableLayout>