我正在尝试将解析的数据存储在我的应用程序中,但即使我已经初始化了类,也没有创建数据库。这是我的代码:
public class Schedule extends ListActivity{
// Flag for Internet connection status
Boolean isInternetPresent = false;
// Alert Dialog Manager
AlertDialogManager alert = new AlertDialogManager();
ConnectionDetector cd;
// All static variables
static final String URL = "url address";
// XML node keys
static final String KEY_ITEM = "group_header"; // Parent node
static final String KEY_NAME = "name"; //Group Number
static final String KEY_SUNM = "day_sun_mrg";
static final String KEY_SUNE = "day_sun_evn";
static final String KEY_MONM = "day_mon_mrg";
static final String KEY_MONE = "day_mon_evn";
static final String KEY_TUEM = "day_tue_mrg";
static final String KEY_TUEE = "day_tue_evn";
static final String KEY_WEDM = "day_wed_mrg";
static final String KEY_WEDE = "day_wed_evn";
static final String KEY_THUM = "day_thu_mrg";
static final String KEY_THUE = "day_thu_evn";
static final String KEY_FRIM = "day_fri_mrg";
static final String KEY_FRIE = "day_fri_evn";
static final String KEY_SATM = "day_sat_mrg";
static final String KEY_SATE = "day_sat_evn";
public static ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
// Creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Calling SQlite Process
DBAdapter dbc=new DBAdapter(this); //Here I am doing my class call
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // Getting XML
Document doc = parser.getDomElement(xml); // Getting DOM element
cd = new ConnectionDetector(getApplicationContext());
// Check if Internet present
isInternetPresent = cd.isOnline();
if (!isInternetPresent) {
// Internet Connection is not present
alert.showAlertDialog(Schedule.this, "Internet Connection Error",
"Please connect to working Internet connection", false);
// stop executing code by return
return;
}
else {
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
// Looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
Element e = (Element) nl.item(i);
// Adding each child node to HashMap key => value
map.put(KEY_NAME, parser.getValue(e,KEY_NAME));
map.put(KEY_SUNM,parser.getValue(e, KEY_SUNM));
map.put(KEY_SUNE, parser.getValue(e,KEY_SUNE));
map.put(KEY_MONM,parser.getValue(e, KEY_MONM));
map.put(KEY_MONE, parser.getValue(e,KEY_MONE));
map.put(KEY_TUEM,parser.getValue(e, KEY_TUEM));
map.put(KEY_TUEE, parser.getValue(e,KEY_TUEE));
map.put(KEY_WEDM,parser.getValue(e, KEY_WEDM));
map.put(KEY_WEDE, parser.getValue(e,KEY_WEDE));
map.put(KEY_THUM,parser.getValue(e, KEY_THUM));
map.put(KEY_THUE, parser.getValue(e,KEY_THUE));
map.put(KEY_FRIM,parser.getValue(e, KEY_FRIM));
map.put(KEY_FRIE, parser.getValue(e,KEY_FRIE));
map.put(KEY_SATM,parser.getValue(e, KEY_SATM));
map.put(KEY_SATE, parser.getValue(e,KEY_SATE));
// Adding HashList to ArrayList
menuItems.add(map);
}
}
}
}
这是我的 DatabaseActivity 类
public class DBAdapter{
// Contacts Table Columns names
public static final String KEY_ROWID = "_id";
private static final String KEY_NAME = "group_id";
private static final String KEY_SUNM = "day_sun_mrg";
private static final String KEY_SUNE = "day_sun_evn";
private static final String KEY_MONM = "day_mon_mrg";
private static final String KEY_MONE = "day_mon_evn";
private static final String KEY_TUEM = "day_tue_mrg";
private static final String KEY_TUEE = "day_tue_evn";
private static final String KEY_WEDM = "day_wed_mrg";
private static final String KEY_WEDE = "day_wed_evn";
private static final String KEY_THUM = "day_thu_mrg";
private static final String KEY_THUE = "day_thu_evn";
private static final String KEY_FRIM = "day_fri_mrg";
private static final String KEY_FRIE = "day_fri_evn";
private static final String KEY_SATM = "day_sat_mrg";
private static final String KEY_SATE = "day_sat_evn";
private static final String TAG = "DBAdapter";
// Database Name
private static final String DATABASE_NAME = "name";
// Contacts table name
private static final String DATABASE_TABLE= "schedule";
// Database Version
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE ="create table schedules (_id integer primary key autoincrement,group_id text,day_sun_mrg text ,day_sun_evn text ,day_mon_mrg text,day_mon_evn text ,day_tue_mrg text,day_tue_evn text,day_wed_mrg text ,day_wed_evn text ,day_thu_mrg text,day_thu_evn text ,day_fri_mrg text ,day_fri_evn text ,day_sat_mrg text,day_sat_evn text )";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
private static final String TAG = null;
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion)
{
Log.e(TAG, "Upgrading database from version " + oldVersion
+ " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS titles");
onCreate(db);
}
}
//---Opens the database---
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
//---closes the database---
public void close()
{
DBHelper.close();
}
public void insertintoDatabase( String sunm) {
// TODO Auto-generated method stub
ContentValues values = new ContentValues();
values.put(KEY_SUNM, sunm); //Sunday Morning
db.insert(DATABASE_TABLE, null, values);
}
//---Deletes a particular title---
public boolean deleteTitle(long group)
{
return db.delete(DATABASE_TABLE, KEY_NAME +
"=" + group, null) > 0;
}
//---Retrieves all the titles---
public Cursor getAllTitles()
{
return db.query(DATABASE_TABLE, new String[] {
KEY_NAME,
KEY_SUNE,
KEY_SUNM,
KEY_SUNE,KEY_MONM,KEY_MONE,KEY_TUEM,
KEY_TUEE,KEY_WEDM,KEY_WEDE,KEY_THUM,
KEY_THUE,KEY_FRIM,KEY_FRIE,KEY_SATM,KEY_SATE},
null,
null,
null,
null,
null);
}
//---Retrieves a particular title---
public Cursor getTitle(long group) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {
KEY_NAME,
KEY_SUNE,
KEY_SUNM,
KEY_SUNE,KEY_MONM,KEY_MONE,KEY_TUEM,
KEY_TUEE,KEY_WEDM,KEY_WEDE,KEY_THUM,
KEY_THUE,KEY_FRIM,KEY_FRIE,KEY_SATM,KEY_SATE
},
KEY_NAME + "=" + group,
null,
null,
null,
null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
//---Updates a title---
public boolean updateTitle(long group, String sunm, String sune, String monm,
String mone, String tuem, String tuee, String wedm, String wede,
String thum, String thue, String frim, String frie, String satm,
String sate)
{
ContentValues args = new ContentValues();
args.put(KEY_NAME,group); // Group number
args.put(KEY_SUNM, sunm); // Sunday morning
args.put(KEY_SUNM, sune); // Sunday evening
args.put(KEY_MONM, monm);
args.put(KEY_MONE, mone);
args.put(KEY_TUEM, tuem);
args.put(KEY_TUEE, tuem);
args.put(KEY_WEDM, wedm);
args.put(KEY_WEDE, wede);
args.put(KEY_THUM, thum);
args.put(KEY_THUE, thue);
args.put(KEY_FRIM, frim);
args.put(KEY_FRIE, frie);
args.put(KEY_SATM, satm);
args.put(KEY_SATE, sate);
return db.update(DATABASE_TABLE, args,
KEY_NAME + "=" + group, null) > 0;
}
public long insertData(String sunm, String sune, String monm, String mone,
String tuem, String tuee, String wedm, String wede, String thum,
String thue, String frim, String frie, String satm, String sate,
String string) {
// TODO Auto-generated method stub
ContentValues initialValues = new ContentValues();
//initialValues.put(KEY_NAME,group); // Group number
initialValues.put(KEY_SUNM, sunm); //Sunday morning
initialValues.put(KEY_SUNM, sune);// Sunday evening
initialValues.put(KEY_MONM, monm);
initialValues.put(KEY_MONE, mone);
initialValues.put(KEY_TUEM, tuem);
initialValues.put(KEY_TUEE, tuem);
initialValues.put(KEY_WEDM, wedm);
initialValues.put(KEY_WEDE, wede);
initialValues.put(KEY_THUM, thum);
initialValues.put(KEY_THUE, thue);
initialValues.put(KEY_FRIM, frim);
initialValues.put(KEY_FRIE, frie);
initialValues.put(KEY_SATM, satm);
initialValues.put(KEY_SATE, sate);
return db.insert(DATABASE_TABLE, null, initialValues);
}
}