Java 在片段中使用数据库

Java 在片段中使用数据库,java,android,mysql,android-fragments,Java,Android,Mysql,Android Fragments,我正在尝试制作一个基本的歌词写作应用程序,它可以让你保存、编辑和存储歌词——但在项目进行到一半时,我决定使用不同的选项卡,这样用户可以在编辑歌词等功能时单击另一个选项卡。我有很多不正确的代码,我知道这是因为我切换到了一个扩展Fragment的类……我只是很难重写所有的东西。有人能告诉我,如果有一些小的调整,我可以做吗?关于这些调整可能是什么,有什么建议吗?谢谢你的帮助!!这是我的密码: public class LyricListFragment extends Fragment { priv

我正在尝试制作一个基本的歌词写作应用程序,它可以让你保存、编辑和存储歌词——但在项目进行到一半时,我决定使用不同的选项卡,这样用户可以在编辑歌词等功能时单击另一个选项卡。我有很多不正确的代码,我知道这是因为我切换到了一个扩展Fragment的类……我只是很难重写所有的东西。有人能告诉我,如果有一些小的调整,我可以做吗?关于这些调整可能是什么,有什么建议吗?谢谢你的帮助!!这是我的密码:

public class LyricListFragment extends Fragment {

private static final int ACTIVITY_CREATE=0;
private static final int ACTIVITY_EDIT=1;
private static final int INSERT_ID = Menu.FIRST;
private static final int DELETE_ID = Menu.FIRST + 1;
private LyricsDbAdapter mDbHelper;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_lyriclist);
    mDbHelper = new LyricsDbAdapter(getActivity());
    mDbHelper.open();
    fillData();
    registerForContextMenu(getListView());
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.activity_lyriclist, container, false);
    return view;
}

public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    super.onCreateOptionsMenu(menu);
    menu.add(0, INSERT_ID, 0, R.string.menu_insert);
    return true;
}

private void fillData() {
    Cursor LyricsCursor = mDbHelper.fetchAllLyrics();
    startManagingCursor(LyricsCursor);

    String[] from = new String[]{LyricsDbAdapter.KEY_TITLE};

    int[] to = new int[]{R.id.text1};

    SimpleCursorAdapter lyrics = 
        new SimpleCursorAdapter(getActivity(), R.layout.lyrics_row, LyricsCursor, from, to);
    setListAdapter(lyrics);
}

public boolean onMenuItemSelected(int featureId, MenuItem item) {
    switch(item.getItemId()) {
        case INSERT_ID:
            createLyric();
            return true;
    }
    return super.onMenuItemSelected(featureId, item);
}

public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.add(0, DELETE_ID, 0, R.string.menu_delete);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        case DELETE_ID:
            AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
            mDbHelper.deleteLyric(info.id);
            fillData();
            return true;
    }
    return super.onContextItemSelected(item);
}

private void createLyric() {
    Intent i = new Intent(getActivity(), LyricEditorFragment.class);
    startActivityForResult(i, ACTIVITY_CREATE);
}

protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Intent i = new Intent(getActivity(), LyricEditorFragment.class);
    i.putExtra(LyricsDbAdapter.KEY_ROWID, id);
    startActivityForResult(i, ACTIVITY_EDIT);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    fillData();
}
}

您可以在
片段中使用
TabHost
。托管选项卡不需要
TabActivity
TabActivity
只提供一些功能,您也可以自己添加到
片段中。请查看文档,特别是关于的部分

您还可以检查和