dtrodriguez
dtrodriguez

Reputation: 91

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

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

}
}

I want to be able to use the variable wk1day1_inst in the following class

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


}

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

Thanks for any help!

Upvotes: 0

Views: 124

Answers (2)

gegobyte
gegobyte

Reputation: 5545

You can use SharedPreferences to save value of variable. It's advantage over putExtra of Intent is that with SharedPreferences the value of variable can be accessed inside any activity while Intent only allows to access variable in 1 activity.

//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", "");

Upvotes: 0

MysticMagicϡ
MysticMagicϡ

Reputation: 28823

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

Hope this helps.

Upvotes: 3

Related Questions