需要帮助将listView中的项目格式化为货币

时间:2012-06-14 19:51:56

标签: android listview currency

我正在编写我的第一个重要的Android应用程序,需要一些帮助将货币格式应用于listView中的某些textViews。我想将CURRENT_VALUE和TOTAL_VALUE字段格式化为货币。我已经完成了一些关于ViewBinde的阅读,我怀疑这是我需要使用的,但似乎无法理解它与我已经拥有的代码之间的关系。任何帮助将不胜感激。

    private void fillData() {

    DBAdapter db = new DBAdapter(this);
        db.open();
    Cursor c = db.getAllInventory();
        startManagingCursor(c);

        String[] from = new String[] { db.DESCRIPTION ,db.QUANTITY,db.CURRENT_VALUE, db.TOTAL_VALUE};
        int[] to = new int[] { R.id.text1,R.id.text2,R.id.text3,R.id.text4 };


        SpecialCursorAdapter invItems =
            new SpecialCursorAdapter(this, R.layout.inv_item, c, from,to);
        setListAdapter(invItems);
    }


public class SpecialCursorAdapter extends SimpleCursorAdapter 

{
    private int[] colors = new int[] { 0xFFD0D1D2, 0xFFFFFFFF };

    public SpecialCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to)
    {
        super(context,layout, c, from, to);




    }
   @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      View view = super.getView(position, convertView, parent);
      int colorPos = position % colors.length;
      view.setBackgroundColor(colors[colorPos]);
      return view;

    }

}

1 个答案:

答案 0 :(得分:1)

我在我的getView方法中使用DecimalFormat类,如下所示:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null)
        convertView = View.inflate(context, layout, null);
    View row = convertView;
    c.moveToPosition(position);

    TextView fourth = (TextView) convertView.findViewById(R.id.ListItem4);

    DecimalFormat df = new DecimalFormat("0.00");
    Double total = Double.valueOf(c.getString(3));

    fourth.setText("$" + df.format(total));
    fourth.setTextColor(MyApplication.shoplistitem_desc);

    return row;
}