Getting the value in EditText Android - java

I am having a problem getting the value in the editText of my program. I set the value in a textView so that I can see if the code gets it. But unfortunately it displays something like this:
android.widget.EditText#410e5a58 -> the number after # sign changes every run in the emulator
Why this happens? This is my code:
package com.example.ITax;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
/**
* Created with IntelliJ IDEA.
* User: Karla Mae Jaro
* Date: 12/3/12
* Time: 3:58 PM
* To change this template use File | Settings | File Templates.
*/
public class AnnualComputation extends MyActivity
{
String civil_status2;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.annual_computation);
Bundle extras = getIntent().getExtras();
if(extras != null)
{
civil_status2 = extras.getString("user_status");
}
final Button btn_compute = (Button) findViewById(R.id.btn_compute_from_annual);
final Button btn_back = (Button) findViewById(R.id.btn_back_from_annual_computation);
btn_back.setOnClickListener(new Button.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent i = new Intent(getApplicationContext(), OpenChoices.class);
startActivity(i);
}
});
btn_compute.setOnClickListener(new Button.OnClickListener()
{
#Override
public void onClick(View v)
{
final EditText net_salary = (EditText) findViewById(R.id.et_net_annual);
final EditText tax_due = (EditText) findViewById(R.id.et_taxdue);
final EditText tax_exe = (EditText) findViewById(R.id.et_taxexemption);
final TextView txt3 = (TextView) findViewById(R.id.textView3);
final TextView txt4 = (TextView) findViewById(R.id.textView4);
final TextView txt5 = (TextView) findViewById(R.id.textView5);
final TextView txt6 = (TextView) findViewById(R.id.textView6);
final TextView txt7 = (TextView) findViewById(R.id.textView7);
final TextView txt8 = (TextView) findViewById(R.id.textView8);
double netSalary, taxDue, rate = 0, exemption = 0, additional = 0, lowerlimit = 0, total;
String ns, td, r, e, a, t;
ns = net_salary.getText().toString();
netSalary = Double.parseDouble(ns);
/* Getting the tax exemption */
if ("SME".equals(civil_status2))
{
exemption = 50000;
}
else if ("SM1".equals(civil_status2))
{
exemption = 25000;
}
else if ("SM2".equals(civil_status2))
{
exemption = 50000;
}
else if ("SM3".equals(civil_status2))
{
exemption = 75000;
}
else if ("SM4".equals(civil_status2))
{
exemption = 100000;
}
/* Getting the rate, additional, lowerlimit */
if(netSalary <= 10000)
{
rate = 0.05;
}
else if((netSalary > 10000) && (netSalary <=30000))
{
rate = 0.1;
additional = 5000;
lowerlimit = 10000;
}
else if ((netSalary > 30000) && (netSalary <= 70000))
{
rate = 0.15;
additional = 2500;
lowerlimit = 30000;
}
else if((netSalary > 70000) && (netSalary <= 14000))
{
rate = 0.20;
additional = 8500;
lowerlimit = 70000;
}
else if ((netSalary > 140000) && (netSalary <= 250000))
{
rate = 0.25;
additional = 22500;
lowerlimit = 140000;
}
else if((netSalary > 250000) && (netSalary <= 500000))
{
rate = 0.30;
additional = 50000;
lowerlimit = 250000;
}
else if (netSalary > 500000)
{
rate = 0.32;
additional = 125000;
lowerlimit = 500000;
}
taxDue = netSalary - exemption;
total = taxDue - lowerlimit;
total = total * rate;
total = total + additional;
/* Converting exemption from Double to String */
td = String.valueOf(net_salary);
e = String.valueOf(exemption);
a = String.valueOf(additional);
r = String.valueOf(rate);
t = String.valueOf(total);
/* Placing the value to the editText (textbox) */
tax_due.setText(td);
tax_exe.setText(e);
txt3.setText(civil_status2);
txt4.setText(td);
txt5.setText(e);
txt6.setText(t);
txt7.setText(r);
txt8.setText(a);
}
});
}
}

use netSalary instead of net_salary
td = String.valueOf(netSalary);
As you are passing the edit text instead of the double variable

Try this,
tax_due.setText( net_salary.getText().toString());

If the problem is here:
td = String.valueOf(net_salary);
Then it is because you are trying to treat net_salary, a widget with a string value set in it, as if it were just a string. Try:
td = String.valueOf(net_salary.getText().toString());

