2

如何在我的 ListView 中将低于 100 的每个值都涂成红色?

我有连接到的 ListViewmy_list.xml

我像这样连接光标:

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);
    }

怎么做 ?

4

1 回答 1

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;
          }

        }

可能有一些错误,但我认为你必须明白这一点。

于 2012-08-23T11:22:02.423 回答