Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to get only one restaurant when I press click button one click

    • 0
    • 0
    • 0
    • 3
    • 0
    • 0
    • 0
    • 471
    Answer it

    I have an app and inside it only one activity (main activity) and layout XML of main activity have only 
    list view . This app will get restaurants near from my location and display it in list view.
    What I need to do is add button to the main activity layout, when I press on button get to me list of restaurant item by item
    from json file and display it in list view but not display all restaurants in one click but show item by item
    every click get restaurant.

    suppose I have 3 restaurant in json file
    when I click on button for first click get me first restaurant
    when I click on button for second click get me second restaurant
    when I click on button for third click get me third restaurant 
    How to customize this code to accept get item by item restaurant from json file
    My code includes class and activity and code is working success  
    my class is FoursquareVenue

    package com.foodsmap_project_app.foodsmap_project_app;
    public class FoursquareVenue
    {
        private String name;
        private String city;
    
        private String category;
    
        public FoursquareVenue() {
            this.name = "";
            this.city = "";
            this.setCategory("");
        }
    
        public String getCity() {
            if (city.length() > 0) {
                return city;
            }
            return city;
        }
    
        public void setCity(String city) {
            if (city != null) {
                this.city = city.replaceAll("\\(", "").replaceAll("\\)", "");
                ;
            }
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getName() {
            return name;
        }
    
        public String getCategory() {
            return category;
        }
    
        public void setCategory(String category) {
            this.category = category;
        }
    
    }

    MainActivity.java

    public class MainActivity extends ListActivity {
        ArrayList<FoursquareVenue> venuesList;
       final String CLIENT_ID = "SVIBXYYXOEARXHDI4FWAHXGO5ZXOY204TCF1QJFXQLY5FPV4";
        final String CLIENT_SECRET = "BAAJO1KXRWESGTJJGVON4W3WUFHAQDAJPLRIYJJ5OPHFQ5VI";
       final String latitude = "30.435665153239377";
       final String longtitude = "31.3280148";
        ArrayAdapter <String> myAdapter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            new fourquare().execute();
    
        }
        private class fourquare extends AsyncTask<View, Void, String> {
    
            String temp;
    
            @Override
            protected String doInBackground(View... urls) {
    
               
                temp = makeCall("https://api.foursquare.com/v2/venues/search?client_id=" + CLIENT_ID + "&client_secret=" + CLIENT_SECRET + "&v=20130815&ll=30.435665153239377,31.144435908398464" + "&query=resturant");
               
    
                return "";
            }
    
            @Override
            protected void onPreExecute() {
    
            }
    
            @Override
            protected void onPostExecute(String result) {
                if (temp == null) {
    
                } else {
    
                    venuesList = (ArrayList<FoursquareVenue>) parseFoursquare(temp);
    
                    List<String> listTitle = new ArrayList<String>();
    
                    for (int i = 0; i < venuesList.size(); i++) {
    
                        listTitle.add(i, venuesList.get(i).getName() + ", " + venuesList.get(i).getCategory() + "" + venuesList.get(i).getCity());
                    }
    
    
                    myAdapter = new ArrayAdapter<String>(MainActivity.this, R.layout.row_layout, R.id.listText, listTitle);
                    setListAdapter(myAdapter);
                }
            }
        }
        public static String makeCall(String url) {
    
    
            StringBuffer buffer_string = new StringBuffer(url);
            String replyString = "";
    
    
            HttpClient httpclient = new DefaultHttpClient();
    
            HttpGet httpget = new HttpGet(buffer_string.toString());
    
            try {
    
                HttpResponse response = httpclient.execute(httpget);
                InputStream is = response.getEntity().getContent();
    
    
                BufferedInputStream bis = new BufferedInputStream(is);
                ByteArrayBuffer baf = new ByteArrayBuffer(20);
                int current = 0;
                while ((current = bis.read()) != -1) {
                    baf.append((byte) current);
                }
    
                replyString = new String(baf.toByteArray());
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            return replyString.trim();
        }
        private static ArrayList<FoursquareVenue> parseFoursquare(final String response) {
    
            ArrayList<FoursquareVenue> temp = new ArrayList<FoursquareVenue>();
            try {
    
    
                JSONObject jsonObject = new JSONObject(response);
    
    
                if (jsonObject.has("response")) {
                    if (jsonObject.getJSONObject("response").has("venues")) {
                        JSONArray jsonArray = jsonObject.getJSONObject("response").getJSONArray("venues");
    
                        for (int i = 0; i < jsonArray.length(); i++) {
                            FoursquareVenue poi = new FoursquareVenue();
                            if (jsonArray.getJSONObject(i).has("name")) {
                                poi.setName(jsonArray.getJSONObject(i).getString("name"));
    
                                if (jsonArray.getJSONObject(i).has("location")) {
                                    if (jsonArray.getJSONObject(i).getJSONObject("location").has("address")) {
                                        if (jsonArray.getJSONObject(i).getJSONObject("location").has("city")) {
                                            poi.setCity(jsonArray.getJSONObject(i).getJSONObject("location").getString("city"));
                                        }
                                        if (jsonArray.getJSONObject(i).has("categories")) {
                                            if (jsonArray.getJSONObject(i).getJSONArray("categories").length() > 0) {
                                                if (jsonArray.getJSONObject(i).getJSONArray("categories").getJSONObject(0).has("icon")) {
                                                    poi.setCategory(jsonArray.getJSONObject(i).getJSONArray("categories").getJSONObject(0).getString("name"));
                                                }
                                            }
                                        }
                                        temp.add(poi);
                                    }
                                }
                            }
                        }
                    }
                }
    
            } catch (Exception e) {
                e.printStackTrace();
                return new ArrayList<FoursquareVenue>();
            }
            return temp;
    
        }
    
    }

     

 3 Answer(s)

  • Thank you for reply You are true but i face problem in implementation code can you help me my code as following

    public class MainActivity extends ListActivity { ArrayList venuesList; Button btn; int clickCount; List shownResults = new ArrayList(); final String CLIENT_ID = "SVIBXYYXOEARXHDI4FWAHXGO5ZXOY204TCF1QJFXQLY5FPV4"; final String CLIENT_SECRET = "BAAJO1KXRWESGTJJGVON4W3WUFHAQDAJPLRIYJJ5OPHFQ5VI"; final String latitude = "30.435665153239377"; final String longtitude = "31.3280148"; ArrayAdapter myAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity&#95;main);
       new fourquare().execute();
        btn=(Button) findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
    
                String name = venuesList.get(clickCount).getName();
    
                String cat = venuesList.get(clickCount).getCategory();
    
                String city = venuesList.get(clickCount).getCity();
    
                shownResults.add(name + "," + "cat" + "" + city);
    
                clickCount++;
    
                myAdapter.notifyDataSetChanged();
                new fourquare().execute();
    
            }
        });
    
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: