我正在使用简单的记事本应用。我在我的片段中添加了一个数据库,但是当我运行应用程序时它没有出现。
这是我的片段代码。一切似乎工作正常但应用程序上没有任何内容。我在onActivityCreated
。
public class TomorrowFragment extends Fragment {
NDb myNDb;
public TomorrowFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_tomorrow, container, false);
}
/**
* how i called my DataBase
*/
@Override
public void onActivityCreated( Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
myNDb = new NDb(getActivity());
}
}
这是数据库代码:
public NDb(Context context) {
super(context, dbname, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table mynotes"
+ "(_id integer primary key, name text,remark text,dates text)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + mynotes);
onCreate(db);
}
public Cursor fetchAll() {
db = this.getReadableDatabase();
Cursor mCursor = db.query(mynotes, new String[] { "_id", "name",
"dates", "remark" }, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public boolean insertNotes(String name, String dates, String remark) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name", name);
contentValues.put("dates", dates);
contentValues.put("remark", remark);
db.insert(mynotes, null, contentValues);
return true;
}
public Cursor getData(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor z = db.rawQuery("select * from " + mynotes + " where _id=" + id
+ "", null);
return z;
}
public int numberOfRows() {
SQLiteDatabase db = this.getReadableDatabase();
int numRows = (int) DatabaseUtils.queryNumEntries(db, mynotes);
return numRows;
}
public boolean updateNotes(Integer id, String name, String dates,
String remark) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name", name);
contentValues.put("dates", dates);
contentValues.put("remark", remark);
db.update(mynotes, contentValues, "_id = ? ",
new String[] { Integer.toString(id) });
return true;
}
public Integer deleteNotes(Integer id) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(mynotes, "_id = ? ",
new String[] { Integer.toString(id) });
}
public ArrayList getAll() {
ArrayList array_list = new ArrayList();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("select * from " + mynotes, null);
res.moveToFirst();
while (res.isAfterLast() == false) {
array_list.add(res.getString(res.getColumnIndex("_id")));
array_list.add(res.getString(res.getColumnIndex(remark)));
array_list.add(res.getString(res.getColumnIndex(dates)));
array_list.add(res.getString(res.getColumnIndex(name)));
res.moveToNext();
}
return array_list;
}
}