解析したデータをデータベースに保存しようとしていますが、インスタンス化の例外があります。Parcelable を使用して、解析された arraylist をあるアクティビティから別のアクティビティに渡します。それらの arraylist をあるアクティビティから別のアクティビティに渡し、それを sqlite に保存する必要があります。
public class AndroidXMLParsingActivity extends ListActivity implements Parcelable{
//private ScheduleClient scheduleClient;
public Calendar cal;
//TextView desciption1;
TextView name1;
private GetData mGetData;
// All static variables
static final String URL = "http address";
// XML node keys
static final String KEY_ITEM = "group_header"; // parent node
static String KEY_NAME = "name";
static String KEY_ID = "group_day";
static String KEY_COST = "day_sun_mrg";
static String KEY_DESC = "day_sun_evn";
static final String KEY_COST1 = "day_mon_mrg";
static final String KEY_DESC1 = "day_mon_evn";
static final String KEY_COST2 = "day_tue_mrg";
static final String KEY_DESC2 = "day_tue_evn";
static final String KEY_COST3 = "day_wed_mrg";
static final String KEY_DESC3 = "day_wed_evn";
static final String KEY_COST4 = "day_thu_mrg";
static final String KEY_DESC4 = "day_thu_evn";
static final String KEY_COST5 = "day_fri_mrg";
static final String KEY_DESC5 = "day_fri_evn";
static final String KEY_COST6 = "day_sat_mrg";
static final String KEY_DESC6 = "day_sat_evn";
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);
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
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_ID, parser.getValue(e, KEY_ID));
map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
map.put(KEY_COST,parser.getValue(e, KEY_COST));
map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
map.put(KEY_COST1,parser.getValue(e, KEY_COST1));
map.put(KEY_DESC1, parser.getValue(e,KEY_DESC1));
map.put(KEY_COST2,parser.getValue(e, KEY_COST2));
map.put(KEY_DESC2, parser.getValue(e,KEY_DESC2));
map.put(KEY_COST3,parser.getValue(e, KEY_COST3));
map.put(KEY_DESC3, parser.getValue(e,KEY_DESC3));
map.put(KEY_COST4,parser.getValue(e, KEY_COST4));
map.put(KEY_DESC4, parser.getValue(e,KEY_DESC4));
map.put(KEY_COST5,parser.getValue(e, KEY_COST5));
map.put(KEY_DESC5, parser.getValue(e,KEY_DESC5));
map.put(KEY_COST6,parser.getValue(e, KEY_COST6));
map.put(KEY_DESC6, parser.getValue(e,KEY_DESC6));
// adding HashList to ArrayList
menuItems.add(map);
//Passing Data to Store Page
mGetData.addLoad(menuItems);
//putStringArrayListExtra(menuItems, ArrayList<String> menuItems);
}
// Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(this, menuItems,
R.layout.list_item,
new String[] { KEY_NAME, KEY_DESC, KEY_COST }, new int[] {
R.id.name, R.id.desciption, R.id.cost});
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
String cost = ((TextView) view.findViewById(R.id.cost)).getText().toString();
String description = ((TextView) view.findViewById(R.id.desciption)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(KEY_NAME, name);
in.putExtra(KEY_COST, cost);
in.putExtra(KEY_DESC, description);
startActivity(in);
}
});
}
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
dest.writeString(KEY_NAME);
dest.writeString(KEY_COST);
dest.writeString(KEY_DESC);
}
public AndroidXMLParsingActivity(Parcel source){
KEY_NAME=source.readString();
KEY_COST=source.readString();
KEY_DESC=source.readString();
source.readArrayList(getClassLoader());
}
そして、私のsqliteコードは次のとおりです[WORKS]
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 = "sunm";
private static final String KEY_SUNE = "sune";
private static final String KEY_MONM = "monm";
private static final String KEY_MONE = "mone";
private static final String KEY_TUEM = "tuem";
private static final String KEY_TUEE = "tuee";
private static final String KEY_WEDM = "wedm";
private static final String KEY_WEDE = "wede";
private static final String KEY_THUM = "thum";
private static final String KEY_THUE = "thue";
private static final String KEY_FRIM = "frim";
private static final String KEY_FRIE = "frie";
private static final String KEY_SATM = "satm";
private static final String KEY_SATE = "sate";
AsyncTask sch;
// Database Name
private static final String DATABASE_NAME = "DB_NAME";
// Contacts table name
private static final String DATABASE_TABLE= "TABLE_NAME";
// Database Version
private static final int DATABASE_VERSION = 1;
// Creating Tables
private static final String DATABASE_CREATE ="create table if not exists TABLE_NAME (_id integer primary key autoincrement,group_id text,sunm text ,sune text ,monm text,mone text ,tuem text,tuee text,wedm text ,wede text ,thum text,thue text ,frim text ,frie text ,satm text,sate text )";
private final Context context;
private DatabaseHelper DBHelper;
private static 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);
}
@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 schedules");
onCreate(db);
}
}
//---opens the database---
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
//---closes the database---
public void close()
{
DBHelper.close();
}
//Inserting data into the table
public void insertData(String group_id, 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) {
try{
Cursor cur = db.rawQuery("SELECT COUNT(*) FROM schedules", null);
if (cur!=null && cur.getCount() > 0){
cur.moveToFirst();
if (cur.getInt(0) <= 6) {//put in these statements contained in array
Log.i("DB getCount", " getcount less than 0, should read array");//log message
String insertQuery ="INSERT INTO schedules(group_id,sunm,sune,monm,mone,tuem,tuee,wedm,wede,thum,thue,frim,frie," +
"satm,sate)"+ "VALUES" + "("+"'"+group_id+"'"+","+"'"+sunm+"'"+","+"'"+sune+"'"+","+"'"+monm+"'"+"," +
""+"'"+mone+"'"+","+"'"+tuem+"'"+","+"'"+tuee+"'"+","+"'"+wedm+"'"+","+"'"+wede+"'"+","+"'"+thum+"'"+"," +
""+"'"+thue+"'"+","+"'"+frim+"'"+","+"'"+frie+"'"+","+"'"+satm+"'"+","+"'"+sate+"'"+")";
db.execSQL(insertQuery);
Log.e("a", ""+cur.getCount());
}
}
else{
//do nothing everything's as it should be
Log.i("DB getCount", " getcount greater than 0"); //log message
}
cur.close();
db.close();
}
catch(SQLiteException e){System.err.println("Exception @ rawQuery: " + e.getMessage());}
}
//---retrieves all the title---
public Cursor getAllTitles()
{
return db.query(DATABASE_TABLE, new String[] {KEY_ROWID,KEY_NAME,KEY_SUNM,KEY_SUNE,KEY_MONM,KEY_MONE,KEY_MONM,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, null);
}
//---retrieves a particular title---
public ScheduleItem getSelectedItem(String group){
ScheduleItem schedule = new ScheduleItem();
// Select All Query
String searchQuery = "select * from schedules where group_id = '"+group+"'";
Log.e("DBAdapter", group);
try{
//Database not open error
Cursor cur = db.rawQuery(searchQuery, null);
if (cur!=null && cur.getCount() > 0){
cur.moveToFirst();
//Setting DB value to model scheduleitem.java
schedule.setName(cur.getString(cur.getColumnIndex("group_id")));
schedule.setSunm(cur.getString(cur.getColumnIndex("sunm")));
schedule.setSune(cur.getString(cur.getColumnIndex("sune")));
schedule.setMonm(cur.getString(cur.getColumnIndex("monm")));
schedule.setMone(cur.getString(cur.getColumnIndex("mone")));
schedule.setTuem(cur.getString(cur.getColumnIndex("tuem")));
schedule.setTuee(cur.getString(cur.getColumnIndex("tuee")));
schedule.setWedm(cur.getString(cur.getColumnIndex("wedm")));
schedule.setWede(cur.getString(cur.getColumnIndex("wede")));
schedule.setThum(cur.getString(cur.getColumnIndex("thum")));
schedule.setThue(cur.getString(cur.getColumnIndex("thue")));
schedule.setFrim(cur.getString(cur.getColumnIndex("frim")));
schedule.setFrie(cur.getString(cur.getColumnIndex("frie")));
schedule.setSatm(cur.getString(cur.getColumnIndex("satm")));
schedule.setSate(cur.getString(cur.getColumnIndex("sate")));
}
cur.close();
db.close();
return schedule;
}catch(SQLiteException e){System.err.println("Exception @ rawQuery: " + e.getMessage());}
return schedule;
}
public void scheduleupdate(){
Log.e(DATABASE_NAME, "Data Removed");
//String delQuery="DELETE FROM TABLE schedules";
context.deleteDatabase(DATABASE_NAME);
//db.execSQL(delQuery);
db.close();
}
}
助けて Logcat エラー
09-18 13:11:03.362: E/AndroidRuntime(471): FATAL EXCEPTION: main
09-18 13:11:03.362: E/AndroidRuntime(471): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.mokshya.multinodeparse/com.mokshya.multinodeparse.parse.AndroidXMLParsingActivity}: java.lang.InstantiationException:
09-18 13:11:03.362: E/AndroidRuntime(471): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
09-18 13:11:03.362: E/AndroidRuntime(471): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-18 13:11:03.362: E/AndroidRuntime(471): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-18 13:11:03.362: E/AndroidRuntime(471): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-18 13:11:03.362: E/AndroidRuntime(471): at android.os.Handler.dispatchMessage(Handler.java:99)
09-18 13:11:03.362: E/AndroidRuntime(471): at android.os.Looper.loop(Looper.java:123)
09-18 13:11:03.362: E/AndroidRuntime(471): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-18 13:11:03.362: E/AndroidRuntime(471): at java.lang.reflect.Method.invokeNative(Native Method)
09-18 13:11:03.362: E/AndroidRuntime(471): at java.lang.reflect.Method.invoke(Method.java:521)
09-18 13:11:03.362: E/AndroidRuntime(471): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-18 13:11:03.362: E/AndroidRuntime(471): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-18 13:11:03.362: E/AndroidRuntime(471): at dalvik.system.NativeStart.main(Native Method)
09-18 13:11:03.362: E/AndroidRuntime(471): Caused by: java.lang.InstantiationException: com.mokshya.multinodeparse.parse.AndroidXMLParsingActivity
09-18 13:11:03.362: E/AndroidRuntime(471): at java.lang.Class.newInstanceImpl(Native Method)
09-18 13:11:03.362: E/AndroidRuntime(471): at java.lang.Class.newInstance(Class.java:1429)
09-18 13:11:03.362: E/AndroidRuntime(471): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
09-18 13:11:03.362: E/AndroidRuntime(471): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
09-18 13:11:03.362: E/AndroidRuntime(471): ... 11 more
com.mokshya.multinodeparse.parse.AndroidXMLParsingActivity