Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- protected class LoadAllHospitalsOnline extends AsyncTask<Void, Void, JSONObject>
- {
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- String loading = "Incarcare...";
- TextView textView = (TextView) findViewById(R.id.textView);
- ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
- ListView listview = (ListView) findViewById(list);
- progressBar.setVisibility(View.VISIBLE);
- textView.setVisibility(View.VISIBLE);
- listview.setVisibility(View.VISIBLE);
- textView.setText(loading);
- }
- @Override
- protected JSONObject doInBackground(Void... params)
- {
- URLConnection urlConn = null;
- BufferedReader bufferedReader = null;
- try
- {
- URL url = new URL(url_all_hospitals);
- urlConn = url.openConnection();
- bufferedReader = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
- StringBuffer stringBuffer = new StringBuffer();
- String line;
- while ((line = bufferedReader.readLine()) != null)
- {
- stringBuffer.append(line);
- }
- String jsonStr = stringBuffer.toString();
- if (jsonStr != null) {
- try {
- JSONObject jsonObj = new JSONObject(jsonStr);
- // Getting JSON Array node
- JSONArray hospitals = jsonObj.getJSONArray("spitale");
- // looping through All Contacts
- for (int i = 0; i < hospitals.length(); i++) {
- JSONObject c = hospitals.getJSONObject(i);
- String address = c.getString("adresa");
- String name = c.getString("nume");
- String phone = c.getString("numar");
- // tmp hash map for single contact
- HashMap<String, String> hospitalz = new HashMap<>();
- // adding each child node to HashMap key => value
- hospitalz.put("nume", name);
- hospitalz.put("adresa", address);
- hospitalz.put("telefon", phone);
- // adding contact to contact list
- hospitalList.add(hospitalz);
- }
- } catch (final JSONException e) {
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- Toast.makeText(getApplicationContext(),
- R.string.nodata,
- Toast.LENGTH_LONG)
- .show();
- }
- });
- }
- } else {
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- Toast.makeText(getApplicationContext(),
- R.string.nodata,
- Toast.LENGTH_LONG)
- .show();
- }
- });
- }
- return new JSONObject(stringBuffer.toString());
- }
- catch(Exception ex)
- {
- return null;
- }
- finally
- {
- if(bufferedReader != null)
- {
- try {
- bufferedReader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- runOnUiThread(new Runnable() {
- public void run() {
- /**
- * Updating parsed JSON data into ListView
- * */
- final ListAdapter adapter = new SimpleAdapter(
- hospitalActivity.this, hospitalList,
- R.layout.list_item, new String[]{"nume", "adresa",
- "telefon"}, new int[]{R.id.nume,
- R.id.adresa, R.id.numar});
- TextView textView = (TextView) findViewById(R.id.textView);
- ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
- ListView listview = (ListView) findViewById(list);
- listview.setAdapter(adapter);
- listview.setVisibility(View.VISIBLE);
- progressBar.setVisibility(View.INVISIBLE);
- textView.setVisibility(View.INVISIBLE);
- LinearLayout mainLayout=(LinearLayout) findViewById(R.id.fkd);
- mainLayout.setVisibility(View.VISIBLE);
- EditText inputSearch = (EditText) findViewById(R.id.textToSearch);
- inputSearch.addTextChangedListener(new TextWatcher() {
- @Override
- public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
- // When user changed the Text
- hospitalActivity.LoadAllHospitalsOnline.this.adapter.getFilter().filter(cs);
- }
- @Override
- public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
- // TODO Auto-generated method stub
- }
- @Override
- public void afterTextChanged(Editable arg0) {
- // TODO Auto-generated method stub
- }
- });
- }
- });
- }
- }
- }
- @Override
- protected void onPostExecute(JSONObject response)
- {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement