Android列表视图不显示数据

时间:2015-01-20 20:29:38

标签: android listview

我正在尝试设置我的列表视图但是我无法显示数据,我确定我遗漏了一些东西,但这里是我的代码:

我的xml:

            <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#95dad4"
    android:gravity="center" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/relativeLayout1"
        android:layout_below="@+id/ll1"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/editdate_top"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="14dp"
            android:paddingBottom="4dp"
            android:text="@string/selectholidaytxt"
            android:textAppearance="@android:style/TextAppearance.Small"
            android:textColor="#8e3a41"
            android:textStyle="bold" />

        <Spinner
            android:id="@+id/countryspinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/editdate_top"
            android:layout_alignRight="@+id/editdate_top"
            android:layout_below="@+id/editdate_top" />

        <ListView
            android:id="@+id/holidayslist"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/countryspinner"
            android:layout_alignRight="@+id/countryspinner"
            android:layout_below="@+id/countryspinner" >
        </ListView>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:background="#42c1b6" >

        <Button
            android:id="@+id/settings_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/home"
            android:layout_alignBottom="@+id/home"
            android:layout_centerHorizontal="true"
            android:background="@drawable/settingicon" />

        <Button
            android:id="@+id/home"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginRight="30dp"
            android:layout_toLeftOf="@+id/settings_btn"
            android:background="@drawable/homeicon" />

        <Button
            android:id="@+id/info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/settings_btn"
            android:layout_alignBottom="@+id/settings_btn"
            android:layout_marginLeft="30dp"
            android:layout_toRightOf="@+id/settings_btn"
            android:background="@drawable/infoicon" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/ll1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="#42c1b6"
        android:orientation="horizontal"
        android:padding="5dp" >

        <Button
            android:id="@+id/buttonback"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:background="@drawable/back_btn" />
    </RelativeLayout>

</RelativeLayout>

我的活动:

public class Listing extends Activity {

    ListView holidays;
    ArrayList<String> DataList = new ArrayList<String>();
    ArrayAdapter<String> arrayAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_holidayslisting);

        holidays = (ListView) findViewById(R.id.holidayslist);

        DataList.add("test");
        DataList.add("test2");
        DataList.add("test3");
        arrayAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, DataList);

        holidays.setAdapter(arrayAdapter);
    }
}

但是列表视图没有显示任何内容。

1 个答案:

答案 0 :(得分:0)

这很简单。你必须填充你的微调器,以便你的微调器有一些宽度。由于您不对其执行任何操作,因此微调器的宽度为零,并且您具有ListView的这些属性

android:layout_alignLeft="@+id/countryspinner"
android:layout_alignRight="@+id/countryspinner"

将listview的宽度与spinner的宽度对齐。

您可以从ListView中删除上述属性,也可以将宽度硬编码到微调器,或者使用某些值填充微调器。

布局应该是这样的,

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/relativeLayout1"
    android:layout_below="@+id/ll1" >

    <TextView
        android:id="@+id/editdate_top"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="14dp"
        android:paddingBottom="4dp"
        android:text="@string/app_name"
        android:textAppearance="@android:style/TextAppearance.Small"
        android:textColor="#8e3a41"
        android:textStyle="bold" />

    <Spinner
        android:id="@+id/countryspinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editdate_top"
        android:layout_alignRight="@+id/editdate_top"
        android:layout_below="@+id/editdate_top" />

    <ListView
        android:id="@+id/holidayslist"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/countryspinner" >
    </ListView>
</RelativeLayout>

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="60dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:background="#42c1b6" >

    <Button
        android:id="@+id/settings_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/home"
        android:layout_alignBottom="@+id/home"
        android:layout_centerHorizontal="true"
        android:background="@drawable/ic_launcher" />

    <Button
        android:id="@+id/home"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginRight="30dp"
        android:layout_toLeftOf="@+id/settings_btn"
        android:background="@drawable/ic_launcher" />

    <Button
        android:id="@+id/info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/settings_btn"
        android:layout_alignBottom="@+id/settings_btn"
        android:layout_marginLeft="30dp"
        android:layout_toRightOf="@+id/settings_btn"
        android:background="@drawable/ic_launcher" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/ll1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="#42c1b6"
    android:orientation="horizontal"
    android:padding="5dp" >

    <Button
        android:id="@+id/buttonback"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:background="@drawable/ic_launcher" />
</RelativeLayout>