我已经尝试了一切,但我没有让它工作!
我有一个对象 ,mObject
它本身包含一个arrayList
我想在TextView
下面显示的对象mObject.getName()
。我如何做到这一点,问题是潜艇ArrayList
的大小可能会有所不同。
因为我不知道它会返回多少项目,所以我不能把它放在我的ListView
布局中,所以我需要以编程方式进行。
我该怎么做 ??
编辑:更多信息...
在我的 " holder.linearLayout
" 中,我想要一个TextView
, 每个返回的项目mObject.getSets()
一个,可能是一个,可能是 10。这就是为什么我不能放入我的 llSets布局文件,我不知道要放入ListView
多少个 s。TextView
编辑:
找到了一个独奏:在我的 ListView 布局中的 LinearLayout 中。我只是遍历了子 ArrayList 中的所有项目并像这样添加它们:
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;
}
llSets ListView 布局:
<?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>