Java 根据ArrayList中的值更改textview的颜色<;HashMap<;字符串,字符串>&燃气轮机;
您好,我有以下元素的ArrayList,它从xml文件中获取值。之后,数据被馈送到一个简单的适配器,该适配器显示数据。我希望R.id.stock_mov的颜色根据值进行更改。如果为负->红色,如果为正,则为绿色。我找不到一个方法来做这件事Java 根据ArrayList中的值更改textview的颜色<;HashMap<;字符串,字符串>&燃气轮机;,java,android,xml,arraylist,colors,Java,Android,Xml,Arraylist,Colors,您好,我有以下元素的ArrayList,它从xml文件中获取值。之后,数据被馈送到一个简单的适配器,该适配器显示数据。我希望R.id.stock_mov的颜色根据值进行更改。如果为负->红色,如果为正,则为绿色。我找不到一个方法来做这件事 ArrayList<HashMap<String, String>> stackItems = new ArrayList<HashMap<String, String>> (); //
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);
}
ArrayList stackItems=new ArrayList();
//final HashMap dspStack=新HashMap();
NodeList stock=doc.getElementsByTagName(“stock”);
对于(int i=0;i0)
R.id.stock_val,
R.id.stock_mov,});
setListAdapter(适配器);
}
您需要在适配器的getView()方法中执行此操作。让它检查股票的价值,然后相应地设置其各自的颜色:
@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;
}
该代码未编译,对吗?请显示您的库存项目XML布局好吗?