gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:06:52 25 4
gpt4 key购买 nike

我有天气应用程序,它从这里获取天气信息 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com