Question

Im new to android and Im building an app to show stock prices from an XML feed.

I have got an array list containing 3 items. However, I want to change the color of one of the items in the array list to red if its -ve or green if its +ve.

I dont know how to do this or where in my code is best to do it.

Please help....

My Adapter class:

public class TheAdapter extends ArrayAdapter<TheMetal>{

public TheAdapter(Context ctx, int textViewResourceId, List<TheMetal> sites) {
    super(ctx, textViewResourceId, sites);
}



@Override
public View getView(int pos, View convertView, ViewGroup parent){
    RelativeLayout row = (RelativeLayout)convertView;
    Log.i("StackSites", "getView pos = " + pos);
    if(null == row){

        LayoutInflater inflater = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = (RelativeLayout)inflater.inflate(R.layout.row_metal, null);
    }




    TextView dispNameTxt = (TextView)row.findViewById(R.id.displayNameText);
    TextView spotPriceTxt = (TextView)row.findViewById(R.id.spotPriceText);
    TextView changeTxt = (TextView)row.findViewById(R.id.changeText);


    dispNameTxt.setText (getItem(pos).getDisplayName());
    spotPriceTxt.setText(getItem(pos).getSpotPrice());
    changeTxt.setText(getItem(pos).getChange());


    return row;

}

My row_metal layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp" >



<TextView
    android:id="@+id/displayNameText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="30sp"
    android:layout_marginLeft="20dp" />

<TextView
    android:id="@+id/spotPriceText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/displayNameText"
    android:textSize="19sp"
    android:textStyle="bold"
    android:gravity="right"
    android:layout_alignParentRight="true"
    android:layout_marginRight="30dp" />

<TextView
    android:id="@+id/changeText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/spotPriceText"
    android:layout_toRightOf="@+id/displayNameText"
    android:textSize="16sp"
    android:gravity="right"
    android:layout_alignParentRight="true"
    android:layout_marginRight="20dp"
    />

Was it helpful?

Solution

It is so simple. You just need to place these line within your getView() method:

        if (Double.parseDouble(getItem(pos).getChange())>=0) {
                     row.setBackgroundColor(Color.parseColor("#00FF00");
                     changeTxt.setTextColor(Color.parseColor("#00FF00"));
                    } else {  
                     row.setBackgroundColor(Color.parseColor("#FF0000");                      
                     changeTxt.setTextColor(Color.parseColor("#FF0000"));
                    }

OTHER TIPS

Change your getView() method to this

@Override  
public View getView(int pos, View convertView, ViewGroup parent){

  if(convertView == null)
    convertView = getLayoutInflater().inflate(R.layout.row_metal, null);


  TextView dispNameTxt = (TextView) convertView .findViewById(R.id.displayNameText);
  TextView spotPriceTxt = (TextView) convertView .findViewById(R.id.spotPriceText);
  TextView changeTxt = (TextView) convertView .findViewById(R.id.changeText);

  dispNameTxt.setText(getItem(pos).getDisplayName());
  spotPriceTxt.setText(getItem(pos).getSpotPrice());
  changeTxt.setText(getItem(pos).getChange());


  if( Double.parseDouble(getItem(pos).getSpotPrice()) >= 0 )
    convertView.setBackgroundColor(Color.GREEN);
  else
    convertView.setBackgroundColor(Color.RED);

  return convertView;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top