自定义列表适配器视图的代码(此列表只有 11 项),并且查看器在这里没有帮助。
@Override
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if( convertView == null ){ // for fast view google io http://www.youtube.com/watch?v=wDBM6wVEO70
convertView = inflater.inflate(R.layout.single_transaction,parent,false);
}
TextView phone_number = (TextView) convertView.findViewById(R.id.phone_number_show);
TextView transaction_summary = (TextView) convertView.findViewById(R.id.transaction_summary_show);
TextView transaction = (TextView) convertView.findViewById(R.id.transaction_show);
Transactions single_transaction = (Transactions) getItem(position);
phone_number.setText(String.valueOf(single_transaction.get_phone_number()));
transaction_summary.setText(single_transaction.get_transaction_summary());
int single_transaction_value = single_transaction.get_transaction();
if(single_transaction_value < 0) {
transaction.setBackgroundColor(Color.parseColor("#098AAA"));
}
transaction.setText(String.valueOf(single_transaction_value));
return convertView;
}
这里如果条件改变了在 xml 中定义的 textview 的默认颜色(天蓝色),
所以如果 single_transaction_value < 0 textview 的颜色更改为 #098AAA(浅绿色)
颜色没有按预期改变。
请注意,顶部栏的颜色是正确的。电话号码的灰色。白色背景上的文本。交易的天蓝色或浅绿色取决于条件。奇怪的行为是颜色从 xml 中定义的默认天蓝色变为代码中定义的浅绿色。
现在 if 条件发生了一点变化,添加了一个 else
if(single_transaction_value < 0) {
transaction.setBackgroundColor(Color.parseColor("#098AAA"));
}
else {
transaction.setBackgroundColor(Color.parseColor("#76BB51"));
}
现在颜色正在按预期变化。
这怎么样..