-1

我已经尝试了一切,但我没有让它工作!

我有一个对象 ,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>

4

2 回答 2

0

您必须使用另一个自定义 ArrayAdapter 在现有列表视图中添加一个列表视图。类似的东西(在你的活动中)

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);

在您的自定义适配器中,您实例化另一个 ListView 并添加对象的项目。(在 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());

现在应该添加对象的所有项目。

总结:关键是使用一个listview集成在另一个listview中。

于 2015-02-14T15:50:27.030 回答
0

我建议您了解如何创建自定义ArrayAdapter以在ListView.

是一个很好的教程,可以解释这一点。我强烈推荐这个教程给你,以及任何其他有类似问题的人。

于 2015-02-14T09:36:28.187 回答