我正在尝试在 fragment 中显示 SQLite 数据库中的列表。 我已经使用 Activity 进行了尝试,并且成功了,但是使用 Fragment 我无法使其使用 Fragment 工作。
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列表并在 fragment 中显示。
最佳答案
@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,使用fragment的onActivityCreated或onViewCreated方法来加载列表。
关于android fragment 在列表中显示sqlite数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29564238/