我试图通过SQLite
从片段中保存数据,但我不能。
实际上,save和read方法运行良好,但如果片段被更改并重新加载,那么任何数据都不存在。
这是我的代码。
DB
public class ToDoItemsDB {
private static final String dbName = "ToDoItemsDB.db";
private static final String tableName = "todo_items";
private DBHelper dbHelper;
private SQLiteDatabase db;
private Context context;
public ToDoItemsDB(Context context) {
this.context = context;
this.dbHelper = new DBHelper(context, null, null, 1);
db = dbHelper.getWritableDatabase();
}
public void getAllItems() {
String sql = "select * from " + tableName;
Cursor cursor = db.rawQuery(sql, null);
while(cursor.moveToNext()) {
System.out.println("result");
System.out.println(cursor.getInt(1));
}
cursor.close();
}
public void addItems(int categoryID, String itemName, int itemID, String subItemName, int subItemID) {
String sql = "insert into "
+ tableName
+ " values("
+ null
+ ","
+ categoryID
+ "," + "'"
+ itemName + "'"
+ ","
+ itemID
+ "," + "'"
+ subItemName + "'"
+ ","
+ subItemID
+")";
db.execSQL(sql);
}
private class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table "
+ tableName
+ "(_id integer primary key autoincrement,"
+ "category_id integer,"
+ "item_name text,"
+ "item_id integer,"
+ "sub_item_name text,"
+ "sub_item_id integer"
+ ")";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exist " + tableName);
onCreate(db);
}
}
MainActivity
public class MainActivity extends Activity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
/**
* Used to store the last screen title. For use in {@link #restoreActionBar()}.
*/
private CharSequence mTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
@Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
Fragment fragment;
FragmentManager fragmentManager = getFragmentManager();
switch(position) {
case 0:
fragment = new ToDoMainActivity();
break;
case 1:
fragment = new SettingActivity();
break;
default:
fragment = new ToDoMainActivity();
}
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
}
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.title_section1);
break;
case 2:
mTitle = getString(R.string.title_section2);
break;
}
}
public void restoreActionBar() {
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.to_do_main, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_to_do_main, container, false);
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
}
}
ToDoMainActivity
public class ToDoMainActivity extends Fragment implements OnClickListener {
private Context context;
private ToDoItemsDB DB;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_to_do_main, container, false);
Button btn = (Button)view.findViewById(R.id.addCategory);
Button getBtn = (Button)view.findViewById(R.id.getAllItem);
btn.setOnClickListener(this);
getBtn.setOnClickListener(this);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
System.out.println("Acitivity Created");
context = getActivity();
}
@Override
public void onResume() {
super.onResume();
System.out.println("On Resume");
DB = new ToDoItemsDB(context);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.addCategory:
Toast.makeText(context, "SET", Toast.LENGTH_SHORT).show();
DB.addItems(3, "test", 3, null, 3);
break;
case R.id.getAllItem:
Toast.makeText(context, "GET", Toast.LENGTH_SHORT).show();
DB.getAllItems();
break;
}
}
}