如何在其他类中使用变量

时间:2014-11-21 15:09:40

标签: java android

我有点迷失了。尝试将变量传递到另一个类以进行输出。这个类获取变量。

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

感谢您的帮助!

2 个答案:

答案 0 :(得分:3)

您可以使用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");

希望这有帮助。

答案 1 :(得分:0)

您可以使用SharedPreferences来保存变量的值。它优于putExtra Intent的优势在于,使用SharedPreferences,可以在任何活动中访问变量的值,而Intent只允许访问1个活动中的变量。

//Save the value in OneWeekPlan_Start_Btn class
SharedPreferences.Editor editor = settings.edit();
editor.putString("weekday", wk1day1_inst);
editor.commit();

//Access the value in Save_File class
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
String wk1day = settings.getString("weekday", "");