java - "&minus"而不是 TextView 中的 "-",它从 JSON 设置文本

标签 java android json textview retrofit2

我有天气应用程序,它从这里获取天气信息 json:

http://icomms.ru/inf/meteo.php/?tid=44

"temp" 值有一个负号(例如:"temp":"−16"),当我使用 retrofit2 和在 TextView 中显示它显示 −16 而不是 -16

Screenshot

如何显示 -16 而不是 −16

来自 RecyclerViewAdapter 的 fragment (我用它来显示多天的天气信息),我将文本设置为 TextView

@Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Meteo meteo = data.get(position);
        holder.date.setText(meteo.date);
        holder.tod.setText(meteo.tod);
        holder.pressure.setText(meteo.pressure);

        // THIS IS TEMPERATURE SETTING TEXT LINE
        holder.temp.setText(meteo.temp);

        holder.humidity.setText(meteo.humidity);
        holder.wind.setText(meteo.wind);
        holder.cloud.setText(meteo.cloud);
}

天气数据类:

public class Meteo {
    public String date;
    public String tod;
    public String pressure;
    public String temp;
    public String humidity;
    public String wind;
    public String cloud;
}

响应正文:

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(APIService.HOST)
            .addConverterFactory(GsonConverterFactory
                    .create())
            .build();

    APIService apiService = retrofit.create(APIService.class);

    Call<List<Meteo>> call = apiService.getMeteo(44);

    call.enqueue(new Callback<List<Meteo>>() {
        @Override
        public void onResponse(Call<List<Meteo>> call, Response<List<Meteo>> response) {
            MeteoAdapter adapter = new MeteoAdapter(response.body());
            // Method show just shows list of weather data
            getViewState().show(adapter);
        }

        @Override
        public void onFailure(Call<List<Meteo>> call, Throwable t) {
            Log.d("MyLog", "WRONG");
        }
    });

最佳答案

试试这个。我用它来格式化 TextView 中的 html 标签:

String s = Html.fromHtml("&minus;16");
tv.setText(s);

推理:它将文本设置为 Spanned 对象,而不是字符串。

注意:目前无法对此进行测试,如果不起作用,请见谅。

关于java - "&minus"而不是 TextView 中的 "-",它从 JSON 设置文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54466049/

相关文章:

java - DocumentListener 会减慢 Document.setCharacterAttributes 方法的速度吗?

android - 窗口已经聚焦,忽略 : com. android.internal.view.IInputMethodClient$Stub$Proxy@41141af0 的聚焦增益

android - Flutter-双向滚动中的固定位置

javascript - getJSON 调用的 "missing ; before statement"错误

java - Eclipse rcp 导出产品抛出 NullPointerException

java - 圆形 PNG 图像使其在 Android 上具有交互性

java - Spock 如何为 Java 类模拟同一个类的方法

android - 使用可观察的 ScrollView 时,如何让我的选项卡 View 填满整个屏幕?

java - Json Jackson Mapper 通用 map 类型问题

javascript - 确定 Javascript 中调用的 JSON 文件以在 cURL 中用于数据挖掘 - twitter 像 'more' 按钮