Advertisement
akajaymo

Untitled

Nov 14th, 2011
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. package com.pxr.tutorial.json;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.IOException;
  5. import java.net.MalformedURLException;
  6. import java.net.URL;
  7. import java.net.URLConnection;
  8. import java.util.ArrayList;
  9. import java.util.HashMap;
  10.  
  11. import org.json.JSONArray;
  12. import org.json.JSONException;
  13. import org.json.JSONObject;
  14.  
  15. import android.app.ListActivity;
  16. import android.content.Intent;
  17. import android.graphics.Bitmap;
  18. import android.graphics.BitmapFactory;
  19. import android.net.Uri;
  20. import android.os.Bundle;
  21. import android.util.Log;
  22. import android.view.View;
  23. import android.view.Window;
  24. import android.widget.AdapterView;
  25. import android.widget.AdapterView.OnItemClickListener;
  26. import android.widget.ImageView;
  27. import android.widget.ListAdapter;
  28. import android.widget.ListView;
  29. import android.widget.SimpleAdapter;
  30. import com.pxr.tutorial.xmltest.R;
  31.  
  32. public class Main extends ListActivity {
  33. /** Called when the activity is first created. */
  34. ImageView n;
  35. Bitmap bm;
  36. BufferedInputStream is;
  37. @Override
  38. public void onCreate(Bundle savedInstanceState) {
  39. super.onCreate(savedInstanceState);
  40. requestWindowFeature(Window.FEATURE_NO_TITLE);
  41. setContentView(R.layout.listplaceholder);
  42.  
  43. ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
  44.  
  45.  
  46. JSONObject json = JSONfunctions.getJSONfromURL("http://10.0.2.2/android/rental.php");
  47.  
  48. try{
  49.  
  50. JSONArray people = json.getJSONArray("rental");
  51.  
  52. for(int i=0;i<people.length();i++){
  53. HashMap<String, String> map = new HashMap<String, String>();
  54. JSONObject e = people.getJSONObject(i);
  55.  
  56. map.put("id", String.valueOf(i));
  57. map.put("name", "Name:" + e.getString("name"));
  58. map.put("rent", "Rent:" + e.getString("rent"));
  59. map.put("phone", "phone:" + e.getString("phone"));
  60. map.put("units", "Available Units:" + e.getString("units"));
  61. map.put("lat", "lat" + e.getString("lat"));
  62. map.put("lon", "lon" + e.getString("lon"));
  63. map.put("image", "image" + e.getString("image"));
  64.  
  65. mylist.add(map);
  66. }
  67. }catch(JSONException e) {
  68. Log.e("log_tag", "Error kupitisha data "+e.toString());
  69. }
  70. URL u;
  71. final ListView lv = getListView();
  72. lv.setTextFilterEnabled(true);
  73. try {
  74. int position = 0;
  75. HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position );
  76. String image =o.get("image");
  77. u = new URL(image);
  78. final URLConnection conn = u.openConnection();
  79. final BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
  80. bm = BitmapFactory.decodeStream(bis);
  81. n.setImageBitmap(bm);
  82. }
  83. catch (MalformedURLException e) {
  84.  
  85. Log.e("log_tag", "Mallformed url from json "+e.toString());
  86. } catch (IOException e) {
  87.  
  88. Log.e("log_tag", "IoException "+e.toString());
  89. }
  90. //n = (ImageView)findViewById(R.id.imageView1);
  91. ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main,
  92.  
  93. new String[] { "name", "rent","units"},
  94. new int[] { R.id.item_title, R.id.item_subtitle1, R.id.item_subtitle2});
  95. n = (ImageView)findViewById(R.id.imageView1);
  96.  
  97. setListAdapter(adapter);
  98.  
  99. final ListView lv2 = getListView();
  100. lv2.setTextFilterEnabled(true);
  101. lv2.setOnItemClickListener(new OnItemClickListener() {
  102. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  103. @SuppressWarnings("unchecked")
  104. HashMap<String, String> o = (HashMap<String, String>) lv2.getItemAtPosition(position);
  105. String number ="tel:"+o.get("phone");
  106. Intent call = new Intent (Intent.ACTION_CALL,Uri.parse(number));
  107. startActivity(call);
  108.  
  109. }
  110. });
  111. }
  112. }
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement