当我按下按钮时如何只获得一个餐厅单击项目监听器

时间:2016-06-06 10:49:13

标签: android

我有app只有一个活动(主要活动)和主要活动的布局XML只有 列表视图。这个应用程序从我的位置附近的餐馆,并在列表视图中显示 我需要做的是当按下按钮到项目列表中的按钮时,向主活动布局添加按钮 来自json文件并在列表视图中显示它,但不是一键显示所有餐馆,而是逐项显示 每次点击获得餐厅 假设我在json文件中有3个餐厅 当我点击按钮第一次点击让我第一个餐厅 当我点击第二次点击按钮给我第二个餐厅 当我点击第三次点击按钮得到我第三家餐厅 如何自定义此代码以接受从json文件中逐项获取项目 我的代码包括类和活动,代码正在成功运行 我的班级是FoursquareVenue

 public class MainActivity extends ListActivity {
    ArrayList<FoursquareVenue> venuesList;
    Button btn;
    int clickCount;

 //   ArrayList<FoursquareVenue> allResults;

    List<String> shownResults = new ArrayList<String>();

    //final String CLIENT_ID = "1CO3ZGVRFHXUIKWGFGDJBLJS4DNKX25EBR1TVEYSEUOFKCJD";
    //final String CLIENT_SECRET = "P24DH3AUZUW4Q3Q1WHLG4HED0WPPMHT4P30TOKG2EFWAJPIJ";
   // final String latitude = "30.435665153239377";
   // final String longtitude = "31.144435908398464";
   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();
        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();

            }
        });

    }
    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");
            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");
            //public static String URL_GET_Place = "https://api.foursquare.com/v2/venues/search?client_id=SVIBXYYXOEARXHDI4FWAHXGO5ZXOY204TCF1QJFXQLY5FPV4&client_secret=BAAJO1KXRWESGTJJGVON4W3WUFHAQDAJPLRIYJJ5OPHFQ5VI&v=20130815&ll=30.1136286,31.3280148&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;

    }

}

MainActivity.java

RewriteEngine on
RewriteCond %{THE_REQUEST} /tournament/index.php\?view=([^&]+)&id=([^&\s]+) [NC]
RewriteRule ^ /tournament/%1/%2? [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?:tournament/)?([^/]+)/?(.*)/?$ /tournament/index.php?view=$1&id=$2 [NC,L]

0 个答案:

没有答案