You are passing an object of EditText(net_salary) in td so td will have the the string representation of your net_salary object.
You have stored your value in netSalary varibale so pass that instead :
td = String.valueOf(netSalary);
And for your information :
android.widget.EditText#410e5a58 is the string representation of your EditText object (i.e. calling toString method of your Edittext object will return this string) .
And value after # is the memory address of your object. And every time it changes because every time your object is created at different memory locations.

Related

Showing data in textView after going to next page

I am facing difficulties in showing data in textView after going to the next page
I am storing the return value in EMI variable but I am not able to print that value in the next page textview.
public class EmiCalculator extends AppCompatActivity {
public static double emical(double p,double r, double t)
{
double emi;
r = r / (12 * 100); // one month interest
t = t * 12; // one month period
emi = (p * r * (double)Math.pow(1 + r, t)) / (double)(Math.pow(1 + r, t) - 1);
return (emi);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.emi_calculator);
EditText getText_1, getText_2, getText_3;
getText_1 = (EditText) findViewById(R.id.emi_editText_1);
getText_2 = (EditText) findViewById(R.id.emi_editText_2);
getText_3 = (EditText) findViewById(R.id.emi_editText_3);
Button calculateButton = (Button) findViewById(R.id.emi_calculate);
calculateButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
double emi_edit_Text_01 = Double.parseDouble(getText_1.getText().toString());
double emi_edit_Text_02 = Double.parseDouble(getText_2.getText().toString());
double emi_edit_Text_03 = Double.parseDouble(getText_3.getText().toString());
double emi = emical(emi_edit_Text_01, emi_edit_Text_02, emi_edit_Text_03);
String str = String.valueOf(emi);
TextView result = (TextView) findViewById(R.id.result_textView_3);
result.setText(""+emi);
Intent nextPage = new Intent(EmiCalculator.this,Result.class);
startActivity(nextPage);
}
});
}
}
you must use
Intent.putExtra
for send data to another activity.
in first activity:
Intent nextPage = new Intent(EmiCalculator.this,Result.class);
nextPage.putExtra("result",""+emi);
startActivity(nextPage);
in second activity:
Intent intentResult=this.getIntent;
if(intentResult.hasExtra("result")){
textview.setText(intent.getStringExtra("result"));
}

Extracting value from database to do operations and save them in another table android

This is my view activity and in which I want to extract data from another table and save operated data to another table. The problem is the data is adding again and again, and in the view activity, the same data is repeating and filling up the whole space.
public class LastActivity extends AppCompatActivity {
TextView txt;
Button btn;
static MyDB myDB;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_last);
myDB = new MyDB(this);
getSupportActionBar().setTitle("Final Data");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
final LinearLayout lm = (LinearLayout) findViewById(R.id.lastactivity);
btn = (Button) findViewById(R.id.subfielddata);
txt = (TextView) findViewById(R.id.textView7);
Bundle bundle = getIntent().getExtras();
String date2 = bundle.getString("date1");
txt.setText(date2);
//create the layout parameters
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Cursor c = Secondlast_Activity.myDB.getAllTime();
c.moveToFirst();
if (c == null) {
Toast.makeText(getApplicationContext(), "Error!! Cursor is empty", Toast.LENGTH_SHORT).show();
} else {
//create all variants to fetch data from database
while (c.moveToNext()) {
String ename = c.getString(0);
int h1 = c.getInt(1);
int h2 = c.getInt(5);
int m1 = c.getInt(2);
int m2 = c.getInt(6);
int ad = c.getInt(7);
int wages = c.getInt(3);
int a, b, d;
int min = 0, res, hr;
//oode for getting hours from edit text
hr = h1 - h2;
//code for converting values to edit text value to minutes
if (m1 > m2) {
a = 60 - m1;
m2 = a + m2;
min = m2;
hr--;
} else {
if (m1 < m2) {
b = m2 - m1;
min = b;
}
}
hr = hr * (-1);
wages = wages * hr;
//oode for getting hours from edit text
hr = h1 - h2;
//code for converting values to edit text value to minutes
if (m1 > m2) {
a = 60 - m1;
m2 = a + m2;
min = m2;
hr--;
} else {
if (m1 < m2) {
b = m2 - m1;
min = b;
}
}
hr = hr * (-1);
wages = wages * hr;
boolean result = Secondlast_Activity.myDB.insertData(ename,hr, wages);
if (result) {
Toast.makeText(LastActivity.this, hr + " Insertion Complete", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(LastActivity.this, "Error!!", Toast.LENGTH_LONG).show();
}
}
}
}
});
//creating cursor for database to let data in count
Cursor c = LastActivity.myDB.getLastData();
c.moveToFirst();
if (c == null) {
Toast.makeText(getApplicationContext(), "Cursor d is empty!!", Toast.LENGTH_SHORT).show();
} else {
//create all variants to fetch data from database
while (c.moveToNext()) {
final String emname = c.getString(0);
final int totalwork = c.getInt(2);
final int Advance = c.getInt(3);
final int totalearn = c.getInt(4);
//create linear layout
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
//create textView to show data
TextView Name = new TextView(this);
Name.setText(emname + "");
Name.setWidth(250);
ll.addView(Name);
TextView work = new TextView(this);
work.setText(totalwork + "");
work.setWidth(290);
ll.addView(work);
TextView Ad = new TextView(this);
Ad.setText(Advance + "");
Ad.setWidth(290);
ll.addView(Ad);
TextView earn = new TextView(this);
earn.setText(totalearn + "");
earn.setWidth(250);
ll.addView(earn);
Name.setLayoutParams(params);
lm.addView(ll);
}
}
}
#Override
public boolean onSupportNavigateUp() {
onBackPressed();
return super.onSupportNavigateUp();
}
}

