나는 이것에 조금 잃었다. 출력을 위해 변수를 다른 클래스로 전달하려고합니다. 이 클래스는 변수를 가져옵니다.
OneWeekPlan_Start_Btn.java
package com.th3ramr0d.prtmanager;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class OneWeekPlan_Start_Btn extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.oneweek_day1);
EditText inputTxt = (EditText) findViewById(R.id.wkOneDayOneInstructorName);
String wk1day1_inst = inputTxt.getText().toString();
Button oneweekDay2 = (Button) findViewById(R.id.wk1Day2);
oneweekDay2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.th3ramr0d.prtmanager.ONEWEEKDAYTWO"));
}
});
}
}
다음 클래스에서 wk1day1_inst 변수를 사용할 수 있기를 원합니다.
Save_File.java
package com.th3ramr0d.prtmanager;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import android.content.Context;
import android.os.Environment;
import android.util.Log;
import android.view.View.OnClickListener;
public class Save_File extends Activity implements OnClickListener{
Button writeExcelButton;
static String TAG = "ExelLog";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.save_file);
writeExcelButton = (Button) findViewById(R.id.wk1Save);
writeExcelButton.setOnClickListener(this);
}
public void onClick(View v)
{
EditText inputTxt = (EditText) findViewById(R.id.wkOneSaveFile);
String fileName = inputTxt.getText().toString();
switch (v.getId())
{
case R.id.wk1Save:
saveExcelFile(this,fileName + ".xls");
}
setContentView(R.layout.created);
}
private static boolean saveExcelFile(Context context, String fileName) {
// check if available and not read only
if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) {
Log.e(TAG, "Storage not available or read only");
return false;
}
boolean success = false;
//New Workbook
Workbook wb = new HSSFWorkbook();
Cell c1 = null;
//Cell style for header row
CellStyle cs = wb.createCellStyle();
cs.setFillForegroundColor(HSSFColor.LIME.index);
cs.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
//New Sheet
Sheet sheet1 = null;
sheet1 = wb.createSheet("My PT Plan");
// Generate column headings
Row row = sheet1.createRow(0);
c1 = row.createCell(0);
c1.setCellValue("FUCK YOU SCIENCE!");
c1.setCellStyle(cs);
c1 = row.createCell(1);
c1.setCellValue("Quantity");
c1.setCellStyle(cs);
c1 = row.createCell(2);
c1.setCellValue("Price");
c1.setCellStyle(cs);
sheet1.setColumnWidth(0, (15 * 500));
sheet1.setColumnWidth(1, (15 * 500));
sheet1.setColumnWidth(2, (15 * 500));
// Create a path where we will place our List of objects on external storage
File file = new File(context.getExternalFilesDir(null), fileName);
FileOutputStream os = null;
try {
os = new FileOutputStream(file);
wb.write(os);
Log.w("FileUtils", "Writing file" + file);
success = true;
} catch (IOException e) {
Log.w("FileUtils", "Error writing " + file, e);
} catch (Exception e) {
Log.w("FileUtils", "Failed to save file", e);
} finally {
try {
if (null != os)
os.close();
} catch (Exception ex) {
}
}
return success;
}
public static boolean isExternalStorageReadOnly() {
String extStorageState = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) {
return true;
}
return false;
}
public static boolean isExternalStorageAvailable() {
String extStorageState = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(extStorageState)) {
return true;
}
return false;
}
}
더 구체적으로 스프레드 시트에 해당 변수를 사용할 수 있기를 원합니다. 즉
c1 = row.createCell(0);
c1.setCellValue(wk1day1_inst);
c1.setCellStyle(cs);
도움을 주셔서 감사합니다!
Intent를 사용하여 두 활동간에 데이터를 전달할 수 있습니다.
Intent intent = new Intent (OneWeekPlan_Start_Btn.this, Save_File.class);
intent.putExtra("wk1day1_inst", wk1day1_ins);
startActivity(intent);
다음 활동에서 다음을 사용하십시오.
String wk1day1_inst = getIntent().getStringExtra("wk1day1_inst");
도움이 되었기를 바랍니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다