これは私の以前の質問hereからの続きです。データベース用のコードを作成しましたが、アプリを実行してもデータベースが作成されません。どこで間違ったのかわかりませんが、Eclipse ではコードにエラーは表示されません。ここに私のアプリからの私のコードがあります。
MainActivity.java
public class MainActivity extends ListActivity {
static final String URL = "http://data.bmkg.go.id/propinsi_15_2.xml";
static final String KEY_ITEM = "Cuaca";
static final String KEY_ID = "Isi";
static final String KEY_ROW = "Row";
static final String KEY_KOTA = "Kota";
static final String KEY_LINTANG = "Lintang";
static final String KEY_BUJUR = "Bujur";
static final String KEY_CUACA = "Cuaca";
static final String KEY_SUHUMIN = "SuhuMin";
static final String KEY_SUHUMAX = "SuhuMax";
static final String KEY_KELEMBAPANMIN = "KelembapanMin";
static final String KEY_KELEMBAPANMAX = "KelembapanMax";
static final String KEY_KECEPATANANGIN = "KecepatanAngin";
static final String KEY_ARAHANGIN = "ArahAngin";
private ProgressDialog pDialog;
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String,String>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new AmbilData().execute();
DatabaseHandler db = new DatabaseHandler(this);
}
class AmbilData extends AsyncTask<String, String, String>{
protected void onPreExecute(){
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading, Mohon Menunggu Beberapa saat...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected String doInBackground (String... args){
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL);
Document doc = parser.getDomElement(xml);
NodeList nl = doc.getElementsByTagName(KEY_ROW);
for (int i = 0; i < nl.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
map.put(KEY_KOTA, parser.getValue(e, KEY_KOTA));
map.put(KEY_LINTANG, parser.getValue(e, KEY_LINTANG));
map.put(KEY_BUJUR, parser.getValue(e, KEY_BUJUR));
map.put(KEY_CUACA, parser.getValue(e, KEY_CUACA));
map.put(KEY_SUHUMIN, parser.getValue(e, KEY_SUHUMIN));
map.put(KEY_SUHUMAX, parser.getValue(e, KEY_SUHUMAX));
map.put(KEY_KELEMBAPANMIN, parser.getValue(e, KEY_KELEMBAPANMIN));
map.put(KEY_KELEMBAPANMAX, parser.getValue(e, KEY_KELEMBAPANMAX));
map.put(KEY_KECEPATANANGIN, parser.getValue(e, KEY_KECEPATANANGIN));
map.put(KEY_ARAHANGIN, parser.getValue(e, KEY_ARAHANGIN));
menuItems.add(map);
}
return null;
}
protected void onPostExecute(String file_url){
pDialog.dismiss();
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
ListAdapter adapter = new SimpleAdapter(MainActivity.this, menuItems, R.layout.list_kota, new String[] {KEY_KOTA, KEY_CUACA, KEY_LINTANG, KEY_BUJUR, KEY_SUHUMIN, KEY_SUHUMAX, KEY_KELEMBAPANMIN, KEY_KELEMBAPANMAX, KEY_KECEPATANANGIN, KEY_ARAHANGIN}, new int[] {R.id.kota, R.id.cuaca, R.id.lintang, R.id.bujur, R.id.suhumin, R.id.suhumax, R.id.kelembapanmin, R.id.kelembapanmax, R.id.kecepatanangin, R.id.arahangin});
setListAdapter(adapter);
}
});
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
KabupatenList.java
public class KabupatenList {
// private variables
int _Row;
String _Kota;
String _Lintang;
String _Bujur;
String _Cuaca;
String _SuhuMin;
String _SuhuMax;
String _KelembapanMin;
String _KelembapanMax;
String _KecepatanAngin;
String _ArahAngin;
// Empty constructor
public KabupatenList() {
}
// constructor
public KabupatenList(int Row, String Kota, String Lintang, String Bujur,
String Cuaca, String SuhuMin, String SuhuMax, String KelembapanMin,
String KelembapanMax, String KecepatanAngin, String ArahAngin) {
this._Row = Row;
this._Kota = Kota;
this._Lintang = Lintang;
this._Bujur = Bujur;
this._Cuaca = Cuaca;
this._SuhuMin = SuhuMin;
this._SuhuMax = SuhuMax;
this._KelembapanMin = KelembapanMin;
this._KelembapanMax = KelembapanMax;
this._KecepatanAngin = KecepatanAngin;
this._ArahAngin = ArahAngin;
}
// constructor
public KabupatenList(String Kota, String Lintang, String Bujur,
String Cuaca, String SuhuMin, String SuhuMax, String KelembapanMin,
String KelembapanMax, String KecepatanAngin, String ArahAngin) {
this._Kota = Kota;
this._Lintang = Lintang;
this._Bujur = Bujur;
this._Cuaca = Cuaca;
this._SuhuMin = SuhuMin;
this._SuhuMax = SuhuMax;
this._KelembapanMin = KelembapanMin;
this._KelembapanMax = KelembapanMax;
this._KecepatanAngin = KecepatanAngin;
this._ArahAngin = ArahAngin;
}
// getting row
public int getRow() {
return this._Row;
}
// setting row
public void setRow(int Row) {
this._Row = Row;
}
// getting kota
public String getKota() {
return this._Kota;
}
// setting kota
public void setKota(String Kota) {
this._Kota = Kota;
}
// getting lintang
public String getLintang() {
return this._Lintang;
}
// setting lintang
public void setLintang(String Lintang) {
this._Lintang = Lintang;
}
// getting bujur
public String getBujur() {
return this._Bujur;
}
// setting bujur
public void setBujur(String Bujur) {
this._Bujur = Bujur;
}
// getting cuaca
public String getCuaca() {
return this._Cuaca;
}
// setting cuaca
public void setCuaca(String Cuaca) {
this._Cuaca = Cuaca;
}
// getting suhumin
public String getSuhuMin() {
return this._SuhuMin;
}
// setting suhumin
public void setSuhuMin(String SuhuMin) {
this._SuhuMin = SuhuMin;
}
// getting suhumax
public String getSuhuMax() {
return this._SuhuMax;
}
// setting suhumax
public void setSuhuMax(String SuhuMax) {
this._SuhuMax = SuhuMax;
}
// getting kelembapanmin
public String getKelembapanMin() {
return this._KelembapanMin;
}
// setting kelembapanmin
public void setKelembapanMin(String KelembapanMin) {
this._KelembapanMin = KelembapanMin;
}
// getting kelembapanmax
public String getKelembapanMax() {
return this._KelembapanMax;
}
// setting kelembapanmax
public void setKelembapanMax(String KelembapanMax) {
this._KelembapanMax = KelembapanMax;
}
// getting kecepatan angin
public String getKecepatanAngin() {
return this._KecepatanAngin;
}
// setting kecepatan angin
public void setKecepatanAngin(String KecepatanAngin) {
this._KecepatanAngin = KecepatanAngin;
}
// getting arah angin
public String getArahAngin() {
return this._ArahAngin;
}
// setting arah angin
public void setArahAngin(String ArahAngin) {
this._ArahAngin = ArahAngin;
}
}
DatabaseHandler.java
public class DatabaseHandler extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "dbKabupaten";
// Contacts table name
private static final String TABLE_KABUPATEN = "tbKabupaten";
// Contacts Table Columns names
private static final String KEY_ROW = "Row";
private static final String KEY_KOTA = "Kota";
private static final String KEY_LINTANG = "Lintang";
private static final String KEY_BUJUR = "Bujur";
private static final String KEY_CUACA = "Cuaca";
private static final String KEY_SUHUMIN = "SuhuMin";
private static final String KEY_SUHUMAX = "SuhuMax";
private static final String KEY_KELEMBAPANMIN = "KelembapanMin";
private static final String KEY_KELEMBAPANMAX = "KelembapanMax";
private static final String KEY_KECEPATANANGIN = "KecepatanAngin";
private static final String KEY_ARAHANGIN = "ArahAngin";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_KABUPATEN_TABLE = "CREATE TABLE " + TABLE_KABUPATEN + "("
+ KEY_ROW + " INTEGER PRIMARY KEY," + KEY_KOTA + " TEXT,"
+ KEY_LINTANG + " TEXT" + KEY_BUJUR + " TEXT" + KEY_CUACA + " TEXT" + KEY_SUHUMIN + " TEXT" + KEY_SUHUMAX + " TEXT" + KEY_KELEMBAPANMIN + " TEXT" + KEY_KELEMBAPANMAX + " TEXT" + KEY_KECEPATANANGIN + " TEXT" + KEY_ARAHANGIN + " TEXT" +")";
db.execSQL(CREATE_KABUPATEN_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_KABUPATEN);
// Create tables again
onCreate(db);
}
/**
* All CRUD(Create, Read, Update, Delete) Operations
*/
// Adding new contact
void addKabupatenList(KabupatenList kabupatenList) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_KOTA, kabupatenList.getKota()); // Kota
values.put(KEY_LINTANG, kabupatenList.getLintang()); // Lintang
values.put(KEY_BUJUR, kabupatenList.getBujur()); //Bujur
values.put(KEY_CUACA, kabupatenList.getCuaca()); //Cuaca
values.put(KEY_SUHUMIN, kabupatenList.getSuhuMin()); //SuhuMin
values.put(KEY_SUHUMAX, kabupatenList.getSuhuMax()); //SuhuMax
values.put(KEY_KELEMBAPANMIN, kabupatenList.getKelembapanMin()); //KelembapanMin
values.put(KEY_KELEMBAPANMAX, kabupatenList.getKelembapanMax()); //KelembapanMax
values.put(KEY_KECEPATANANGIN, kabupatenList.getKecepatanAngin()); //KecepatanAngin
values.put(KEY_ARAHANGIN, kabupatenList.getArahAngin()); //ArahAngin
// Inserting Row
db.insert(TABLE_KABUPATEN, null, values);
db.close(); // Closing database connection
}
// Getting single contact
KabupatenList getKabupatenList(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_KABUPATEN, new String[] { KEY_ROW,
KEY_KOTA, KEY_LINTANG, KEY_BUJUR, KEY_CUACA, KEY_SUHUMIN, KEY_SUHUMAX, KEY_KELEMBAPANMIN, KEY_KELEMBAPANMAX, KEY_KECEPATANANGIN, KEY_ARAHANGIN }, KEY_ARAHANGIN + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
KabupatenList kabupatenList = new KabupatenList();
// return contact
return kabupatenList;
}
// Getting All Contacts
public List<KabupatenList> getKabupaten() {
List<KabupatenList> kabupaten = new ArrayList<KabupatenList>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_KABUPATEN;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
KabupatenList kabupatenList = new KabupatenList();
kabupatenList.setRow(Integer.parseInt(cursor.getString(0)));
kabupatenList.setKota(cursor.getString(1));
kabupatenList.setLintang(cursor.getString(2));
kabupatenList.setBujur(cursor.getString(3));
kabupatenList.setCuaca(cursor.getString(4));
kabupatenList.setKelembapanMin(cursor.getString(5));
kabupatenList.setKelembapanMax(cursor.getString(6));
kabupatenList.setKecepatanAngin(cursor.getString(7));
kabupatenList.setArahAngin(cursor.getString(8));
// Adding contact to list
kabupaten.add(kabupatenList);
} while (cursor.moveToNext());
}
// return contact list
return kabupaten;
}
// Updating single contact
public int updateKabupaten(KabupatenList kabupatenList) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_KOTA, kabupatenList.getKota());
values.put(KEY_LINTANG, kabupatenList.getLintang());
values.put(KEY_BUJUR, kabupatenList.getBujur());
values.put(KEY_CUACA, kabupatenList.getCuaca());
values.put(KEY_SUHUMIN, kabupatenList.getSuhuMin());
values.put(KEY_SUHUMAX, kabupatenList.getSuhuMax());
values.put(KEY_KELEMBAPANMIN, kabupatenList.getKelembapanMin());
values.put(KEY_KELEMBAPANMAX, kabupatenList.getKelembapanMax());
values.put(KEY_KECEPATANANGIN, kabupatenList.getKecepatanAngin());
values.put(KEY_ARAHANGIN, kabupatenList.getArahAngin());
// updating row
return db.update(TABLE_KABUPATEN, values, KEY_ROW + " = ?",
new String[] { String.valueOf(kabupatenList.getRow()) });
}
// Deleting single contact
public void deleteKabupaten(KabupatenList kabupatenList) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_KABUPATEN, KEY_ROW + " = ?",
new String[] { String.valueOf(kabupatenList.getRow()) });
db.close();
}
// Getting contacts Count
public int getKabupatenCount() {
String countQuery = "SELECT * FROM " + TABLE_KABUPATEN;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
}
アプリがxmlを解析してSQLiteに保存できるようにするにはどうすればよいですか。コードに何か問題がありますか、それともコードを変更する必要がありますか?