0

I created a custom xml file with a LinearLayout and TextView. I have a for loop that i want go through an ArrayList and add the view to a ScrollView for each object. Below is the code i tried to achieve this, but it just gives me a blank screen. I tried to see if the ArrayList was empty, but that's not it. I didn't really know what i was doing when i tried this. I just wanted to see if it worked, but it didn't so what's the problem?

        for(int x = 0; x < runs.size(); x++){

        inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.run_layout, (ViewGroup) getCurrentFocus());

        TextView name = (TextView) layout.findViewById(R.id.tvRunName);
        name.setText(runs.get(x).getName());

        llRuns.addView(layout);
    }

Heres the run_layout xml....

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/run_background" >

<TextView
    android:id="@+id/tvRunName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@android:color/white"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:layout_gravity="center"
    android:layout_marginTop="10dp" />

Here's the Activities xml....

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".YourRuns"
android:background="#181818"
android:id="@+id/llYourRunsLayout">

<ScrollView
    android:id="@+id/svRuns"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/llRuns"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >


    </LinearLayout>

</ScrollView>

4

1 回答 1

0

将 (ViewGroup) getCurrentFocus() 更改为 null,因为 inflater.inflate() 第二个参数是视图的父级,并且当您启动活动时默认没有焦点元素。并且您首先在 inflate 时指示两个父级视图,然后在您说将布局添加到 llRuns 时。

for(int x = 0; x < runs.size(); x++){

    inflater = getLayoutInflater();
    View layout = inflater.inflate(R.layout.run_layout, (ViewGroup) getCurrentFocus());

    TextView name = (TextView) layout.findViewById(R.id.tvRunName);
    name.setText(runs.get(x).getName());

    llRuns.addView(layout);
}
于 2013-08-08T16:05:08.450 回答