在片段中使用数据库

时间:2014-04-17 04:28:47

标签: java android mysql android-fragments

我试图创建一个基本的歌词编写应用程序,允许您保存,编辑和存储歌词 - 但是在项目的中途,我已经决定要使用不同的标签,以便用户可以在编辑歌词等功能时单击另一个选项卡。我有很多不正确的代码,我知道这是因为我切换到了扩展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();
}
}

1 个答案:

答案 0 :(得分:1)

您可以在TabHost内使用Fragment就好了。托管标签不需要TabActivityTabActivity仅提供一些您也可以自己添加到Fragment的功能。查看TabHost文档,尤其是关于addTab()的部分。

您还可以查看Android Fragements with TabsAndroid Tabs with ListFragments