使用ListView的片段

时间:2014-08-19 14:21:24

标签: android listview android-fragments android-arrayadapter

我正在尝试使用片段中的数组适配器填充列表视图,但列表视图不会显示。

这是我的片段:

package kyfb.android.kyfb.com.kyfb;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class BenefitsFragment extends Fragment {

    private ListView benefitsList;
    private String[] benefitsArray;
    private ArrayAdapter<String> arrayAdapter;

    public BenefitsFragment() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View rootView = inflater.inflate(R.layout.fragment_benefits, null);

        benefitsList = (ListView)rootView.findViewById(R.id.listFeed);

        benefitsArray = new String[] {"Automotive", "Financial Services", "Farm", "Home", "Health",
                "Identity Theft", "Insurance", "Security", "Travel"};

        arrayAdapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_list_item_1, benefitsArray);

        benefitsList.setAdapter(arrayAdapter);

        return rootView;
    }
}

这是XML。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/plain_background">

    <ListView
        android:id="@+id/listFeed"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:fadeScrollbars="true"
        android:fastScrollEnabled="true"
        android:fadingEdge="none"
        android:divider="@color/kfb_blue"
        android:dividerHeight="1dp"
        android:visibility="gone"
        android:background="@color/plain_background"/>

</RelativeLayout>

1 个答案:

答案 0 :(得分:2)

您无法看到该列表,因为您应用了以下内容:

 android:visibility="gone"

属性。删除它,列表应该出现。