我正在尝试在片段中显示SQLite数据库中的列表。 我已经使用一个活动尝试了它并且它是成功的,但是使用片段我无法使用片段使其工作。
public class AppleFragment extends ListFragment {
/** An array of items to display in ArrayList */
String apple_versions[] = new String[]{
"Mountain Lion",
"Lion",
"Snow Leopard",
"Leopard",
"Tiger"
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/** Creating array adapter to set data in listview */
ArrayAdapter<String> adapter = new ArrayAdapter<String> (getActivity().getBaseContext(), android.R.layout.simple_list_item_multiple_choice, apple_versions);
/** Setting the array adapter to the listview */
setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onStart() {
super.onStart();
/** Setting the multiselect choice mode for the listview */
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
}
这只是一个例子,我想将其转换为SQLite列表并将其显示在片段中。
答案 0 :(得分:0)
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, apple_versions);
setListAdapter(adapter);
}
不要使用onCreateView,使用片段的onActivityCreated或onViewCreated方法加载列表。