2012-08-23 68 views
2

如何在ListView中绘制红色下100以下的每个值?有关在mt中绘画行的咨询ListView

我有一个连接到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); 
    } 

怎么办呢?

回答

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

     } 

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