这是我的 Activity 。我陷入 Activity 中,需要帮助。 我需要读取数据,将它们作为键并生成 map <>。通过按下按钮从用户处获取 key ,显示 key 的结果,然后如果用户批准,则使用另一个按钮将它们添加到列表中。
public class ExcelReaderActivity extends AppCompatActivity {
public static final String SAMPLE_XLSX_FILE_PATH = "/home/saeid/Documents/ABBREV.xlsx";
public static final Map<String, String> numberDescMap = new HashMap<>();
public final TextView productInput = findViewById(R.id.textView_InputProductNumber);
public final TextView productName = findViewById(R.id.textView_ProductName);
public final Button checkInput = findViewById(R.id.button_GO);
public final Button addInput = findViewById(R.id.button_AddProduct);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reader_excel);
}
public static void main(String[] args) throws IOException, InvalidFormatException {
// Creating a Workbook from an Excel file (.xls or .xlsx)
Workbook workbook = WorkbookFactory.create(new File(SAMPLE_XLSX_FILE_PATH));
// Retrieving the number of sheets in the Workbook
System.out.println("Workbook has " + workbook.getNumberOfSheets() + " Sheets : ");
// obtain a sheetIterator and iterate over it
Iterator<Sheet> sheetIterator = workbook.sheetIterator();
System.out.println("Retrieving Sheets using Iterator");
while (sheetIterator.hasNext()) {
Sheet sheet = sheetIterator.next();
System.out.println("=> " + sheet.getSheetName());
}
// Getting the Sheet at index zero
Sheet sheet = workbook.getSheetAt(0);
Row rowCount = sheet.getRow(0);
int totalNumberOfRows = sheet.getPhysicalNumberOfRows();
int totalNumberOfColumns = rowCount.getPhysicalNumberOfCells();
System.out.print("number of rows => " + totalNumberOfRows + "\n");
System.out.print("number of columns => " + totalNumberOfColumns + "\n");
// Create a DataFormatter to format and get each cell's value as String
DataFormatter dataFormatter = new DataFormatter();
// obtain a rowIterator and columnIterator and iterate over them
System.out.println("\n\nIterating over Rows and Columns using Iterator\n");
Iterator<Row> rowIterator = sheet.rowIterator();
for (int currentRow = 1; currentRow < totalNumberOfRows; currentRow++){
Row row = rowIterator.next();
row = sheet.getRow(currentRow);
Cell cell0 = row.getCell(0);
Cell cell1 = row.getCell(1);
String cellValue0 = dataFormatter.formatCellValue(cell0);
String cellValue1 = dataFormatter.formatCellValue(cell1);
numberDescMap.put(cellValue0,cellValue1);
//System.out.print(currentRow + "- *" + dbNumbers + "*" + "*" + description + "*" + "\n");
}
List<String> dbNumbers = new ArrayList(numberDescMap.keySet());
List<String> description = new ArrayList(numberDescMap.values());
int checkIndex = 1;
for (String mapkeys : dbNumbers){
System.out.println(checkIndex++ + "- " + mapkeys + "==>" +numberDescMap.get(mapkeys) + "\n");
}
// Closing the workbook
workbook.close();
}
}
我需要在 main() 之后访问按钮和 TextView。我尝试了多种方法但到目前为止还没有成功。 我需要在 main 中的结果之后使用按钮和 TextView。在使用它们之前我需要 map <>。 我尝试将它们设置为静态或在 onCreate() 中定义,但没有成功。我很感激任何帮助。
我在得到答案后进行了此编辑 这是充当方法的 Java 文件
public class FileReader{
private final Activity activity;
public FileReader(Activity activity) {this.activity = activity;
}
public static final String SAMPLE_XLSX_FILE_PATH = "/home/saeid/Documents/ABBREV.xlsx";
public static final Map<String, String> numberDescMap = new HashMap<>();
public Map<String, String> ExcelFileReader() {
// Creating a Workbook from an Excel file (.xls or .xlsx)
Workbook workbook = null;
try {
workbook = WorkbookFactory.create(new File(SAMPLE_XLSX_FILE_PATH));
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
}
// obtain a sheetIterator and iterate over it
Iterator<Sheet> sheetIterator = workbook.sheetIterator();
System.out.println("Retrieving Sheets using Iterator");
while (sheetIterator.hasNext()) {
Sheet sheet = sheetIterator.next();
//System.out.println("=> " + sheet.getSheetName());
}
// Getting the Sheet at index zero
Sheet sheet = workbook.getSheetAt(0);
Row rowCount = sheet.getRow(0);
int totalNumberOfRows = sheet.getPhysicalNumberOfRows();
int totalNumberOfColumns = rowCount.getPhysicalNumberOfCells();
System.out.println("number of rows => " + totalNumberOfRows );
System.out.println("number of columns => " + totalNumberOfColumns );
// Create a DataFormatter to format and get each cell's value as String
DataFormatter dataFormatter = new DataFormatter();
// 1. You can obtain a rowIterator and columnIterator and iterate over them
System.out.println("\n\nIterating over Rows and Columns using Iterator\n");
Iterator<Row> rowIterator = sheet.rowIterator();
for (int currentRow = 1; currentRow < totalNumberOfRows; currentRow++) {
Row row = rowIterator.next();
row = sheet.getRow(currentRow);
Cell cell0 = row.getCell(0);
Cell cell1 = row.getCell(1);
String cellValue0 = dataFormatter.formatCellValue(cell0);
String cellValue1 = dataFormatter.formatCellValue(cell1);
numberDescMap.put(cellValue0, cellValue1);
//System.out.print(currentRow + "- *" + dbNumbers + "*" + "*" + description + "*" + "\n");
}
// Closing the workbook
try {
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
return numberDescMap;
}
}
我进行了调试,工作簿始终为空,并且我在 Activity 中得到空值。
这就是我在 Activity 中的称呼
FileReader fileReader = new FileReader(this);
final Map<String, String> numberDescMap = fileReader.ExcelFileReader();
最佳答案
确保您正确理解 Activity 生命周期。看看这个link
您不需要使用public static void main
public class ExcelReaderActivity extends AppCompatActivity {
public static final String SAMPLE_XLSX_FILE_PATH = "/home/saeid/Documents/ABBREV.xlsx";
public static final Map<String, String> numberDescMap = new HashMap<>();
public final TextView productInput ;
public final TextView productName;
public final Button checkInput ;
public final Button addInput;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reader_excel);
productInput = findViewById(R.id.textView_InputProductNumber);
productName = findViewById(R.id.textView_ProductName);
checkInput = findViewById(R.id.button_GO);
addInput = findViewById(R.id.button_AddProduct);
createWorkbook();
}
createWorkbook(){
// Creating a Workbook from an Excel file (.xls or .xlsx)
Workbook workbook = WorkbookFactory.create(new File(SAMPLE_XLSX_FILE_PATH));
// Retrieving the number of sheets in the Workbook
System.out.println("Workbook has " + workbook.getNumberOfSheets() + " Sheets : ");
// obtain a sheetIterator and iterate over it
Iterator<Sheet> sheetIterator = workbook.sheetIterator();
System.out.println("Retrieving Sheets using Iterator");
while (sheetIterator.hasNext()) {
Sheet sheet = sheetIterator.next();
System.out.println("=> " + sheet.getSheetName());
}
// Getting the Sheet at index zero
Sheet sheet = workbook.getSheetAt(0);
Row rowCount = sheet.getRow(0);
int totalNumberOfRows = sheet.getPhysicalNumberOfRows();
int totalNumberOfColumns = rowCount.getPhysicalNumberOfCells();
System.out.print("number of rows => " + totalNumberOfRows + "\n");
System.out.print("number of columns => " + totalNumberOfColumns + "\n");
// Create a DataFormatter to format and get each cell's value as String
DataFormatter dataFormatter = new DataFormatter();
// obtain a rowIterator and columnIterator and iterate over them
System.out.println("\n\nIterating over Rows and Columns using Iterator\n");
Iterator<Row> rowIterator = sheet.rowIterator();
for (int currentRow = 1; currentRow < totalNumberOfRows; currentRow++){
Row row = rowIterator.next();
row = sheet.getRow(currentRow);
Cell cell0 = row.getCell(0);
Cell cell1 = row.getCell(1);
String cellValue0 = dataFormatter.formatCellValue(cell0);
String cellValue1 = dataFormatter.formatCellValue(cell1);
numberDescMap.put(cellValue0,cellValue1);
//System.out.print(currentRow + "- *" + dbNumbers + "*" + "*" + description + "*" + "\n");
}
List<String> dbNumbers = new ArrayList(numberDescMap.keySet());
List<String> description = new ArrayList(numberDescMap.values());
int checkIndex = 1;
for (String mapkeys : dbNumbers){
System.out.println(checkIndex++ + "- " + mapkeys + "==>" +numberDescMap.get(mapkeys) + "\n");
}
// Closing the workbook
workbook.close();
}
}
您可以使用createWorkbook方法中的所有 View 实例
关于java - 如何在静态 main 中或之后定义 Button 和 TextView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48664599/