@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView listView = findViewById(R.id.list_view); List<ImportViewModel> data = readExcelFile(); // 调用您的 readExcelFile() 方法获取数据 ImportViewModelAdapter adapter = new ImportViewModelAdapter(data); listView.setAdapter(adapter); TextView textView = findViewById(R.id.text_type); textView.setVisibility(View.GONE); TextView textView1 = findViewById(R.id.text_name); textView1.setVisibility(View.GONE); TextView textView2 = findViewById(R.id.text_style); textView2.setVisibility(View.GONE); TextView textView3 = findViewById(R.id.text_inventory_num); textView3.setVisibility(View.GONE); RfApplication.toneGenerator = new ToneGenerator(streamType, percantageVolume); btn_update = findViewById(R.id.btn_update); rvList = findViewById(R.id.list_view); rvList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) { ShowData item = (ShowData) adapterView.getItemAtPosition(i); if (!isStarted) { modeDialog(item); } else { CustomToast.showLoad(MainActivity.this, getString(R.string.please_stop_read), 2000); } return true; } }); // showDataList = new ArrayList<>(); //adapter = new StockInListViewAdapter(this, showDataList); rvList.setAdapter(adapter); }java.lang.IllegalStateException: Cannot get a numeric value from a text cell怎么解决
时间: 2023-12-10 20:40:39 浏览: 195
这个错误通常是因为您在尝试从 Excel 表格中读取文本单元格时,将其转换为数字或其他类型时发生的。您可以通过在读取数据之前检查单元格类型并据此进行类型转换来解决此问题。以下是一个示例代码片段,演示如何处理这个问题:
```
private List<ImportViewModel> readExcelFile() {
List<ImportViewModel> data = new ArrayList<>();
try {
InputStream input = getAssets().open("data.xlsx");
Workbook workbook = WorkbookFactory.create(input);
Sheet sheet = workbook.getSheetAt(0);
for (Row row : sheet) {
ImportViewModel item = new ImportViewModel();
Cell cell0 = row.getCell(0);
if (cell0.getCellType() == CellType.NUMERIC) {
item.setId((int) cell0.getNumericCellValue());
} else {
item.setId(Integer.parseInt(cell0.getStringCellValue()));
}
Cell cell1 = row.getCell(1);
item.setName(cell1.getStringCellValue());
Cell cell2 = row.getCell(2);
item.setStyle(cell2.getStringCellValue());
Cell cell3 = row.getCell(3);
if (cell3.getCellType() == CellType.NUMERIC) {
item.setInventoryNum((int) cell3.getNumericCellValue());
} else {
item.setInventoryNum(Integer.parseInt(cell3.getStringCellValue()));
}
data.add(item);
}
input.close();
workbook.close();
} catch (Exception e) {
e.printStackTrace();
}
return data;
}
```
这里的代码通过调用 `getCellType()` 方法检查单元格类型,并根据需要使用 `getNumericCellValue()` 或 `getStringCellValue()` 方法获取值。这应该能够解决您遇到的问题。
阅读全文