-1

I have a question: Is it possible to divide a very long text, which the app gets from the Internet in a Json-data, into small parts of the text? Then these parts should be shown in single cardviews.

If it's possible, can somebody show me how? I tried a very long time and didn't get it. Thanks for help!


EDIT

That's the jsonRequest and it builds the TextView.

    jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "http://example.com/document.json",
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {

                    try {
                        JSONArray jsonArray = response.getJSONArray("data1");

                        for (int i = 0; i < jsonArray.length(); i++) {

                            JSONObject obj = jsonArray.getJSONObject(i);

                            String a = obj.getString("a");
                            String b = obj.getString("b");
                            String c = obj.getString("c");
                            String d = obj.getString("d");

                            textView.append(Html.fromHtml("<p><b>" + a + "</b><br>" + b + "<br>" + c + "<br>" + d + "</p>"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },

            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("VOLLEY", error.toString());
                }
            }


    );

    requestQueue.add(jsonObjectRequest);

    ;

Now the TextView should be divided into small parts. In every part should be the Strings a, b, c and d. And around these small parts should be a cardview.

android
  • 21
  • 1
  • 5
  • [Paginating text in Android](http://stackoverflow.com/questions/31837840/paginating-text-in-android/32096884#32096884) – Onik Apr 25 '17 at 18:51

3 Answers3

0

One attempt would be :

Break the text parts with something unique like , and then you can use

StringTokenizer tokens = new StringTokenizer(comma_string_here, ",");
String first_text = tokens.nextToken();
String second_text = tokens.nextToken();

You can loop through and set background as you desire for those text.

Vikrant
  • 444
  • 1
  • 5
  • 22
0

you can split text based on any special character or space if you provide me sample data, so I can give you proper solutions.

deepak p
  • 76
  • 2
0

First let me say I think there must be a better approach to do what you want to do, but if you want to really cut a string in pieces to show it different cardviews, another way of doing it is to cut it based on a fixed number of characters.

This function will take the text and the fixed limit. And it will return an array. In the first position it will have the thext that has been cut out and in the second position the remaining text.

The cut part may fall in the middle of a word. If you don't want that, you can add some more logic using rtn[0].lastIndexOf(" ") to search for the last blank space and adjust the results accordingly.

public static String[] limitToNChars(String text, int limit){
    String rtn[] = new String[2];
    if(text.length() <= limit){
        rtn[0] = text;
        rtn[1] = "";
    }else{
        rtn[0] = text.substring(0,limit);
        rtn[1] = text.substring(limit);
    }
    return rtn;
}

You can call this function in a loop pasing the result's second position as the first parameter of the function until the second position is an empty string to get all the substrings from the initial string.

Juan
  • 5,525
  • 2
  • 15
  • 26
  • Thanks, but do you know how I can put these small parts after building substrings in cardviews? – android Apr 25 '17 at 16:35