gpt4 book ai didi

java - 在 fragment 中使用数据库

转载 作者:行者123 更新时间:2023-11-29 03:01:23 26 4
gpt4 key购买 nike

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

最佳答案

您可以在 Fragment 中使用 TabHost 就好了。 TabActivity 不是托管选项卡所必需的。 TabActivity 只提供一些您也可以自己添加到 Fragment 的功能。查看TabHost文档,尤其是关于 addTab() 的部分.

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

关于java - 在 fragment 中使用数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23124610/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com