2014-04-19 153 views
0

我试图从CSV文件导入所有数据并覆盖SQLite数据库中的表中的数据。我有这样的方法,但我得到的错误:导入csv时出现Android错误

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) { 

} 
} 

回答

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

不能使用SimpleDateFormat.format()格式化String

使用format()Date对象转换为String s。使用parse()String s转换为Date对象。