2

The error is solved and the app runs. The problem is how do i add a listener to the amound field in the app aswell? As the app is now it just sums up for ex "sek 1" + "usd 6.69" = "7.69". Any good ideas how to make this work?

And here is my code:

package com.example.currencyconverter;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;


public class MainActivity extends Activity {
 private String [] currency_name;
 private ArrayAdapter<String> adapter;
 private Spinner spin1, spin2;

 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 setUpTheSpinners();
 }
 @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        return true;
    }


 private void setUpTheSpinners() {
 currency_name = getResources().getStringArray(R.array.currency_name);
 adapter= new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, currency_name);
 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
 OnItemSelectedListener listener = new CurrencySelectedListener();
 spin1 = (Spinner)findViewById(R.id.spinner1);
 spin1.setAdapter(adapter);
 spin1.setOnItemSelectedListener(listener);
 spin2 = (Spinner)findViewById(R.id.spinner2);
 spin2.setAdapter(adapter);
 spin2.setOnItemSelectedListener(listener);
 }

 private void calculateSum() {
 String [] rates = getResources().getStringArray(R.array.currency_rate);
 int index1 = spin1.getSelectedItemPosition();
 int index2 = spin2.getSelectedItemPosition();

 double rate1 = Double.parseDouble( rates[index1] );
 double rate2 = Double.parseDouble( rates[index2] );

 double totalRate = rate1 + rate2;
 TextView totalRateText = (TextView)findViewById(R.id.textView4);
 totalRateText.setText("" + totalRate);
 }

 private class CurrencySelectedListener implements OnItemSelectedListener {

 public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
 calculateSum();
 }

 public void onNothingSelected(AdapterView<?> parent) {
 }
 }
}
  • Please format your code in a readable manner. What line gives this error? – m0skit0 Oct 20 '14 at 11:27
  • Im sorry, i have marked the the lines in the code as B. Its the "CurrencySelectedListener" and "View" that gives me error. Thanks! – Oliver Okane Oct 20 '14 at 11:30
  • Your code compiles fine for me. Note that the error refers to `MainActivity.currencySelectedListener` while your class is actually `MainActivity.CurrencySelectedListener`. – m0skit0 Oct 20 '14 at 11:39
  • Btw, use `String.valueOf(totalRate)` instead of `"" + totalRate`. Later is slower and consumes more memory. – m0skit0 Oct 20 '14 at 11:40
  • 1
    Compiles fine for me. CurrencySelectedListener does implement both the functions required and they are present since API 1. Hopefully the *** in your code is for illustration purpose only. – Rohan Oct 20 '14 at 11:46
  • 1
    use annotation @Override before onItemSelected defination – Angad Tiwari Oct 20 '14 at 11:47
  • @AngadTiwari `@Override` is optional. I didn't include it and it compiled fine. – m0skit0 Oct 20 '14 at 11:48
  • but @m0skit0 as you see....the syntex is write ...somehow...system doesn't detect write ... why not try to implements OnItemSelectedListener on Activity and override its methods on it..and check – Angad Tiwari Oct 20 '14 at 11:52
  • @AngadTiwari Sorry, I didn't understand what you mean with *write*... Implementing on Activity or any other class is the same. As I said it compiles fine for me. – m0skit0 Oct 20 '14 at 11:53
  • yes you are right...but i think this must be try in different way as its not the big problem... – Angad Tiwari Oct 20 '14 at 12:00
  • Can anyone see why this just crash when i try to run it on the emulator? – Oliver Okane Oct 20 '14 at 12:21
  • @OliverOkane attach the logcat..how can we know about exception? – Angad Tiwari Oct 20 '14 at 12:40
  • I have added the logcat and the error with the app carshing is solved. The problem is now that it doessnt return any value. Nothing happens. Im so sorry for stupid questions or missing information! – Oliver Okane Oct 20 '14 at 12:52

0 Answers0