Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. public class currency extends Activity{
  2.  
  3. int item1,
  4. item2;
  5. private String array_spinner[];
  6. private String array_spinner2[];
  7. public Double rates[],
  8. obj4;
  9. public String API_URL2,
  10. rates_new[],
  11. s_query;
  12. public SQLiteDatabase db;
  13.  
  14. @ Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.currency1);
  18.  
  19. array_spinner = new String[38];
  20. rates = new Double[38];
  21. rates_new = new String[38];
  22. array_spinner[0] = " USD US Dollar";
  23.  
  24. rates[0] = 1.0;
  25.  
  26. db = openOrCreateDatabase("MyDB", MODE_PRIVATE, null);
  27. db.execSQL("DROP TABLE IF EXISTS FunnyNames");
  28. db.execSQL("CREATE TABLE IF NOT EXISTS FunnyNames (id INTEGER, Email DOUBLE, FirstName VARCHAR, LastName VARCHAR);");
  29. db.execSQL("INSERT INTO FunnyNames VALUES('0','" + rates[0] + "', 'Anita', 'Bath');");
  30.  
  31. for (int i = 1; i <= 37; i++) {
  32. final int j = i;
  33. API_URL2 = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22USD"
  34. + array_spinner2[i]
  35. + "%22)&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=";
  36.  
  37. AsyncHttpClient client = new AsyncHttpClient();
  38. client.get(API_URL2, new AsyncHttpResponseHandler() {
  39.  
  40. @ Override
  41. public void onFailure(Throwable arg0, String arg1) {
  42. super.onFailure(arg0, arg1);
  43. }
  44.  
  45. @ Override
  46. public void onFinish() {
  47. super.onFinish();
  48. }
  49.  
  50. @ Override
  51. public void onStart() {
  52. super.onStart();
  53. }
  54.  
  55. @ Override
  56. public void onSuccess(String response) {
  57. try {
  58. JSONObject jsonObj = new JSONObject(response);
  59. JSONObject obj1 = jsonObj.getJSONObject("query");
  60. JSONObject obj2 = obj1.getJSONObject("results");
  61. JSONObject obj3 = obj2.getJSONObject("rate");
  62. obj4 = obj3.getDouble("Rate");
  63. rates[j] = obj4;
  64. } catch (JSONException e) {
  65. e.printStackTrace();
  66. }
  67. }
  68.  
  69. });
  70. }
  71. for (int k = 0; k <= 37; k++) {
  72. s_query = "INSERT INTO FunnyNames VALUES('" + k + "','" + rates[k] + "', 'Anita', 'Bath');";
  73. db.execSQL(s_query);
  74. String select_query = "SELECT * from FunnyNames WHERE id=" + k;
  75. Cursor c = db.rawQuery(select_query, null);
  76. c.moveToFirst();
  77. rates_new[k] = c.getString(1);
  78. }
  79. final Spinner s1 = (Spinner)findViewById(R.id.spinner23);
  80. ArrayAdapter adapter1 = new ArrayAdapter(this,
  81. android.R.layout.simple_spinner_item, array_spinner);
  82. s1.setAdapter(adapter1);
  83. adapter1.setDropDownViewResource(R.layout.spinner_layout);
  84.  
  85. s1.setOnItemSelectedListener(new OnItemSelectedListener() {
  86. public void onItemSelected(AdapterView < ? > argo0, View arg1,
  87. int arg2, long arg3) {
  88. int item = s1.getSelectedItemPosition();
  89. item1 = item;
  90.  
  91. }
  92.  
  93. public void onNothingSelected(AdapterView < ? > arg0) {}
  94. });
  95.  
  96. final Spinner s2 = (Spinner)findViewById(R.id.spinner24);
  97. ArrayAdapter adapter2 = new ArrayAdapter(this,
  98. android.R.layout.simple_spinner_item, array_spinner);
  99. s2.setAdapter(adapter2);
  100. adapter2.setDropDownViewResource(R.layout.spinner_layout);
  101. s2.setOnItemSelectedListener(new OnItemSelectedListener() {
  102. public void onItemSelected(AdapterView < ? > argo0, View arg1,
  103. int arg2, long arg3) {
  104. int item = s2.getSelectedItemPosition();
  105. item2 = item;
  106.  
  107. }
  108. public void onNothingSelected(AdapterView < ? > arg0) {}
  109.  
  110. });
  111. Button currnecy_convert = (Button)findViewById(R.id.Button12);
  112. currnecy_convert.setOnClickListener(new OnClickListener() {
  113.  
  114. @ Override
  115. public void onClick(View arg0) {
  116. final EditText input_currency = (EditText)findViewById(R.id.editText11);
  117. final TextView converted_currency = (TextView)findViewById(R.id.textView61);
  118.  
  119. if (!input_currency.getText().toString().equals("")) {
  120.  
  121. converted_currency.setText(String.format(" " + rates_new[6] + " " + rates_new[20] + " " + rates_new[31]));
  122. } else {
  123. Toast.makeText(getApplicationContext(), "Please enter a USD value!", Toast.LENGTH_LONG).show();
  124. }
  125. }
  126. });
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement