android - 如何根据正值或负值更改自定义 ListView 中数组列表项的颜色

标签 android android-layout android-listview arraylist android-arrayadapter

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

最佳答案

就是这么简单。您只需要将这些行放在您的 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"));
                    }

关于android - 如何根据正值或负值更改自定义 ListView 中数组列表项的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23120428/

相关文章:

android - 如何让我的 TTS 不与谷歌地图语音重叠(用于行车方向)?

android - 使用 defStyleAttr 无法在当前主题中找到自定义 View 的样式

Android 小部件位图大小

java - Android studio 中的入职屏幕

Android 依次下载多个文件并在ListView中显示进度

android - 加载android时EditTextPreference总是显示默认摘要

android - 如何管理不同大小的布局

android - 在 scrollView android 上实现缩放

单个项目上的 Android StickyListHeaders

android - ListView 性能很慢