关于在mt ListView中绘制行的咨询

时间:2012-08-23 11:00:00

标签: java android

如何在ListView中以红色绘制100以下的每个值?

我有连接到my_list.xml

的ListView

我像这样连接光标:

public void update_list(String NN) {
        c = db.rawQuery(NN, null);
        startManagingCursor(c);
        String[] from = new String[]{"_id","Store","Makat","Des","Qty" };
        int[] to = new int[]{R.id.txtID, R.id.txtStore,R.id.txtMakat ,R.id.txtDes ,R.id.txtQty};
        SimpleCursorAdapter notes = new SimpleCursorAdapter (this, R.layout.my_list, c, from, to);
        setListAdapter(notes);
    }

怎么做?

1 个答案:

答案 0 :(得分:2)

我的建议是制作自己的适配器,扩展SimpleCursorAdapter。在其中,您应该覆盖创建每个行视图的getView方法。

    class MyCustomAdapter extends SimpleCursorAdapter{
          private Context context;
          private Cursor c;

          public MyCustomAdapter(Context context, int layout, Cursor c,
            String[] from, int[] to) {
            super(context, layout, c, from, to);
            this.c = c;
            this.context = context;
          }

          @Override
          public View getView(int pos, View inView, ViewGroup parent) {

                LayoutInflater inflater = (LayoutInflater) context
                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View rowView = inflater.inflate(R.layout.rowlayout, parent, false); 

                this.c.moveToPosition(pos);

                //get the value from cursor, evaluate it, and set the color accordingly
                int columnIndexOfQty = 4;
                int qty = c.getInt(columnIndexOfQty);

                if(qty < 100){
                  rowView.setBackgroundColor(Color.RED);
                }
                return rowView;
          }

        }

可能存在一些错误,但我认为您必须明白这一点。