【发布时间】:2015-02-14 07:42:27
【问题描述】:
我已经尝试了所有方法,但我无法正常工作!
我有一个对象mObject,它本身包含一个arrayList 对象,我想在mObject.getName() 下方显示一个TextView。我该如何做到这一点,问题是子ArrayList 的大小可以不同。
由于我不知道它会返回多少项目,所以我无法将它放在我的 ListView 布局中,所以我需要以编程方式进行。
我该怎么做??
编辑:更多信息...
在我的“holder.linearLayout”中,我想要一个TextView,每个从mObject.getSets() 返回的项目一个,可能是一个,可能是10。这就是为什么我不能放入我的llSets ListView布局文件,不知道要放多少TextViews。
编辑:
找到一个独奏: 在我的 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>
【问题讨论】:
-
当前代码有什么问题?好像还可以
-
我不明白你想做什么!!!
标签: java android arraylist adapter getview