这是我的活动。我陷入了困境,需要帮助。 我需要读取数据,将它们作为关键字并生成地图<>。通过按键从用户获取密钥显示密钥的结果,然后在用户批准时将另一个按钮添加到列表中。
app.get('/*',(req,res) => {
// send file as response
// __dirname -- > points to current director
res.sendFile(path.join(__dirname,'./index.html'));
})
我需要在main()之后访问Buttons和TextViews。我尝试了几种方法但到目前为止没有成功。 我需要在main之后使用Buttons和TextViews。我需要地图<>在使用它们之前。 我试过让它们静态或在onCreate()中定义但是没有成功。我感谢任何帮助。
我在得到答案后做了这个编辑 这是用作方法的Java文件
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();
}
}
}
我做了一个调试,工作簿总是为null,我在活动中得到null。
这就是我在活动中称之为
的方式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;
}
答案 0 :(得分:2)
确保您正确理解活动生命周期。结帐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 方法
中的所有视图实例答案 1 :(得分:0)
Do like this
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<>();
private TextView productInput, productName ;
private Button checkInput , addInput ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reader_excel);
productName = findViewById(R.id.textView_ProductName);
productInput = findViewById(R.id.textView_InputProductNumber);
checkInput = findViewById(R.id.button_GO);
addInput = findViewById(R.id.button_AddProduct);
workBookCalculation();
}
public void workBookCalculation() 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();
}
}
并在活动中的任何位置使用textview;