如何动态更改单个项目的文本颜色?

时间:2017-12-23 05:40:54

标签: java android listview android-arrayadapter

如何动态更改listview的单个项目的文本颜色? 我想在Android中实现以下功能。

  1. 定期调用API并获取每个价格数据
  2. 在列表视图中打印
  3. 计算每个价格保证金
  4. 如果某个项目的边距为负,则将颜色更改为红色。加上是绿色的。
  5. 此功能通常出现在Cryptocurrency Apps中。 像那样 https://www.youtube.com/watch?v=UuCkg_P6_Ws

    我实施了1,2和3。 如何实施4? 如果你对此有所了解,请告诉我。

    public class CoinAdapter extends ArrayAdapter<CoinItem> {
    Context context;
    LayoutInflater layoutInflater;
    ArrayList<CoinItem> coinItem;
    float x;
    import android.content.Context;
    import android.content.res.ColorStateList;
    import android.graphics.Color;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    import android.widget.ArrayAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;
    import java.util.ArrayList;
    
    
    public class CoinAdapter extends ArrayAdapter<CoinItem> {
    Context context;
    LayoutInflater layoutInflater;
    ArrayList<CoinItem> coinItem;
    float x;
    
    
    public CoinAdapter(Context context, int resource, ArrayList<CoinItem> coinItem) {
        super(context, resource, coinItem);
        this.context = context;
        this.layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.coinItem = coinItem;
    }
    
    public float diff(String btc_last, String btc_prelast) {
        x = Float.parseFloat(btc_last) - Float.parseFloat(btc_prelast);
        return x;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (null == convertView) {
            convertView = layoutInflater.inflate(R.layout.item_ticker, null);
        }
    
        ImageView icon_coin = (ImageView) convertView.findViewById(R.id.icon_coin);
        icon_coin.setImageBitmap(coinItem.get(position).getIcon_coin());
    
        TextView text_last = (TextView) convertView.findViewById(R.id.text_last);
        text_last.setText(coinItem.get(position).getText_last() + "円");
        if (x > 0) text_last.setTextColor(Color.GREEN);
        else if (x < 0) text_last.setTextColor(Color.RED);
        else text_last.setTextColor(Color.BLACK);
    
        TextView text_coin = (TextView) convertView.findViewById(R.id.text_coin);
        text_coin.setText(coinItem.get(position).getText_coin());
    
        TextView text_bid = (TextView) convertView.findViewById(R.id.text_bid);
        text_bid.setText("売値");
    
        TextView text_bid_price = (TextView) convertView.findViewById(R.id.text_bid_price);
        text_bid_price.setText(coinItem.get(position).getText_bid_price() + "円");
    
        TextView text_ask = (TextView) convertView.findViewById(R.id.text_ask);
        text_ask.setText("買値");
    
        TextView text_ask_price = (TextView) convertView.findViewById(R.id.text_ask_price);
        text_ask_price.setText(coinItem.get(position).getText_ask_price() + "円");
    
        return convertView;
    }
    }
    

    调用API代码就是那样

        final Handler handler = new Handler();
        final int delay = 1000;
        btc_prelast="0";
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                coin_btc.clone().enqueue(new Callback<Ticker>() {
                    @Override
                    public void onResponse(Call<Ticker> call, Response<Ticker> response) {
                        btc_last = Float.toString(response.body().getLast());
                        btc_bid = Float.toString(response.body().getBid());
                        btc_ask = Float.toString(response.body().getAsk());
                        coinItem.set(0, new CoinItem(BitmapFactory.decodeResource(getResources(), icon_coin[0]), btc_last, name_coin[0], btc_bid, btc_ask));
                        coinAdapter.diff(btc_last,btc_prelast);
                        btc_prelast = btc_last;
                        coinAdapter.notifyDataSetChanged();
    
                    }
                    @Override
                    public void onFailure(Call<Ticker> call, Throwable t) {
    
                    }
                });
    

3 个答案:

答案 0 :(得分:0)

我假设您在列表视图中已经在布局中有TextView。您所要做的就是调用TextView#setTextColor。一个例子是:

TextView t = (TextView)v.findViewById(R.id.textviewname);
t.setTextColor(Color.RED);

答案 1 :(得分:0)

简单地放置else条件就行了。

假设TextView的默认颜色为黑色。

if(x < 0 ){
  textview.setTextColor(Color.RED);
}else if(x > 0){
  textview.setTextColor(Color.GREEN);
}else{
  //your textview default colour
  textview.setTextColor(Color.BLACK);
}

答案 2 :(得分:0)

在你的Getview方法中, 检查你的保证金。

    if(margin > 0){textview.setTextColor(Color.GREEN);}
    else if(margin < 0){textview.setTextColor(Color.RED);}
    else {textview.setTextColor(Color.BLACK);}

检查此条款。