NooNoo NooNoo - 10 months ago 141
Java Question

How to change text color individual item dynamically?

How to change text color individual item of listview dynamically?
I want to implement the following function in Android.


  1. Call API regularly and get each price data

  2. Print in listview

  3. Calculate each price margin

  4. If a certain item's margin is minus, change color to red. plus is green.



This function is often seen in Cryptocurrency Apps.
like that
https://www.youtube.com/watch?v=UuCkg_P6_Ws

I implemented 1, 2, and 3.
How to implement 4?
If you know anything about that please let me know.

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;
}
}


Calling API code is like that

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) {

}
});


image

Answer Source

Simple putting the else condition would work.

Assuming your default colour for TextView is Black.

if(x < 0 ){
  textview.setTextColor(Color.RED);
}else if(x > 0){
  textview.setTextColor(Color.GREEN);
}else{
  //your textview default colour
  textview.setTextColor(Color.BLACK);
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download