ListView listView = findViewById(R.id.lisT_l); listView.setAdapter(adapter);
时间: 2024-05-18 13:15:39 浏览: 15
这段代码是在当前Activity中找到一个id为lisT_l的ListView,并将之前创建的adapter设置为它的适配器。通过findViewById()方法可以获取到布局文件中定义的控件,而ListView的setAdapter()方法则将adapter与ListView关联起来,使得ListView能够正确地显示数据。当adapter中的数据发生变化时,ListView也会相应地更新显示内容。
相关问题
private Intent intent = null; void initialize() { inventoryButton = findViewById(R.id.inventoryButton); findButton = findViewById(R.id.findButton); settingButton = findViewById(R.id.settingButton); exitButton = findViewById(R.id.exitButton); languageButton = findViewById(R.id.languageButton); inventoryButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // Intent intent = new Intent(HomeActivity.this, HomeActivity.class); if (ButtonOnCilk.isFastViewClick(inventoryButton, getBaseContext())) { setContentView(R.layout.activity_main); CustomToast.showLoad(HomeActivity.this, getString(R.string.load)); startActivity(intent); // 读取 Excel 文件 List<String> data = readExcelFile(); listView = findViewById(R.id.rv_list); // 将数据显示在 ListView 中 ArrayAdapter<String> adapter = new ArrayAdapter<>(HomeActivity.this, android.R.layout.simple_list_item_1, data); listView.setAdapter(adapter); } } });有什么问题,怎么解决
这段代码的问题在于,`intent` 变量没有被初始化,而且在 `onClick` 方法中没有被赋值,因此会导致启动 `Activity` 失败。你可以将 `intent` 的初始化放在 `initialize` 方法中,并在 `onClick` 方法中赋值,修改后的代码如下:
```
private Intent intent = null;
void initialize() {
inventoryButton = findViewById(R.id.inventoryButton);
findButton = findViewById(R.id.findButton);
settingButton = findViewById(R.id.settingButton);
exitButton = findViewById(R.id.exitButton);
languageButton = findViewById(R.id.languageButton);
// 初始化 intent
intent = new Intent(HomeActivity.this, HomeActivity.class);
inventoryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (ButtonOnCilk.isFastViewClick(inventoryButton, getBaseContext())) {
setContentView(R.layout.activity_main);
CustomToast.showLoad(HomeActivity.this, getString(R.string.load));
// 在 onClick 方法中赋值
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
// 读取 Excel 文件
List<String> data = readExcelFile();
listView = findViewById(R.id.rv_list);
// 将数据显示在 ListView 中
ArrayAdapter<String> adapter = new ArrayAdapter<>(HomeActivity.this, android.R.layout.simple_list_item_1, data);
listView.setAdapter(adapter);
}
}
});
}
```
另外,你需要确保 `readExcelFile()` 方法已经正确实现,否则也可能会导致启动 `Activity` 失败。
@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怎么解决
这个错误通常是因为您在尝试从 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()` 方法获取值。这应该能够解决您遇到的问题。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)