user2847219 user2847219 - 26 days ago 17
Android Question

Android error when import csv

I'm trying to import all the data from the CSV file and overwrite those in the table in SQLite database. I have this method but I get error:

java.lang.IllegalArgumentException: Bad class: class java.lang.String





public void onClick(DialogInterface dialog, int which) {

File root = Environment.getExternalStorageDirectory();
File exportDir = new File(root.getAbsolutePath());
File csvFile = new File(exportDir,"myfile.csv");
try {
deleteTable();
ContentValues cv = new ContentValues();
CSVReader dataRead = new CSVReader(new FileReader(csvFile));
String[] vv = null;
while((vv = dataRead.readNext())!=null) {
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" );
String strDate = sdf.format( vv[0] );
cv.clear();
cv.put(Table.DATA,strDate);
cv.put(Table.A,vv[1]);
cv.put(Table.B,vv[2]);
cv.put(Table.C,vv[3]);
cv.put(Table.D,vv[4]);
SQLiteDatabase db = mHelper.getWritableDatabase();
db.insert(MyTable.TABLE_NAME,null,cv);

}
dataRead.close();

} catch (Exception e) {

}
}

Answer
String strDate = sdf.format( vv[0] );

You can't use SimpleDateFormat.format() to format a String.

Use format() for converting Date objects to Strings. Use parse() to convert Strings to Date objects.

Comments