android fragment 在列表中显示sqlite数据

标签 android sqlite android-fragments

我正在尝试在 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/

相关文章:

java - 如何从处理程序中删除未分配给变量的 Runnable

android - Android模拟器中上网

android - 在两个 fragment 之间添加一个监听器

android - FragmentNavigator 为透明背景添加 Fragment 而不是替换

java - 从加速度计找到手机的角度?

android - 谷歌地图问题并获得子 fragment 管理器

c# - 使用SQLite DB在C#中创建Microsoft报表

iphone - 在单例模式下使用 sqlite 时何时打开和关闭连接

android - Sqlite数据库中,如何在表中同时插入多行

android - 如何修复 InstantiationException,无法实例化 Fragment?