Redundant error for Android Studio (java) code

This is a Calculator program where several inputs are used to create several outputs. The single button on the App that I'm writing runs all the calculations then gives the outputs.
I'm getting one consistent "warning" on the lines under public void onClick
The warning is: Variable 'aResult' initializer 'Double.parseDouble(tvaResult.getText().toString())' is redundant
package com.example.ericallenbellville.rcbeamdesign;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btnCalc;
private TextView tvaResult;
private TextView tvcResult;
private TextView tvetResult;
private TextView tvphiResult;
private TextView tvMnResult;
private TextView tvphiMnResult;
private TextView tvbeta1Result;
private EditText etB,etD,etAs,etFc,etFy;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
btnCalc = (Button)findViewById(R.id.btnCalc);
etB = (EditText)findViewById(R.id.etB);
etD = (EditText)findViewById(R.id.etD);
etAs = (EditText)findViewById(R.id.etAs);
etFc = (EditText)findViewById(R.id.etFc);
etFy = (EditText)findViewById(R.id.etFy);
tvaResult = (TextView)findViewById(R.id.tvaResult);
tvcResult = (TextView)findViewById(R.id.tvcResult);
tvetResult = (TextView)findViewById(R.id.tvetResult);
tvphiResult = (TextView)findViewById(R.id.tvphiResult);
tvMnResult = (TextView)findViewById(R.id.tvMnResult);
tvphiMnResult = (TextView)findViewById(R.id.tvphiMnResult);
tvbeta1Result = (TextView)findViewById(R.id.tvbeta1Result);
btnCalc.setOnClickListener(this);
}
#Override
public void onClick(View view) {
btnCalc = (Button)findViewById(R.id.btnCalc);
Double B = Double.parseDouble(etB.getText().toString());
Double D = Double.parseDouble(etD.getText().toString());
Double As = Double.parseDouble(etAs.getText().toString());
Double Fc = Double.parseDouble(etFc.getText().toString());
Double Fy = Double.parseDouble(etFy.getText().toString());
Double aResult = Double.parseDouble(tvaResult.getText().toString());
Double cResult = Double.parseDouble(tvcResult.getText().toString());
Double etResult = Double.parseDouble(tvetResult.getText().toString());
Double beta1Result = Double.parseDouble(tvbeta1Result.getText().toString());
Double phiResult = Double.parseDouble(tvphiResult.getText().toString());
Double MnResult = Double.parseDouble(tvMnResult.getText().toString());
Double phiMnResult = Double.parseDouble(tvphiMnResult.getText().toString());
switch(view.getId() ) {
case R.id.btnCalc:
if (Fc <= 4000) {
beta1Result = (0.85);
} else if (4000 < Fc && Fc <= 8000) {
beta1Result = ((0.85)-(0.05 * ((Fc - 4000) / (1000))));
} else {
beta1Result = 0.65;
}
aResult = ((Fy * As) / (0.85 * Fc * B));
cResult = (aResult / beta1Result);
etResult = (((D - cResult) / (cResult)) * 0.003);
if (etResult >= 0.005) {
phiResult = (0.9);
} else if (0.002 <= etResult && etResult < 0.005) {
phiResult = (0.65 + (etResult - 0.002) * 0.25 / (0.005 - 0.002));
} else {
phiResult = (0.00);
}
MnResult = (((Fy * As) * (D - (aResult / 2.0))));
phiMnResult = phiResult * MnResult;
tvaResult.setText(String.valueOf(aResult));
tvcResult.setText(String.valueOf(cResult));
tvetResult.setText(String.valueOf(etResult));
tvphiResult.setText(String.valueOf(phiResult));
tvMnResult.setText(String.valueOf(MnResult));
tvphiMnResult.setText(String.valueOf(phiMnResult));
}}
}

