根据ArrayList中的值更改textview的颜色 >

问题描述 投票:1回答:1

嗨,我有以下ArrayList元素从xml文件中获取值。之后,数据被送到一个显示它的简单适配器。我希望R.id.stock_mov的颜色根据值改变。如果是负 - >红色,如果它是正的那么是绿色。我无法找到如何做到这一点的方法

         ArrayList<HashMap<String, String>> stackItems = new ArrayList<HashMap<String, String>> ();
    // final HashMap<String, String> dspStack = new HashMap<String, String>();

     NodeList stock = doc.getElementsByTagName("stock");
        for (int i=0; i<stock.getLength(); i++){
            HashMap<String, String> map = new HashMap<String, String>();
            Node nodeCurr = stock.item(i);
             Element currElmnt = (Element) nodeCurr;
             map.put("name", parser.getValue(currElmnt, "name"));
             map.put("val", parser.getValue(currElmnt, "val"));
             map.put("mov", parser.getValue(currElmnt, "mov"));
             stackItems.add(map);

        }

        ListAdapter adapter = new SimpleAdapter(this, stackItems,
                R.layout.stocks_def_item,
                new String[] { "name", "val", "mov"}, new int[] {
                        R.id.stock_name,

                        if(stackItems.get(i)>0)
                        R.id.stock_val,
                        R.id.stock_mov,});

        setListAdapter(adapter);


}
java android xml arraylist colors
1个回答
0
投票

您需要在adaptergetView()方法中执行此操作。检查库存的价值,然后相应地设置相应的颜色:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // use the ------- ^position^ parameter to access the current stock's value
    double currentStockValue = ...;

    TextView stockMove = (TextView)convertView.findViewById(R.id.stock_mov);

    if( currentStockValue > 0) // if the current stock's value is positive
        stockMove.setTextColor(Color.parseColor("#6AC36A")); // set text color to green
    else
        stockMove.setTextColor(Color.parseColor("#FF3300")); // set text color to red   

    ...
    return convertView;
}
© www.soinside.com 2019 - 2024. All rights reserved.