0

Here is my json URL https://jsonplaceholder.typicode.com/todos I want to display only completed: true is to be strike through, how can I do that?

MainActivity.java

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("https://jsonplaceholder.typicode.com/")
        .addConverterFactory(GsonConverterFactory.create())
        .build();

JsonPlaceHolderApi jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);

Call<List<Post>> call = jsonPlaceHolderApi.getPosts();

call.enqueue(new Callback<List<Post>>() {
    @Override
    public void onResponse(Call<List<Post>> call, Response<List<Post>> response) {
        if (!response.isSuccessful()) {
            textresult.setText("Code: " + response.code());
            return;
        }
        List<Post> posts = response.body();
        for (Post post : posts) {
            String content = "";
            content += "User ID: " + post.getUserId() + "\n";
            content += "ID: " + post.getId() + "\n";
            content += " Title: " + post.getTitle() + "\n";
            content += "Completed: " + post.getCompleted() + "\n\n";

            textresult.append(content);

            if (post.getCompleted().contains("true")) {
                textresult.setPaintFlags(textresult.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
            }
        }
    }
    @Override
    public void onFailure(Call<List<Post>> call, Throwable t) {
        textresult.setText(t.getMessage());
    }
});

JsonPlaceHolderApi.java

public interface JsonPlaceHolderApi {

    @GET("todos")                

    Call<List<Post>> getPosts();       


}

Post.java

public class Post {

    private int userId;

    private int id;

    private String title;

    private String completed;

    public int getUserId() {
        return userId;
    }

    public int getId() {
        return id;
    }

    public String getTitle() {
        return title;
    }

    public String getCompleted() {
        return completed;
    }
}

When I try to run the above code I got output as image, but I need if completed: true it should be strike.

jeprubio
  • 17,312
  • 5
  • 45
  • 56

1 Answers1

0

it is not possible to do setPaintFlags() with custom text better use can use HTML text for your work for you don't need to check "post.complete()", just append all content in a single string and replace your target string (complete: true) with HTML text.

just do it like

     String content = "";
        for (Post post : posts) {
        content += "User ID: " + post.getUserId() + "\n";
        content += "ID: " + post.getId() + "\n";
        content += " Title: " + post.getTitle() + "\n";
        content += "Completed: " + post.getCompleted() + "\n\n";
 }

 String tempHtmlText = content.replaceAll("Completed: true","<strike>Completed: true</strike>");

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        textresult.setText(Html.fromHtml(tempHtmlText , Html.FROM_HTML_MODE_LEGACY));
    } else {
        textresult.setText(Html.fromHtml(tempHtmlText));
    }

or you can use Gson formmater