Displaying Double in another acitivity

I am trying to display a double from this class in another class..
So here is my code:
public class Calculator extends AppCompatActivity {
Button next;
TextView pPrice;
TextView renovations;
TextView misc2;
TextView util;
TextView rep;
TextView mortage;
TextView misc1;
TextView rent;
public double getStartingCostsResult() {
return startingCostsResult;
}
double startingCostsResult;
double monthlyMinus;
double monthlyPlus;
double monthlyROI;
double yearlyROI;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
// Setting these textviews to those in the xml.
pPrice = (TextView) findViewById(R.id.pPrice);
renovations = (TextView) findViewById(R.id.renovations);
misc2 = (TextView) findViewById(R.id.misc2);
util = (TextView) findViewById(R.id.util);
rep = (TextView) findViewById(R.id.rep);
mortage = (TextView) findViewById(R.id.mortage);
misc1 = (TextView) findViewById(R.id.misc);
rent = (TextView) findViewById(R.id.rent);
next = (Button) findViewById(R.id.next);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent expense = new Intent(getApplicationContext(), Results.class);
if ((pPrice.getText().length() > 0) && (renovations.getText().length() > 0) && (misc2.getText().length() > 0)) {
double price = Double.parseDouble(pPrice.getText().toString());
// double costs = Double.parseDouble(cCosts.getText().toString());
double reno = Double.parseDouble(renovations.getText().toString());
double misc = Double.parseDouble(misc2.getText().toString());
startingCostsResult = price + reno + misc;
if((util.getText().length()>0) && (rep.getText().length()>0) && (mortage.getText().length()>0) && (misc1.getText().length()>0)){
double utilities = Double.parseDouble(util.getText().toString());
double repairs = Double.parseDouble(rep.getText().toString());
double mort = Double.parseDouble(mortage.getText().toString());
double miscsell = Double.parseDouble(misc1.getText().toString());
monthlyMinus = utilities + repairs + mort + miscsell;
if (rent.getText().length()>0){
double monthlyRent = Double.parseDouble(rent.getText().toString());
monthlyPlus = monthlyRent;
monthlyROI = monthlyPlus - monthlyMinus;
yearlyROI = monthlyROI *12;
startActivity(expense);
}else{
Toast.makeText(Calculator.this, "Please enter '0' in all boxes that don't apply.", Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(Calculator.this, "Please enter '0' in all boxes that don't apply.", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(Calculator.this, "Please enter '0' in all boxes that don't apply.", Toast.LENGTH_SHORT).show();
}
}
});
}
}
So I am trying to display the yearlyROI double in another class.
I have tried this:
Calculator calc = new Calculator();
otherClass.setText((int) calc.yearlyROI);
But my app crashes when I click next.
you should put an extra in the expense intent like this.
expense.putExtra("yearlyRoi",yearlyRoi);
then in the nexet activity you can get it like this.
Intent recievedIntent = this.getIntent();
double yearlyRoi = recievedIntent.getDoubleExtra("yearlyRoi", defaultValue);
default value can be 0.0 or anything you want.
as for the crash i think its another problem,you need to give us error log of your app.
If you want to access variables from a different Activity you need to add them to your intent.
In your case:
expense.putExtra("yearlyROI", yearlyROI);
startActivity(expense);
Then in your new Activity:
double yearlyROI = getIntent().getDoubleExtra("yearlyROI");
Hope it helps!

Clear a TextView when EditText inputs

I have 2 EditText fields set up for numeric inputs, a button to start a calculation on the 2 inputs when pressed, and a TextView to display the result of the calculation. For repeated calculations I want to clear the TextView result as soon as either EditText is changed.
Following the reply to "A better way to OnClick for EditText fields" given by 'avalancha', my program clears the result when the first EditText field is changed, but retains the previous answer if only the second EditText field is changed. Yet I have used the same source code for both fields.
Can someone explain why, and how to cure this? my code is appended:
public class DoublesActivity extends ActionBarActivity {
private EditText textBox1, textBox2;
private Button calcButton;
private Context context;
#Override
protected void onCreate(Bundle outState) {
super.onCreate(outState);
setContentView(R.layout.activity_doubles); // Sets the layout .xml file
context = this.getApplicationContext();
textBox1 = (EditText) findViewById(R.id.editText1); //textBox1 holds a reference to the editText1 object in the xml layout
textBox2 = (EditText) findViewById(R.id.editText2);
textBox1.setText("");
textBox2.setText("");
final TextView textBox3 = (TextView) findViewById(R.id.textView1);
textBox2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v2, boolean hasFocus2) {
if (hasFocus2) {
textBox3.setText("");
}
}
});
textBox1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v1, boolean hasFocus1) {
if (hasFocus1) {
textBox3.setText("");
}
}
});
calcButton = (Button) findViewById(R.id.button1);
calcButton.setBackgroundColor(Color.CYAN);
calcButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
CharSequence userNumber1 = textBox1.getText(); //userNumber1 is a CharSequence holding the text in textBox1
CharSequence userNumber2 = textBox2.getText();
Float handicap1 = Float.parseFloat(userNumber1.toString()); //convert to integer
Float handicap2 = Float.parseFloat(userNumber2.toString()); //convert to integer
Float handicapT = calculate(handicap1, handicap2);
CharSequence userNumber = String.valueOf(handicapT);
if (handicapT > 98.5) {
userNumber = "Non-valid h'cap 1!";
}
if (handicapT < -98.5) {
userNumber = "Non-valid h'cap 2!";
}
textBox3.setText(userNumber); // put result in the TextView
}
});
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
TextView textBox3 = (TextView) findViewById(R.id.textView1);
CharSequence userNumber = textBox3.getText();
outState.putCharSequence("savedText", userNumber);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
final TextView textBox3 = (TextView) findViewById(R.id.textView1);
CharSequence userText = savedInstanceState.getCharSequence("savedText");
textBox3.setText(userText);
}
Float calculate(Float h1, Float h2) {
float[] handicapArray;
handicapArray = new float[29];
handicapArray[0] = 28;
handicapArray[1] = 26;
handicapArray[2] = 24;
handicapArray[3] = 22;
handicapArray[4] = 20;
handicapArray[5] = 18;
handicapArray[6] = 16;
handicapArray[7] = 14;
handicapArray[8] = 12;
handicapArray[9] = 11;
handicapArray[10] = 10;
handicapArray[11] = 9;
handicapArray[12] = 8;
handicapArray[13] = 7;
handicapArray[14] = 6;
handicapArray[15] = 5;
handicapArray[16] = 4.5F;
handicapArray[17] = 4;
handicapArray[18] = 3.5F;
handicapArray[19] = 3;
handicapArray[20] = 2.5F;
handicapArray[21] = 2;
handicapArray[22] = 1.5F;
handicapArray[23] = 1;
handicapArray[24] = 0.5F;
handicapArray[25] = 0;
handicapArray[26] = -0.5F;
handicapArray[27] = -1;
handicapArray[28] = -1.5F;
int index1 = -1;
for (int i = 0; i < 29; i++) {
if (Math.abs(h1 - handicapArray[i]) < 0.001) {
index1 = i;
break;
}
}
if (index1 == -1) {
EditText textBox1 = (EditText) findViewById(R.id.editText1);
textBox1.setText("");
}
int index2 = -1;
for (int i = 0; i < 29; i++) {
if (Math.abs(h2 - handicapArray[i]) < 0.001) {
index2 = i;
break;
}
}
if (index2 == -1) {
EditText textBox2 = (EditText) findViewById(R.id.editText2);
textBox2.setText("");
}
int indexT = (index1 + index2) / 2; // Correctly rounds indexT halves down.
Float result = handicapArray[indexT];
if (index1 == -1) {
result = 99F;
}
;
if (index2 == -1) {
result = -99F;
}
;
return result;
}
Use addTextChangedListener to clear textview.
editText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
resultTextView.setText("");
}
});
For example please use below link
android on Text Change Listener

Categories

Resources