如何根据每个项目中显示的数据更改列表项目的背景颜色?
我有一个SQLite中的状态不同的订单列表:分配,加载,交付。我希望每个订单在列表中显示时都有不同的彩色背景。到目前为止,我还没有找到一个好办法来做到这一点。如何根据每个项目中显示的数据更改列表项目的背景颜色?
我发现很多关于如何更改的基于位置列表项的背景色的讨论,但没有基于数据内容。我还发现了很多关于如何更改用于突出显示所选项目的颜色的讨论。这些对我没有帮助。
唯一的方法,我想出了解决我的问题涉及到整个列表上运行,它已经由适配器创建后,并设置每个项目的背景。它很烂并且浪费。我希望有一个更高效的方法,可以让适配器中的背景在光标上创建时进行更改。
我确定有更好的方法。对于Android来说,我只是太新而不了解它。
我真的非常感谢迄今的回应。我正在尽我所能将它们合并,但我仍然没有成功。根据我所得到的答案和我所做的研究,我刚刚尝试了这些。
public class OrderListAdapter extends SimpleCursorAdapter {
private static final String TAG = "SimpleCursorAdapter";
Context _context = null;
int layoutResourceId = 0;
public OrderListAdapter(Context context, int layout, Cursor c,
String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
_context = context;
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
String tag = TAG + ".getView()";
Log.d(tag,"in getView()");
if (view == null) {
LayoutInflater inflater = ((Activity)_context).getLayoutInflater();
view = inflater.inflate(R.layout.fragment_order_list_row, null);
}
setRowColor(view);
return view;
}
private void setRowColor(View view) {
String tag = TAG + ".setRowColor()";
Cursor cursor = getCursor();
int col = cursor
.getColumnIndex(DBContract.DeliveryOrderTable.ENROUTE_FLAG);
String enroute_flag = cursor.getString(col);
Log.d(tag, "enroute_flag = [" + enroute_flag + "]");
col = cursor
.getColumnIndex(DBContract.DeliveryOrderTable.DELIVERED_DATETIME);
String deliveredDateStr = cursor.getString(col);
Log.d(tag, "deliveredDateStr = [" + deliveredDateStr + "]");
int bgColorId = 0;
if (!deliveredDateStr.equals("")) {
bgColorId = R.color.bg_status_delivered_color;
Log.d(tag, "Setting to delivered color");
} else if (enroute_flag.startsWith("T") || enroute_flag.startsWith("Y")) {
Log.d(tag, "Setting to enroute color");
bgColorId = R.color.bg_status_enroute_color;
} else {
Log.d(tag, "Setting to assigned color");
bgColorId = R.color.bg_status_assigned_color;
}
view.setBackgroundColor(_context.getResources().getColor(bgColorId));
}
}
不幸的是,这是行不通的。如果我没有调用super.getView(),那么显然,由于我没有明确进行传输,所以我在字段中没有数据,但我想我可以修改返回的视图。
我的痕迹告诉我,我读的数据,但背景颜色没有改变。
看来,我试图改变的看法是的LinearLayout,但改变背景颜色似乎不工作。
Got it!确保使所有子视图的背景透明。
,如果你正在使用的ListView任何自定义适配器的话,你将有一个方法getView(),在刚刚调用一个方法返回之前,并通过视图(这是返回),并根据您的数据要改变行的颜色。
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view = convertView;
if (view == null) {
LayoutInflater vi = (LayoutInflater) _c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.list_item_var, null);
}
TextView varView = (TextView) view.findViewById(R.id.var);
TextView valueView = (TextView) view.findViewById(R.id.value);
VarDetails var = _data.get(position);
setRowColor(view, var.getVar());
varView.setText(var.var);
valueView.setText("Value: " + var.value);
return view;
}
private void setRowColor(View view, String var) {
if("assigned".equalsIgnoreCase(var)){
view.setBackgroundColor(Color.rgb(255,0,0));
}else if("loaded".equalsIgnoreCase(var)){
view.setBackgroundColor(Color.rgb(0,255,0));
}else if("delivered".equalsIgnoreCase(var)){
view.setBackgroundColor(Color.rgb(0,0,255));
}
}
请根据您的数据类型更改方法。
我想说尽量延长CursorAdapter的与一个ListView绑定你的数据库。然后你可以重载ListView.dispatchDraw()来定制你的Paint对象。
或者,也许是有帮助的检查:Customizing Android ListView Items with Custom ArrayAdapter
它采用基于天气状况不同的图像。移植到您的问题,您可以使用9补丁或以编程方式创建Drawables作为背景,而不是在Paint中更改东西。
这也很有帮助。谢谢。 – Rben 2013-04-24 13:06:42
可能的重复[如何在ListView中正确更改特定行的背景颜色? (Android)](http://stackoverflow.com/questions/4634796/how-can-i-change-the-background-color-for-an-specific-row-properly-in-a-listview) – Simon 2013-04-23 18:20:42
我看了在那一个。但它似乎只是改变颜色,取决于视图的位置,而不是行中的数据。它给了我一个我正在努力的想法,所以它可能会导致一个解决方案,但它不是我所需要的。 – Rben 2013-04-23 20:53:36
我也在研究[更改基于属性的背景颜色](http://stackoverflow.com/questions/13967788/change-background-color-based-on-property)作为另一种可能的解决方案。只需要更多地了解适配器如何工作。 – Rben 2013-04-23 20:54:21