Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. @Override
  2. public void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. setContentView(R.layout.activity_main);
  5.  
  6. // Permission StrictMode
  7. if (android.os.Build.VERSION.SDK_INT > 9) {
  8. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  9. StrictMode.setThreadPolicy(policy);
  10. }
  11. // spinner1
  12. final Spinner spin = (Spinner) findViewById(R.id.spinner1);
  13.  
  14. String url = "http://localhost/customer/getCustomers.php";
  15. try
  16. {
  17. JSONArray data = new JSONArray(getJSONUrl(url));
  18.  
  19. final ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
  20. HashMap<String, String> map;
  21.  
  22. for (int i = 0; i < data.length(); i++) {
  23. JSONObject c = data.getJSONObject(i);
  24.  
  25. map = new HashMap<String, String>();
  26. map.put("customerID", c.getString("customerID"));
  27. map.put("name", c.getString("name"));
  28. map.put("phone", c.getString("phone"));
  29. MyArrList.add(map);
  30. }
  31. SimpleAdapter sAdap;
  32. sAdap = new SimpleAdapter(MainActivity.this, MyArrList, R.layout.activity_show,
  33. new String[]{"customerID", "name", "phone"}, new int[]{R.id.ColCustomerID, R.id.ColName, R.id.ColTel});
  34. spin.setAdapter(sAdap);
  35.  
  36. final AlertDialog.Builder viewDetail = new AlertDialog.Builder(this);
  37.  
  38. spin.setOnItemSelectedListener(new OnItemSelectedListener() {
  39.  
  40. public void onItemSelected(AdapterView<?> arg0, View selectedItemView, int position, long id) {
  41. String sCustomerID = MyArrList.get(position).get("customerID").toString();
  42. String sName = MyArrList.get(position).get("name").toString();
  43. String sTel = MyArrList.get(position).get("phone").toString();
  44.  
  45. viewDetail.setIcon(android.R.drawable.btn_star_big_on);
  46. viewDetail.setTitle("Customer Detail");
  47. viewDetail.setMessage("customerID : " + sCustomerID + "n" + "name : " + sName + "n" + "Tel : " + sTel);
  48. viewDetail.setPositiveButton("OK", new DialogInterface.OnClickListener() {
  49. public void onClick(DialogInterface dialog, int which)
  50. {
  51. dialog.dismiss();
  52. }
  53. });
  54. viewDetail.show();
  55. }
  56. public void onNothingSelected(AdapterView<?> arg0) {
  57. Toast.makeText(MainActivity.this, "Your Selected : Nothing", Toast.LENGTH_SHORT).show();
  58. }
  59. });
  60. }
  61. catch (JSONException e)
  62. {
  63. e.printStackTrace();
  64. }
  65. }
  66. public String getJSONUrl(String url) {
  67. StringBuilder str = new StringBuilder();
  68. HttpClient client = new DefaultHttpClient();
  69. HttpGet httpGet = new HttpGet(url);
  70. try {
  71. HttpResponse response = client.execute(httpGet);
  72. StatusLine statusLine = response.getStatusLine();
  73. int statusCode = statusLine.getStatusCode();
  74. if (statusCode == 200) { // Download OK
  75. HttpEntity entity = response.getEntity();
  76. InputStream content = entity.getContent();
  77. BufferedReader reader = new BufferedReader(new InputStreamReader(content));
  78. String line;
  79. while ((line = reader.readLine()) != null) {
  80. str.append(line);
  81. }
  82. } else {
  83. Log.e("Log", "Failed to download result..");
  84. }
  85. } catch (ClientProtocolException e) {
  86. e.printStackTrace();
  87. } catch (IOException e) {
  88. e.printStackTrace();
  89. }
  90.  
  91. return str.toString();
  92. }
  93. @Override
  94. public boolean onCreateOptionsMenu(Menu menu) {
  95. // getMenuInflater().inflate(R.menu.activity_main, menu);
  96. return true;
  97. }
  98.  
  99. [{"customerID":"1","name":"john","phone":"0786333210"},
  100. {"customerID":"2","name":"sam","phone":"0786223210"},
  101. {"customerID":"3","name":"ali","phone":"0786543210"}]
  102.  
  103. public String getJSONUrl(String url) {
  104. StringBuilder str = new StringBuilder();
  105. HttpClient client = new DefaultHttpClient();
  106. HttpGet httpGet = new HttpGet(url);
  107. try {
  108. HttpResponse response = client.execute(httpGet);
  109. StatusLine statusLine = response.getStatusLine();
  110. int statusCode = statusLine.getStatusCode();
  111. if (statusCode == 200) { // Download OK
  112. HttpEntity entity = response.getEntity();
  113. InputStream content = entity.getContent();
  114. BufferedReader reader = new BufferedReader (newInputStreamReader(content));
  115. String line;
  116. while ((line = reader.readLine()) != null) {
  117. str.append(line);
  118. }
  119. } else {
  120. Log.e("Log", "Failed to download result..");
  121. }
  122. } catch (ClientProtocolException e) {
  123. e.printStackTrace();
  124. } catch (IOException e) {
  125. e.printStackTrace();
  126. }
  127.  
  128. return str.toString();
  129. }
  130.  
  131. @Override
  132. public boolean onCreateOptionsMenu(Menu menu) {
  133. // getMenuInflater().inflate(R.menu.activity_main, menu);
  134. return true;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement