Advertisement
Guest User

Cod Modificat

a guest
Jan 18th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.11 KB | None | 0 0
  1. protected class LoadAllHospitalsOnline extends AsyncTask<Void, Void, JSONObject>
  2. {
  3. @Override
  4. protected void onPreExecute() {
  5. super.onPreExecute();
  6. String loading = "Incarcare...";
  7. TextView textView = (TextView) findViewById(R.id.textView);
  8. ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
  9. ListView listview = (ListView) findViewById(list);
  10. progressBar.setVisibility(View.VISIBLE);
  11. textView.setVisibility(View.VISIBLE);
  12. listview.setVisibility(View.VISIBLE);
  13. textView.setText(loading);
  14. }
  15.  
  16. @Override
  17. protected JSONObject doInBackground(Void... params)
  18. {
  19.  
  20. URLConnection urlConn = null;
  21. BufferedReader bufferedReader = null;
  22. try
  23. {
  24. URL url = new URL(url_all_hospitals);
  25. urlConn = url.openConnection();
  26. bufferedReader = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
  27.  
  28. StringBuffer stringBuffer = new StringBuffer();
  29. String line;
  30. while ((line = bufferedReader.readLine()) != null)
  31. {
  32. stringBuffer.append(line);
  33. }
  34. String jsonStr = stringBuffer.toString();
  35. if (jsonStr != null) {
  36. try {
  37. JSONObject jsonObj = new JSONObject(jsonStr);
  38.  
  39. // Getting JSON Array node
  40. JSONArray hospitals = jsonObj.getJSONArray("spitale");
  41.  
  42. // looping through All Contacts
  43. for (int i = 0; i < hospitals.length(); i++) {
  44. JSONObject c = hospitals.getJSONObject(i);
  45.  
  46. String address = c.getString("adresa");
  47. String name = c.getString("nume");
  48. String phone = c.getString("numar");
  49.  
  50. // tmp hash map for single contact
  51. HashMap<String, String> hospitalz = new HashMap<>();
  52.  
  53. // adding each child node to HashMap key => value
  54. hospitalz.put("nume", name);
  55. hospitalz.put("adresa", address);
  56. hospitalz.put("telefon", phone);
  57.  
  58. // adding contact to contact list
  59. hospitalList.add(hospitalz);
  60. }
  61. } catch (final JSONException e) {
  62. runOnUiThread(new Runnable() {
  63. @Override
  64. public void run() {
  65. Toast.makeText(getApplicationContext(),
  66. R.string.nodata,
  67. Toast.LENGTH_LONG)
  68. .show();
  69. }
  70. });
  71.  
  72. }
  73. } else {
  74. runOnUiThread(new Runnable() {
  75. @Override
  76. public void run() {
  77. Toast.makeText(getApplicationContext(),
  78. R.string.nodata,
  79. Toast.LENGTH_LONG)
  80. .show();
  81. }
  82. });
  83.  
  84. }
  85. return new JSONObject(stringBuffer.toString());
  86. }
  87. catch(Exception ex)
  88. {
  89. return null;
  90. }
  91. finally
  92. {
  93. if(bufferedReader != null)
  94. {
  95. try {
  96. bufferedReader.close();
  97. } catch (IOException e) {
  98. e.printStackTrace();
  99. }
  100. runOnUiThread(new Runnable() {
  101. public void run() {
  102. /**
  103. * Updating parsed JSON data into ListView
  104. * */
  105. final ListAdapter adapter = new SimpleAdapter(
  106. hospitalActivity.this, hospitalList,
  107. R.layout.list_item, new String[]{"nume", "adresa",
  108. "telefon"}, new int[]{R.id.nume,
  109. R.id.adresa, R.id.numar});
  110.  
  111. TextView textView = (TextView) findViewById(R.id.textView);
  112. ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
  113. ListView listview = (ListView) findViewById(list);
  114. listview.setAdapter(adapter);
  115. listview.setVisibility(View.VISIBLE);
  116. progressBar.setVisibility(View.INVISIBLE);
  117. textView.setVisibility(View.INVISIBLE);
  118. LinearLayout mainLayout=(LinearLayout) findViewById(R.id.fkd);
  119. mainLayout.setVisibility(View.VISIBLE);
  120. EditText inputSearch = (EditText) findViewById(R.id.textToSearch);
  121. inputSearch.addTextChangedListener(new TextWatcher() {
  122.  
  123. @Override
  124. public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
  125. // When user changed the Text
  126. hospitalActivity.LoadAllHospitalsOnline.this.adapter.getFilter().filter(cs);
  127. }
  128.  
  129. @Override
  130. public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
  131. // TODO Auto-generated method stub
  132.  
  133. }
  134. @Override
  135. public void afterTextChanged(Editable arg0) {
  136. // TODO Auto-generated method stub
  137. }
  138. });
  139. }
  140. });
  141. }
  142. }
  143. }
  144.  
  145. @Override
  146. protected void onPostExecute(JSONObject response)
  147. {
  148.  
  149. }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement