George
George

Reputation: 5691

android -- make calculations with input data (numeric) -- (show graphs?)

I am starting to learn android and i have a few questions. I have my main program as:

public class Radiation_overflowActivity  extends Activity implements OnClickListener {

    EditText init_cores;
        View final_cores;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    //Set up click listeners
    init_cores=(EditText) findViewById(R.id.init_cores);
    //init_cores.setOnClickListener(this);
    final_cores=(View) findViewById(R.id.final_cores);
    final_cores.setOnClickListener(this);

     }

    //called when a button is clicked
    public void onClick(View v) {

        switch (v.getId()){

        case R.id.final_cores:
            //int r = Integer.parseInt(init_cores.getText().toString().trim());
            double initcores=Double.parseDouble(init_cores.getText().toString().trim());
            double l=2,t=2;
            double fcores=initcores*Math.exp(-l*t);
            Intent i=new Intent(this,calcs.class);
            i.putExtra("value",fcores);
            startActivity(i);
            break;
         }

       }
      }

Also, my main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/initial_cores" />
<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/init_cores"
    android:inputType="numberDecimal" />
<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/final_cores"
    android:text="@string/calculate" />
</LinearLayout>

My calcs.java:

public class calcs extends Activity{

    TextView calcs_final;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.calcs);

        calcs_final=(TextView) findViewById(R.id.calcs_final);

        double f=getIntent().getExtras().getDouble("value");

        calcs_final.setText(Double.toString(f));


}

}

My calcs.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/calcs_final" 
        android:text="@string/result" />


</LinearLayout>

I want to do this:

The user enters data into the edittext (init_cores) and then i want to make some calculations to compute the final_cores and show it to the user.

I have trouble for showing the results.I am starting another activity (calcs.class and calcs.xml). Where i must do the calculations?To the calcs.class? Right now,the user enters a number ,presses the "Calculate" button and then it shows the text (from the strings.xml) "Number of cores"" ,but not the result.

Also, can i use the input from the data and from my calculations and make a plot?If you could give me some directions to this.

Finally,is my approach right?

Thank you!

Upvotes: 2

Views: 3294

Answers (2)

user370305
user370305

Reputation: 109257

As per I think your final_cores is a view on which, you want to get your edittext's value for calculation(Hope I am not wrong)

So, just do Something like,

public void OnClick(View v) {

    switch (v.getId()){

    case R.id.final_cores:
        int r = Integer.parseInt(init_cores.getText().toString().trim());
        Intent i=new Intent(this,calcs.class);
        i.putExtra("value",r);
        startActivity(i);
        break;
    }

}

Remove onClickListener from your init_cores edittext. And get values of r in your Calcs.class using

int r = getIntent().getExtras().getInt("value");

Also be sure in your edit text always you are entering numeric values so it can be parsable into Integer..

Try this and let me know what happen..

Thanks,

Upvotes: 0

Ramseys
Ramseys

Reputation: 431

For plotting your data, you can use the AChartEngine library, which is quite extensive and easy to use.

Upvotes: 1

Related Questions