简体   繁体   中英

How do I set onClick Listener in JSON for loop in order to retrieve the clicked item or text?

I am trying to retrieve the clicked text and to copy it to clipboard but when i do so in my for loop, despite on copying only the clicked text all the text gets copied to clipboard.

               private void jsonParse() {

    String url = "https://gist.githubusercontent.com/nasrulhazim/54b659e43b1035215cd0ba1d4577ee80/raw/e3c6895ce42069f0ee7e991229064f167fe8ccdc/quotes.json";
    request = new JsonObjectRequest(Request.Method.GET, url, null,
            new com.android.volley.Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        JSONArray jsonArray = response.getJSONArray("quotes");

                        for (int i = 0; i < 60; i++) {
                            quotes = jsonArray.getJSONObject(i);
                            String quote = quotes.getString("quote");
                            String author = quotes.getString("author");
                            quoteTextView.append("→ " + quote + "\n" + "By - " + author + "\n\n");
                           String copy = quotes.getString("quote");

                    }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new com.android.volley.Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    });

}

Thanks in advance.

You can add OnClick on your textView

and then get the Text on which the user clicks.

 quoteTextView.getText();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM