使用 AsyncTask 根据值定义文本颜色

Define text color based on value using AsyncTask

所以基本上我的 AsyncTask 从 url 获取值,当我执行时我想显示绿色文本或红色文本(如果包含(“-”))。 我四处搜索,none 选项对我有用。我确实有一个 RecyclerView.ViewHolder 但在我执行之前不知道如何合并。一切正常,除了颜色。 提前谢谢你

Activity

public class BTCData extends AsyncTask<Void,RecyclerView.ViewHolder,Void> {
    String data = "";
    String dataParsed1h ="";
    String dataParsed24h ="";
    String dataParsed7d ="";
    String percent_change_1h = "";
    String percent_change_24h = "";
    String percent_change_7d = "";
    Activity activity;
    List<Model> items;

    public BTCData() {

    }

    @Override
    protected Void doInBackground(Void... voids) {
        try {
            URL url = new URL ("https://...");
            HttpURLConnection httpURLConnection = (java.net.HttpURLConnection) url.openConnection();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String line = "";
            while (line != null){
                line = bufferedReader.readLine();
                data = data+line;
            }

            JSONArray JA = new JSONArray(data);
            for(int i=0 ;i< JA.length(); i++){
                JSONObject JO = (JSONObject) JA.get(i);
                percent_change_1h =  "1H  " + JO.getString("percent_change_1h") + "%";
                percent_change_24h = "24H  " + JO.getString("percent_change_24h") + "%";
                percent_change_7d = "7D  " + JO.getString("percent_change_7d") + "%" ;

                dataParsed1h = dataParsed1h + percent_change_1h;
                dataParsed24h = dataParsed24h + percent_change_7d;
                dataParsed7d = dataParsed7d + percent_change_24h;


            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        MainActivity.btc_percent_change_1h.setText(this.dataParsed1h);
        MainActivity.btc_percent_change_24h.setText(this.dataParsed24h);
        MainActivity.btc_percent_change_7d.setText(this.dataParsed7d);
    }
}```

**View Holder**
public class CoinAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

Adapter adapter;
boolean isLoading;
Activity activity;
List<Model> items;

int visibleThreshold = 5,lastVisibleItem, totalItemcount;

public void setAdapter(Adapter adapter) {
    this.adapter = adapter;
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(activity)
            .inflate(R.layout.activity_main,parent,false);
    return new CoinViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
    Model item = items.get(position);
    CoinViewHolder holderItem = (CoinViewHolder)holder;

    holderItem.btc_percent_change_1h.setTextColor(item.getPercentage_change_1h().contains("-")?
            Color.parseColor("#FF0000"):Color.parseColor("#32CD32"));
    holderItem.btc_percent_change_24h.setTextColor(item.getPercentage_change_24h().contains("-")?
            Color.parseColor("#FF0000"):Color.parseColor("#32CD32"));
    holderItem.btc_percent_change_7d.setTextColor(item.getPercentage_change_7d().contains("-")?
            Color.parseColor("#FF0000"):Color.parseColor("#32CD32"));

}

@Override
public int getItemCount() {
    return items.size();
}
public void setLoader() {isLoading = true;}
public void updateData (List<Model> models)
{
    this.items = models;
    notifyDataSetChanged();
}
**on Activity**


    public static TextView btc_percent_change_1h;
    public static TextView btc_percent_change_24h;
    public static TextView btc_percent_change_7d;



//Percentage
        btc_percent_change_1h = (TextView) findViewById(R.id.btc_percent_change_1h);
        btc_percent_change_24h = (TextView) findViewById(R.id.btc_percent_change_24h);
        btc_percent_change_7d = (TextView) findViewById(R.id.btc_percent_change_7d);


and finally call...

BTCData process = new BTCData();
        process.execute();

这是因为您在 onBindViewHolder 中设置颜色,在这种情况下,数据仅为 0,没有红色符号,稍后您正在设置数据,但之后不会调用 onBindViewHolder,因此不会反映更改。

你做这一切的方式不是理想的方式,建议你阅读设计模式以正确的方式实现它。

也许不是最好的选择,但它确实有效

@Override
    protected void onPostExecute(CoinAdapter aVoid) {
        super.onPostExecute(aVoid);
        MainActivity.btc_percent_change_1h.setTextColor((this.dataParsed1h.contains("-")?
                Color.parseColor("#FF0000"):Color.parseColor("#32CD32")));
        MainActivity.btc_percent_change_1h.setText(this.dataParsed1h);
        MainActivity.btc_percent_change_24h.setTextColor((this.dataParsed24h.contains("-")?
                Color.parseColor("#FF0000"):Color.parseColor("#32CD32")));
        MainActivity.btc_percent_change_24h.setText(this.dataParsed24h);
        MainActivity.btc_percent_change_7d.setTextColor((this.dataParsed7d.contains("-")?
                Color.parseColor("#FF0000"):Color.parseColor("#32CD32")));
        MainActivity.btc_percent_change_7d.setText(this.dataParsed7d);
    }

喜欢就可以实现。根据位置获取适配器中的值并像这样更改它的颜色。然后连接两个字符串并显示它。您可以在多台设备上测试它 运行 并在每台设备上准确显示。

String status = getColoredSpanned(act.getResources().getString(R.string.order_status),"#3EA7BD");
               String variable_status =getColoredSpanned(act.getResources().getString(R.string.status_order_pending),"#E23941");
               String text = status+variable_status;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    myViewHolder.tv_order_product_status.setText(Html.fromHtml(text,  Html.FROM_HTML_MODE_LEGACY), TextView.BufferType.SPANNABLE);
                } else {
                    myViewHolder.tv_order_product_status.setText(Html.fromHtml(text), TextView.BufferType.SPANNABLE);
                }