Call a method when opening the application

Asked

Viewed 78 times

0

I have the following class

public class ReadXlsx extends Activity {
    public TextView txtcoluna1;
    public TextView txtcoluna2;
    public TextView txtcoluna3;
    public TextView txtcoluna4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_read_xlsx);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); //Seta a tela para o layout retrato
        View botaoexemplo = findViewById(R.id.botaoexemplo);
        txtcoluna1 = (TextView) findViewById(R.id.txtcoluna1);
        txtcoluna2 = (TextView) findViewById(R.id.txtcoluna2);
        txtcoluna3 = (TextView) findViewById(R.id.txtcoluna3);  
        txtcoluna4 = (TextView) findViewById(R.id.txtcoluna4);  
    }

    public void ler(View view) throws IOException, BiffException, WriteException{

        //Setando Parâmetros
        Workbook workbook = Workbook.getWorkbook(new File("sdcard/AperamApps/DBQ/DBQmestre/speed.xls"));
        // Abrir Planilha
        Sheet sheet = workbook.getSheet(0);
        // Contas as linhas
        int linhas = sheet.getRows();
        //Informa o inicio da leitura
        System.out.println("Iniciando a leitura da planilha XLS:");
        for(int i = 0; i < linhas; i++){
        // Captura o valor das celulas ([Coluna], [linha])
            Cell b1 = sheet.getCell(1, 0);
            Cell b2 = sheet.getCell(1, 1);
            Cell b3 = sheet.getCell(1, 2);
            Cell b4 = sheet.getCell(1, 3);
            Cell b5 = sheet.getCell(1, 4);
            Cell b6 = sheet.getCell(1, 5);
            Cell b7 = sheet.getCell(1, 6);
            Cell b8 = sheet.getCell(1, 7);
            Cell b9 = sheet.getCell(1, 8);
        //Converter  valores das celulas para strings
            String as1 = b1.getContents();
            String as2 = b2.getContents();
            String as3 = b3.getContents(); 
            String as4 = b4.getContents(); 
            String as5 = b5.getContents(); 
            String as6 = b6.getContents(); 
            String as7 = b7.getContents(); 
            String as8 = b8.getContents(); 
            String as9 = b9.getContents(); 
        //Imprime os resutados

          txtcoluna1.setText("#1 | "+ as1);
          txtcoluna2.setText("#2 | "+ as2);
          txtcoluna3.setText("#3 | "+ as3);
          txtcoluna4.setText("#4 | "+ as4);

        // insere dados na planilha filtrada
        Workbook pfiltro = Workbook.getWorkbook(new File("sdcard/AperamApps/DBQ/DBQmestre/p-filtrada.xls"));
        WritableWorkbook outfiltro = Workbook.createWorkbook(new File("sdcard/AperamApps/DBQ/DBQmestre/out-filtrada.xls"), pfiltro);    
        pfiltro.close();
        Toast.makeText(getApplicationContext(), "Teste de função", Toast.LENGTH_SHORT).show();
        WritableSheet sheet1 = outfiltro.getSheet("filtro"); 
        WritableCell cell = sheet1.getWritableCell(0, 0); 
        WritableCell cella2 = sheet1.getWritableCell(0, 1); 
        WritableCell cella3 = sheet1.getWritableCell(0, 2); 
        WritableCell cella4 = sheet1.getWritableCell(0, 3); 
        WritableCell cella5 = sheet1.getWritableCell(0, 4);     
        WritableCell cella6 = sheet1.getWritableCell(0, 5); 
        WritableCell cella7 = sheet1.getWritableCell(0, 6); 
        WritableCell cella8 = sheet1.getWritableCell(0, 7); 
        WritableCell cella9 = sheet1.getWritableCell(0, 8); 

        if (cell.getType() == CellType.LABEL) 
        { 
          Label l = (Label) cell; 
          l.setString(as1); 

        }

        if (cell.getType() == CellType.LABEL) 
        { 
          Label l = (Label) cella2; 
          l.setString(as2); 
        }

        if (cell.getType() == CellType.LABEL) 
        { 
          Label l = (Label) cella3; 
          l.setString(as3); 
        }

        if (cell.getType() == CellType.LABEL) 
        { 
          Label l = (Label) cella4; 
          l.setString(as4); 
        }

        if (cell.getType() == CellType.LABEL) 
        { 
          Label l = (Label) cella5; 
          l.setString(as5); 
        }

        if (cell.getType() == CellType.LABEL) 
        { 
          Label l = (Label) cella6; 
          l.setString(as6); 
        }

        if (cell.getType() == CellType.LABEL) 
        { 
          Label l = (Label) cella7; 
          l.setString(as7); 
        }

        if (cell.getType() == CellType.LABEL) 
        { 
          Label l = (Label) cella8; 
          l.setString(as8); 
        }

        if (cell.getType() == CellType.LABEL) 
        { 
          Label l = (Label) cella9; 
          l.setString(as9); 
        }

        // Escreve na planilha filtrada
        outfiltro.write();
        //Fecha a planilha de entrada de dados
        pfiltro.close();
        }
        workbook.close();
        }; 

        public void salvar1 (View view) throws IOException, BiffException, WriteException {

            Workbook workbook = Workbook.getWorkbook(new File("sdcard/AperamApps/DBQ/DBQmestre/speed.xls"));
            WritableWorkbook copy = Workbook.createWorkbook(new File("sdcard/AperamApps/DBQ/DBQmestre/speed2.xls"), workbook);


            WritableSheet sheet2 = copy.getSheet("speed"); 
            WritableCell cell = sheet2.getWritableCell(1, 2); 

            if (cell.getType() == CellType.LABEL) 
            { 
              Label l = (Label) cell; 
              l.setString("Teste"); 
            }
            copy.write(); 
            copy.close();
            workbook.close();

            Toast.makeText(getApplicationContext(), "Teste de função", Toast.LENGTH_SHORT).show();
        }
    }

How do I so that when the user opens the application, it automatically runs the "read method" ?

  • I think what you want is to call the read method in the application class. To do this you need to create a class that understands Aplication and in Oncreate you call that read. Don’t forget to say in xml your application Class.

  • 1

    In the method onCreate() call the method ler()

  • Good afternoon, thank you both, I used the formula of Ramaral for finding it easier. It worked perfectly.

1 answer

0

Solved, according to the response of the Ramaral user

In the onCreate() method call the read method()

  • 1

    The downside is that every time there is a configuration change (for example, if you rotate the screen) the method Activity.onCreate() will be called again and with it the method ler(). So depending on the situation the option to call the method in Application.onCreate() might be more interesting.

  • I understand, however this method will have to be calling constantly even, because it will take data from a spreadsheet that updates every 30 seconds, the application also works only in Landscape mode.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.