new text document

Download New Text Document

If you can't read please download the document

Upload: osman-residovic

Post on 24-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

private static String url = "http://hoteli.azurewebsites.net/api/Hotels/"; private static final String TAG_HOTELI = "Hotels"; private static final String TAG_NAME = "Name"; private static final String TAG_RATING = "Rating"; private static final String TAG_CIJENA = "Price"; private static final String TAG_STATUS = "Status"; private static final String TAG_LATITUDE = "Latitude"; private static final String TAG_LONGITUDE = "Longitude"; private static final String TAG_SLIKA = "ImagePath"; private static final String TAG_OPIS = "Description"; private ProgressDialog pDialog; private ListView listView; JSONArray hoteli = null; ArrayList listaHotela = new ArrayList();private class getHotel extends AsyncTask { @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(Izmijeni_hotel.this); pDialog.setMessage("Molimo saekajte!"); pDialog.setCancelable(false); pDialog.show(); } @Override protected Void doInBackground(Void... arg0) { ServiceHandler sh = new ServiceHandler(); String jsonStr = null; try { jsonStr = sh.makeServiceCall(url, ServiceHandler.GET); } catch (HttpException e) { e.printStackTrace(); } catch (URISyntaxException e) { e.printStackTrace(); } Log.d("Response: ", "> " + jsonStr); if (jsonStr != null) { try { JSONObject jsonObj = new JSONObject(jsonStr); hoteli = jsonObj.getJSONArray(TAG_HOTELI); for (int i = 0; i < hoteli.length(); i++) { JSONObject c = hoteli.getJSONObject(i); String id = c.getString("Id"); String name = c.getString(TAG_NAME); String rating = c.getString(TAG_RATING); String cijena = c.getString(TAG_CIJENA); String status = c.getString(TAG_STATUS); String latitude = c.getString(TAG_LATITUDE); String longitude = c.getString(TAG_LONGITUDE); String slika = c.getString(TAG_SLIKA); String opis = c.getString(TAG_OPIS); HashMap hotel = new HashMap(); hotel.put("Id", id); hotel.put(TAG_NAME, name); hotel.put(TAG_RATING, rating); hotel.put(TAG_CIJENA, cijena); hotel.put(TAG_STATUS, status); hotel.put(TAG_LATITUDE, latitude); hotel.put(TAG_LONGITUDE, longitude); hotel.put(TAG_SLIKA, slika); hotel.put(TAG_OPIS, opis); listaHotela.add(hotel); } } catch (JSONException e) { e.printStackTrace(); } } else { Log.e("ServiceHandler", "Ne mogu dohvatiti podatke!"); } return null; } @Override protected void onPostExecute(Void result) { pDialog.dismiss(); final ListAdapter adapter = new SimpleAdapter( Izmijeni_hotel.this, listaHotela, R.layout.item_hotel, new String[]{TAG_NAME, "Id"}, new int[]{R.id.name, R.id.id}); listView.setAdapter(adapter); final Intent intent = new Intent(getApplicationContext(), NoviHotel.class); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { Set set = listaHotela.get(position).entrySet(); Iterator i = set.iterator(); while (i.hasNext()) { Map.Entry me = (Map.Entry) i.next(); intent.putExtra(me.getKey().toString(), me.getValue().toString()); } startActivity(intent); } }); listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView parent, View view, final int position, long id) { AlertDialog.Builder adb = new AlertDialog.Builder(Izmijeni_hotel.this); adb.setMessage("Da li ste sigurni da elite obrisati hotel?"); adb.setCancelable(false); adb.setPositiveButton("Da", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { IzbrisiHotel izbrisiHotel = new IzbrisiHotel(); Set set = listaHotela.get(position).entrySet(); Iterator i = set.iterator(); while (i.hasNext()) { Map.Entry me = (Map.Entry) i.next(); if (me.getKey().equals("Id")) izbrisiHotel.execute(me.getValue().toString()); } startActivity(new Intent(Izmijeni_hotel.this, Izmijeni_hotel.class)); } }); adb.setNegativeButton("Ne", null); adb.show(); return true; } }); } }