Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.pxr.tutorial.json;
- import java.io.BufferedInputStream;
- import java.io.IOException;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.net.URLConnection;
- import java.util.ArrayList;
- import java.util.HashMap;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import android.app.ListActivity;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.net.Uri;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.view.Window;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.ImageView;
- import android.widget.ListAdapter;
- import android.widget.ListView;
- import android.widget.SimpleAdapter;
- import com.pxr.tutorial.xmltest.R;
- public class Main extends ListActivity {
- /** Called when the activity is first created. */
- ImageView n;
- Bitmap bm;
- BufferedInputStream is;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.listplaceholder);
- ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
- JSONObject json = JSONfunctions.getJSONfromURL("http://10.0.2.2/android/rental.php");
- try{
- JSONArray people = json.getJSONArray("rental");
- for(int i=0;i<people.length();i++){
- HashMap<String, String> map = new HashMap<String, String>();
- JSONObject e = people.getJSONObject(i);
- map.put("id", String.valueOf(i));
- map.put("name", "Name:" + e.getString("name"));
- map.put("rent", "Rent:" + e.getString("rent"));
- map.put("phone", "phone:" + e.getString("phone"));
- map.put("units", "Available Units:" + e.getString("units"));
- map.put("lat", "lat" + e.getString("lat"));
- map.put("lon", "lon" + e.getString("lon"));
- map.put("image", "image" + e.getString("image"));
- mylist.add(map);
- }
- }catch(JSONException e) {
- Log.e("log_tag", "Error kupitisha data "+e.toString());
- }
- URL u;
- final ListView lv = getListView();
- lv.setTextFilterEnabled(true);
- try {
- int position = 0;
- HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position );
- String image =o.get("image");
- u = new URL(image);
- final URLConnection conn = u.openConnection();
- final BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
- bm = BitmapFactory.decodeStream(bis);
- n.setImageBitmap(bm);
- }
- catch (MalformedURLException e) {
- Log.e("log_tag", "Mallformed url from json "+e.toString());
- } catch (IOException e) {
- Log.e("log_tag", "IoException "+e.toString());
- }
- //n = (ImageView)findViewById(R.id.imageView1);
- ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main,
- new String[] { "name", "rent","units"},
- new int[] { R.id.item_title, R.id.item_subtitle1, R.id.item_subtitle2});
- n = (ImageView)findViewById(R.id.imageView1);
- setListAdapter(adapter);
- final ListView lv2 = getListView();
- lv2.setTextFilterEnabled(true);
- lv2.setOnItemClickListener(new OnItemClickListener() {
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- @SuppressWarnings("unchecked")
- HashMap<String, String> o = (HashMap<String, String>) lv2.getItemAtPosition(position);
- String number ="tel:"+o.get("phone");
- Intent call = new Intent (Intent.ACTION_CALL,Uri.parse(number));
- startActivity(call);
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement