H4T3D

Android class

Sep 25th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. package com.example.priem_ali.priem_patient;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.view.Menu;
  7. import android.view.MenuItem;
  8. import android.widget.ArrayAdapter;
  9. import android.widget.ShareActionProvider;
  10. import android.widget.Spinner;
  11. import android.widget.TextView;
  12. import android.widget.Toast;
  13.  
  14. import org.json.JSONArray;
  15. import org.json.JSONException;
  16. import org.json.JSONObject;
  17.  
  18. /**
  19. * Created by Priem-Ali on 9/22/2016.
  20. */
  21. public class spinnerActivity extends Activity {
  22.  
  23.  
  24. private Spinner spinner;
  25. private TextView textView12;
  26.  
  27. protected void onCreate(Bundle savedInstanceState) {
  28. super.onCreate(savedInstanceState);
  29. setContentView(R.layout.spineractivity);
  30.  
  31.  
  32. spinner = (Spinner)findViewById( R.id.spinner );
  33. textView12 = (TextView)findViewById( R.id.textView12 );
  34.  
  35. String strJson="{\"products\":[{\"item_id\":\"103\",\"name\":\"Crispy Broast\",\"category\":\"Broast\",\"unit_price\":\"180.00\",\"cost_price\":\"180.00\",\"quantity\":\"100\",\"tax\":\"10.00\"}]}";
  36. String data = "";
  37. try {
  38. JSONObject jsonRootObject = new JSONObject(strJson);
  39.  
  40. //Get the instance of JSONArray that contains JSONObjects
  41. JSONArray jsonArray = jsonRootObject.optJSONArray("products");
  42.  
  43.  
  44. // Creating adapter for spinner
  45.  
  46.  
  47. //Iterate the jsonArray and print the info of JSONObjects
  48. for(int i=0; i < jsonArray.length(); i++){
  49. JSONObject jsonObject = jsonArray.getJSONObject(i);
  50.  
  51.  
  52. String name = jsonObject.optString("name").toString();
  53. String category = jsonObject.optString("category").toString();
  54. String unitp = jsonObject.optString("unit_price").toString();
  55. String quan = jsonObject.optString("quantity").toString();
  56.  
  57.  
  58. //data += " \n Name= "+ name +" \n , \n" + " category= \"+ category +\" \n , \n" + "\" + \" unitprice= \"+ unitp +\" \n + \"\" + \" quanantity= \"+ quan +\" \n "
  59. ;
  60.  
  61. data += name + "\n"+ category + "\n" + unitp + "\n"+quan;
  62.  
  63. }
  64.  
  65. try {
  66. String arra [] = data.split("\n");
  67.  
  68. ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.expandable_list_content, arra);
  69.  
  70. // Drop down layout style - list view with radio button
  71. dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  72.  
  73. // attaching data adapter to spinner
  74. spinner.setAdapter(dataAdapter);
  75. } catch (Exception e) {
  76. e.printStackTrace();
  77. Toast.makeText(this , e.toString() , Toast.LENGTH_LONG).show();
  78. }
  79.  
  80. } catch (JSONException e) {
  81. Toast.makeText(this , e.toString() , Toast.LENGTH_LONG).show();
  82. textView12.setText(e.toString()+data+"Google");
  83. e.printStackTrace();}
  84.  
  85. textView12.setText(data+"\n");
  86. }
  87.  
  88. private ShareActionProvider mShareActionProvider;
  89.  
  90. public boolean onCreateOptionsMenu(Menu menu) {
  91. // Inflate menu resource file.
  92. getMenuInflater().inflate(R.menu.menu, menu);
  93.  
  94. // Locate MenuItem with ShareActionProvider
  95. MenuItem item = menu.findItem(R.id.menu_item_share);
  96.  
  97. // Fetch and store ShareActionProvider
  98. mShareActionProvider = (ShareActionProvider) item.getActionProvider();
  99.  
  100. // Return true to display menu
  101. return true;
  102. }
  103.  
  104. // Call to update the share intent
  105. private void setShareIntent(Intent shareIntent) {
  106. if (mShareActionProvider != null) {
  107. mShareActionProvider.setShareIntent(shareIntent);
  108. }
  109. }
  110.  
  111. }
Add Comment
Please, Sign In to add comment