我有一个listview和自定义适配器为它扩展SingleTypeAdapter。列表视图包含textview(s)。每当文本视图中的数值更改时,文本颜色也应该更改。例如。如果textview更改为无符号数,则颜色应为红色,否则为黄色。如果我想要最佳表现,我怎么能这样做?
我只对改变红色和白色之间的颜色感兴趣,仅适用于3个viewtexts字段(更改,percentChange,price)。我该怎么做?
我的适配器:
/**
* Adapter to display a list of stock quotes
*/
public class StockListAdapter extends SingleTypeAdapter<CurrentStock> {
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("MMMM dd");
/**
* @param inflater
* @param items
*/
public StockListAdapter(LayoutInflater inflater, List<CurrentStock> items) {
super(inflater, R.layout.stock_quote_list_item);
setItems(items);
}
/**
* @param inflater
*/
public StockListAdapter(LayoutInflater inflater) {
this(inflater, null);
}
/*@Override
public long getItemId(final int position) {
final String id = getItem(position).getObjectId();
return !TextUtils.isEmpty(id) ? id.hashCode() : super
.getItemId(position);
}*/
@Override
protected int[] getChildViewIds() {
return new int[] { R.id.symbol, R.id.exchange, R.id.price, R.id.companyName, R.id.change, R.id.percentChange };
}
@Override
protected void update(int position, CurrentStock currentStock) {
setText(0, currentStock.getSymbol());
setText(1, currentStock.getExchange());
setText(2, currentStock.getPrice().toString());
setText(3, currentStock.getCompanyName());
setText(4, currentStock.getChange().toString());
setText(5, currentStock.getPercentChange());
}
}
列表的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp">
<TextView
android:id="@+id/companyName"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/table_text_selector"/>
<TextView
android:id="@+id/exchange"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/table_text_light_selector"
android:layout_below="@id/companyName"/>
<TextView
android:id="@+id/symbol"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/table_text_light_selector"
android:layout_below="@id/companyName"
android:layout_toLeftOf="@id/exchange"/>
<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:singleLine="true"
android:ellipsize="end"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/table_text_selector"
android:layout_alignParentRight="true"/>
<TextView
android:id="@+id/change"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/table_stock_change_text"
android:layout_alignParentRight="true"
android:layout_below="@id/price"/>
<TextView
android:id="@+id/percentChange"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/table_stock_change_text"
android:layout_alignBaseline="@id/change"
android:layout_alignBottom="@id/change"
android:layout_toLeftOf="@id/change"
android:layout_marginRight="5dp"/>
</RelativeLayout>
答案 0 :(得分:0)
编辑:getView
中的代码将更容易,并且在将文本解析为数字时不会遇到麻烦。请参阅新的代码段。
在StockListAdapter
添加getView()
方法以覆盖行为时,显示新的库存商品。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View line = super.getView(position, convertView, parent);
// Set price, change and %change to red/white
((TextView) line.findViewById(R.id.price)).setTextColor((currentStock.getPrice() < 0d)? 0xFFFF0000:0xFFFFFFFF);
((TextView) line.findViewById(R.id.change)).setTextColor((currentStock.getChange() < 0d)? 0xFFFF0000:0xFFFFFFFF);
((TextView) line.findViewById(R.id.percentChange)).setTextColor((currentStock.getPercentChange() < 0d)? 0xFFFF0000:0xFFFFFFFF);
}
一些评论: