Sunteți pe pagina 1din 3

java - Chamar um mtodo ao abrir o aplicativo - S...

http://pt.stackoverow.com/questions/94454/cha...

registrar

Stack Overflow em Portugus um site de perguntas e respostas para programadores profissionais e entusiastas. 100% gratuito
e no requer registro.

log-in

tour

ajuda

Registrar-se

Chamar um mtodo ao abrir o aplicativo


tenho a seguinte classe
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 Parmetros
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
txtcoluna2.setText("#2
txtcoluna3.setText("#3
txtcoluna4.setText("#4

|
|
|
|

"+
"+
"+
"+

as1);
as2);
as3);
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 funo",
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);

1 de 3

23/11/2015 17:35

java - Chamar um mtodo ao abrir o aplicativo - S...

http://pt.stackoverow.com/questions/94454/cha...

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();

2 de 3

23/11/2015 17:35

java - Chamar um mtodo ao abrir o aplicativo - S...

http://pt.stackoverow.com/questions/94454/cha...

Toast.makeText(getApplicationContext(), "Teste de funo",


Toast.LENGTH_SHORT).show();
}
}

Como eu fao para que quando o usurio abrir o aplicativo, ele execute automaticamente o
mtodo "ler" ?
java

android

eclipse

editada 25/10 s 14:26

perguntada 25/10 s 12:52

ramaral
11,6mil

Matheus Arruda
4

16

37

39

Penso o que voc quer chamar o mtodo ler na classe aplicao. Para tal tens de criar uma classe que
entende aplication e no Oncreate chamas esse ler. No te esqueas de dizer no xml a tua Class aplicao.
iGoDa 25/10 s 14:08
1

No mtodo onCreate() chame o mtodo ler() ramaral 25/10 s 14:28


Boa tarde, muito obrigado aos dois, utilizei a formula do Ramaral por achar mais fcil. Funcionou
perfeitamente. Matheus Arruda 25/10 s 15:17

1 Resposta

Resolvido, de acordo com a resposta do usurio Ramaral


No mtodo onCreate() chame o mtodo ler()
respondida 25/10 s 15:27
Matheus Arruda
39

A desvantagem que toda vez que houver uma mudana de configurao (por exemplo, se voc girar a
tela) o mtodo Activity.onCreate() voltar a ser chamado e com ele o mtodo ler() . Por isso
dependendo da situao a opo de chamar o mtodo em Application.onCreate() pode ser mais
interessante. Piovezan 25/10 s 15:33
Entendo, porem esse mtodo ter que ser chamando constantemente mesmo, pois ele ira pegar dados de
uma planilha que atualiza a cada 30 segundos, o aplicativo tambm s funciona em modo landscape.
Matheus Arruda 25/10 s 15:35

3 de 3

23/11/2015 17:35

S-ar putea să vă placă și