1

我有一个从 sqlite 数据库填充的列表,其数据值范围从 -1 到 1。有没有办法将负项的背景颜色设置为红色,将正项设置为绿色?通过在项目视图中添加一个绿色向上箭头表示正值和一个红色向下箭头图像表示负值,可以产生类似的效果。另一个示例可能是一个待办事项列表,其中包含按优先级值进行颜色编码的项目。在 ios 中,这将发生在 cellforrowat indexpath 中,而在 windows phone/silverlight 中,这将通过将依赖属性绑定到数据来完成。这可以通过Android实现吗?

4

3 回答 3

1

这在 Android 上很容易做到。每当列表出现时,每个列表项都会更新,ListAdapter 中有一个 getView 方法,您可以在其中设置列表项的值。

因此,您将有一个 list_item.xml 布局,仅用于列表项。在自定义 ListAdapter 中,您可以在 getView() 方法中设置背景颜色,您可以在其中设置膨胀(视图)list_item。设置背景()。

检查这些链接: http ://techdroid.kbeanie.com/2009/07/custom-listview-for-android.html http://saigeethamn.blogspot.com/2010/04/custom-listview-android-developer。 html

于 2012-04-14T06:23:39.557 回答
1

像这样尝试..在getView方法中

   @Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.label);
     ImageView imageview= (ImageView) rowView.findViewById(R.id.img);
    textView.setText(values[position]);
    String s = values[position];
    if (s.startsWith("-")) {
        imageView.setImageResource(R.drawable.uparrow);
   //or set background colour of those views here..
    } else {
        imageView.setImageResource(R.drawable.downarrow);
    }


    return rowView;
}
于 2012-04-14T06:24:40.457 回答
0

在适配器中的 getview 方法中,您可以检查该值并在创建每个项目时以编程方式更改其颜色或可绘制对象。

于 2012-04-14T06:23:36.023 回答