1

對於Android而言,我是新來的,我構建了一個應用程序來顯示來自XML提要的股票價格。如何根據正值或負值在自定義列表視圖中更改數組列表項的顏色

我有一個包含3個項目的數組列表。但是,如果數組列表中的其中一個項目的顏色更改爲紅色(如果其爲-ve)或綠色(如果其+ ve),則該顏色將變爲紅色。

我不知道如何做到這一點或在我的代碼中最好做到這一點。

請幫助....

我的適配器類:

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; 

} 

我row_metal佈局:

<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" 
    /> 

回答

1

它是如此簡單。你只需要把你的getView()方法中的這些行:

 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")); 
        } 
+0

這將改變TextView的唯一 – desidigitalnomad

+0

答案我更新的顏色。 – Dariush

+0

謝謝你們,非常感謝幫助。 – user3303848

1

更改getView()方法本

@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; 
} 
相關問題