ListView 没有显示在片段中?
ListView not being displayed within fragment?
我有一个片段,我在其中调用了一个函数,该函数应该填充和显示在片段的 xml 中定义的列表视图,但由于某种原因 none 文本出现在列表显示。我的所有日志都在控制台中正确输出,因此我知道选项列表已正确填充。这是我的代码。任何帮助将不胜感激!!
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>
<TextView android:layout_width="match_parent" android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
这个 TextView
占用了所有可用的 space,因为它的高度是 macht_parent
,ListView
没有留下 space。删除它或将其高度更改为 wrap_content
我有一个片段,我在其中调用了一个函数,该函数应该填充和显示在片段的 xml 中定义的列表视图,但由于某种原因 none 文本出现在列表显示。我的所有日志都在控制台中正确输出,因此我知道选项列表已正确填充。这是我的代码。任何帮助将不胜感激!!
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>
<TextView android:layout_width="match_parent" android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
这个 TextView
占用了所有可用的 space,因为它的高度是 macht_parent
,ListView
没有留下 space。删除它或将其高度更改为 wrap_content