-1

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 TextViews 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>
Clint Eastwood
  • 165
  • 3
  • 10

2 Answers2

0

I would recommend you to learn about how to create a custom ArrayAdapter to display your data in a ListView.

This is a good tutorial that explain just that. I strongly recommend this tutorial for you, and any other that's having problems such as this.

Marcus
  • 6,697
  • 11
  • 46
  • 89
0

You have to add a listview in your existing listview with another custom ArrayAdapter. Something like that (in your Activity)

ArrayAdapter<mObject> adapter = new CustomAdapterForMyObjects();
//Add Objects with Lists inside to your adapter
mObject obj1 = new mObject("Hello World");
//Add listitems to your Object
mObject obj2 = new mObject("Second one");
//Add listitems to your Object

lv.setAdapter(adapter);

In your Custom Adapter you instantiate the other ListView and add the items of your objects. (in getView())

//instantiate ListView    
ListView lv = convertView.findViewById(R.id.lv_forTheItemsOfYourObject)

//get current object for that position    
mObject object = list.get(position);

//add your items to the Adapter, in this case strings    
ArrayAdapter<String> adapterForItems = new ArrayAdapter<String>(context, R.layout.rowforitems, object.getItems());

Now all the items of your Object should be added.

Summary: The Key is to use a listview integrated in another listview.

Erich
  • 1,838
  • 16
  • 20
  • Thanks for the help, I search for nested listviews and all I could find was that you should not do it :) Found another thread though, I will add the solution in the main thread – Clint Eastwood Feb 15 '15 at 10:16