Note that there are some explanatory texts on larger screens.

plurals
  1. POcurrency converter android code doesn't work
    primarykey
    data
    text
    <p>I am writing and android application, my code is a currency converter, when I enter the amount I want to convert nothing will change- the converted amount is ZERO-</p> <p>I don't know what is wrong in my code:</p> <p>here is the code I am writing:</p> <pre><code>public class Currency_convert extends Activity { /** Called when the activity is first created. */ float inputValue; Handler h; static int decimalsize = 2; public DecimalFormat df = new DecimalFormat(); String US = "USD"; String EURO = "EUR"; String INR = "INR"; String AUD = "AUD"; String GBP = "GBP"; public double dUS;// = 0.0; public double dEURO = 1.0; public double dINR;// = 0.0; public double dAUD;// = 0.0; public double dGBP;// = 0.0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.currency); boolean rateupdate = false; AbsoluteLayout al = (AbsoluteLayout) findViewById(R.id.AbsoluteLayout01); al.setBackgroundColor(Color.LTGRAY); final EditText amount = (EditText) findViewById(R.id.txt1); final EditText etResult = (EditText) findViewById(R.id.txt2); final Spinner sp1 = (Spinner) findViewById(R.id.spinerr1); final Spinner sp2 = (Spinner) findViewById(R.id.spinerr2); final InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.currency, android.R.layout.simple_spinner_item); final Button btn = (Button) findViewById(R.id.bb1); amount.setText("0.0"); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // adapter.setDropDownViewResource(android.R.layout.) sp1.setAdapter(adapter); sp2.setAdapter(adapter); rateupdate = getRate(); if(rateupdate) btn.setText("Currency Rate updated"); else btn.setText("Updated Currency Rate "); //btn.setText(Boolean.toString(rateupdate)); OnClickListener ltouch = new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub //getRate(); } };;; btn.setOnClickListener(ltouch ); OnItemClickListener itemClick = new OnItemClickListener() { @Override public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position,long id) { int curr1 = (int) sp1.getSelectedItemId(); int curr2 = (int) sp2.getSelectedItemId(); double result = Convert(curr1, curr2, amount.getText().toString()); etResult.setText(Double.toString(result)); } }; OnItemSelectedListener ItemSelectListener = new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView&lt;?&gt; parent, View view,int position, long id) { try { int curr1 = (int) sp1.getSelectedItemId(); int curr2 = (int) sp2.getSelectedItemId(); double result = Convert(curr1, curr2, amount.getText().toString()); etResult.setText(Double.toString(result)); } catch(Exception e) { } } @Override public void onNothingSelected(AdapterView&lt;?&gt; arg0) { // TODO Auto-generated method stub } }; sp1.setOnItemSelectedListener(ItemSelectListener); sp2.setOnItemSelectedListener(ItemSelectListener); amount.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub int length = s.length(); if(length == 0) //amount.setText("0.0"); s = "0.0"; int curr1 = (int) sp1.getSelectedItemId(); int curr2 = (int) sp2.getSelectedItemId(); //double result = Convert(curr1, curr2, amount.getText().toString()); double result = Convert(curr1, curr2, s.toString()); etResult.setText(Double.toString(result)); } @Override public void beforeTextChanged(CharSequence s, int start, int count,int after) { // TODO Auto-generated method stub } @Override public void afterTextChanged(Editable s) { // TODO Auto-generated method stub } }); OnTouchListener l = new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub imm.hideSoftInputFromWindow(amount.getWindowToken(),0);// return true; } }; al.setOnTouchListener(l); sp1.setSelection(4); sp2.setSelection(0); } protected Double Convert(int currency_from_index, int currency_to_index, String s_amount) { // TODO Auto-generated method stub Double result = 0.0; Double amount = Double.parseDouble(s_amount); Double source = 0.0; Double destination = 0.0; if((dUS * dAUD * dINR * dGBP * dEURO) == 0) { result = 0.0; return result; } else { switch (currency_from_index) { case 0: source = dUS; break; case 1: source = dAUD; break; case 2: source = dINR; break; case 3: source = dGBP; break; case 4: source = dEURO; break; default: break; } switch (currency_to_index) { case 0: destination = dUS; break; case 1: destination = dAUD; break; case 2: destination = dINR; break; case 3: destination = dGBP; break; case 4: destination = dEURO; break; default: break; } result = amount*destination/source; Math.round(result); try { df.setMaximumFractionDigits(decimalsize); df.setMinimumFractionDigits(decimalsize); String str_result = df.format(result); result = Double.parseDouble(str_result); } catch(Exception e) { } return result; } } public boolean getRate() { //ConversionRate cr = new ConversionRate(); try { //URL url = new URL(eText.getText().toString()); String path = "http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml"; URL url = new URL(path); URLConnection conn = url.openConnection(); // Get the response BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); factory.setNamespaceAware(true); XmlPullParser xpp = factory.newPullParser(); //xpp.setInput( new StringReader ( "&lt;foo&gt;Hello World!&lt;/foo&gt;" ) ); xpp.setInput(rd); int eventType = xpp.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { if(eventType == XmlPullParser.START_DOCUMENT) { //System.out.println("Start document"); //tView.append("Start document"); } else if(eventType == XmlPullParser.END_DOCUMENT) { //System.out.println("End document"); //tView.append("End document"); } else if(eventType == XmlPullParser.START_TAG) { //System.out.println("Start tag "+xpp.getName()); //tView.append("Start tag "+xpp.getName()); //tView.append("Attribute Count = " + xpp.getAttributeCount()); if(xpp.getAttributeCount() &gt; 1) { /*tView.append("Currency = " + xpp.getAttributeValue(0) + "\t"); tView.append("Rate = " + xpp.getAttributeValue(1)); tView.append("\n");*/ //cr.setName(xpp.getAttributeValue(0)); //cr.setRate(Double.parseDouble(xpp.getAttributeValue(1))); if(xpp.getAttributeValue(0).equals(US))// toString()== US) Currency_convert.this.dUS = Double.parseDouble(xpp.getAttributeValue(1)); else if(xpp.getAttributeValue(0).equals(INR))// .toString() == "INR") dINR = Double.parseDouble(xpp.getAttributeValue(1)); else if(xpp.getAttributeValue(0).equals(GBP))// .toString() == "GBP") dGBP = Double.parseDouble(xpp.getAttributeValue(1)); else if(xpp.getAttributeValue(0).equals(AUD))// .toString() == "AUD") dAUD = Double.parseDouble(xpp.getAttributeValue(1)); } } else if(eventType == XmlPullParser.END_TAG) { //System.out.println("End tag "+xpp.getName()); //tView.append("End tag "+xpp.getName()); } else if(eventType == XmlPullParser.TEXT) { //System.out.println("Text "+xpp.getText()); //tView.append("Text "+xpp.getText()); } eventType = xpp.next(); } return true; } catch(Exception e) { e.printStackTrace(); return false; } } } </code></pre> <p>here is xml file:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;AbsoluteLayout android:id="@+id/AbsoluteLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;EditText android:id="@+id/txt1" android:text="@+id/EditText01" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_y="10dip" android:numeric="decimal" android:singleLine="true" android:ems="8" android:hint="Enter amount" android:layout_x="5dip"&gt;&lt;/EditText&gt; &lt;EditText android:id="@+id/txt2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@+id/EditText02" android:editable="false" android:enabled="true" android:singleLine="true" android:layout_x="5dip" android:ems="8" android:layout_y="69dip"&gt;&lt;/EditText&gt; &lt;Spinner android:id="@+id/spinerr1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_y="10dip" android:layout_x="180dip"&gt;&lt;/Spinner&gt; &lt;Spinner android:layout_y="69dip" android:id="@+id/spinerr2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_x="180dip"&gt;&lt;/Spinner&gt; &lt;Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/bb1" android:layout_x="50dip" android:text="Update Currency Rates" android:layout_y="120dip"&gt;&lt;/Button&gt; &lt;/AbsoluteLayout&gt; </code></pre> <p>I don't get any errors in LogCat !!!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload