ListView没有在片段中显示?

时间:2015-05-25 16:46:12

标签: android listview android-fragments android-linearlayout simpleadapter

我有一个片段,我在其中调用一个函数,该函数应该填充并显示片段的xml中定义的列表视图,但由于某些原因,没有文本出现在列表视图中。我的所有日​​志都在控制台中正确输出,因此我知道选项列表正在正确填充。这是我的代码。任何帮助将不胜感激!!

public class PickEventTypeFragment extends Fragment {
private static final String TAG = "DaniDebug";
public static ArrayList<HashMap<String, String>> optionList;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

    SharedPreferences preferences = this.getActivity().getPreferences(Activity.MODE_PRIVATE);

    final View view = inflater.inflate(R.layout.fragment_pick_event_type, container, false);
    displayListView(view);
    Log.d(TAG, "got here");
    return view;
}

private void displayListView(View view) {

    //makes a list of options for first string
    optionList = new ArrayList<HashMap<String, String>>();
    initList();
    Log.d(TAG, optionList.get(0).get("option"));
    Log.d(TAG, optionList.get(1).get("option"));

    //get the ListView component from the layout
    ListView lv = (ListView) view.findViewById(R.id.ETlistView);

    lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    lv.setItemsCanFocus(false);

    //enable user to scroll
    lv.setFastScrollEnabled(true);

    final SimpleAdapter simpleAdapt = new SimpleAdapter(getActivity(), optionList, android.R.layout.simple_list_item_1, new String[]{"option"}, new int[]{android.R.id.text1});

    lv.setAdapter(simpleAdapt);

}

//method that adds options to the list
private void initList(){
    Log.d(TAG, "init List executed");
    optionList.add(createOption("option", "Food"));
    optionList.add(createOption("option", "Pong"));
    optionList.add(createOption("option", "Study"));
    optionList.add(createOption("option", "Workout"));
    optionList.add(createOption("option", "Meetup"));

    Collections.sort(optionList, new Alphabatize("option"));

}

public static HashMap<String, String> createOption(String key, String name){
    HashMap<String, String> option = new HashMap<String, String>();
    option.put(key, name);

    return option;
}

}

和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"
android:orientation="vertical"
tools:context="com.biggreenapps.needone.PickEventTypeFragment">

<TextView android:layout_width="match_parent" android:layout_height="match_parent"
    android:text="@string/hello_blank_fragment" />

<ListView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/ETlistView"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:choiceMode="multipleChoice" />


</LinearLayout>

1 个答案:

答案 0 :(得分:2)

<TextView android:layout_width="match_parent" android:layout_height="match_parent"
    android:text="@string/hello_blank_fragment" />

TextView占据了所有可用空间,因为它的高度为macht_parent,而ListView没有留出空间。将其删除或将其高度更改为wrap_content