Search code examples
androidlistviewandroid-fragmentsandroid-linearlayoutsimpleadapter

ListView not being displayed within fragment?


I have a fragment in which I call a function that is supposed to populate and display a listview defined in the fragment's xml, but for some reason none of the text is appearing in the listview. All of my logs are outputting correctly in the console, so I know that the optionlist is getting populated correctly. Here's my code. Any help would be greatly appreciated!!

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

}

And the xml file:

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

Solution

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

    this TextView is taking all the space available since its height is macht_parent leaving not space for the ListView. Remove it or change its height to wrap_content