如何使用其他类变量 [英] How to use variables in other classes
本文介绍了如何使用其他类变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是一个有点失去了对这个。试图通过变量到另一个类的输出。该类获取变量。
Im a little lost on this. Trying to pass variables onto another class for output. This class gets the variable.
OneWeekPlan_Start_Btn.java
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在下面的类
I want to be able to use the variable wk1day1_inst in the following class
Save_File.java
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;
}
}
更具体地说我希望能够使用这个变量而S preadsheet。即
More specifically I want to be able to use that variable for the spreadsheet. ie
c1 = row.createCell(0);
c1.setCellValue(wk1day1_inst);
c1.setCellStyle(cs);
感谢您的帮助!
推荐答案
您可以在两个活动之间使用意图传递数据。
You can pass data between two activities using Intent.
Intent intent = new Intent (OneWeekPlan_Start_Btn.this, Save_File.class);
intent.putExtra("wk1day1_inst", wk1day1_ins);
startActivity(intent);
而在你的下一个活动,使用:
And in your next Activity, use this:
String wk1day1_inst = getIntent().getStringExtra("wk1day1_inst");
希望这有助于。
这篇关于如何使用其他类变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文