0

我有这个(编辑):

   public class ExpensesDataSource {
    private SQLiteDatabase database;
    private DBHelper helper;

    public ExpensesDataSource(Context context) {
        helper = new DBHelper(context);
    }

    public void open() {
        database = helper.getWritableDatabase();
    }

    public void close() {
        database.close();
    }

    public List<Expense> getAllExpenses() {
        open();
        Cursor all_cursor = database.query(EXPENSES_TABLE, new String[]{BaseColumns._ID, KEY_DESC,
                KEY_AMOUNT, KEY_DATE}, null, null, null, null, KEY_DATE);
        List<Expense> allExpenses = new ArrayList<Expense>();

        if (all_cursor.moveToFirst()) {

            while(!all_cursor.isLast()) {
                Expense expense = new Expense();
                expense.setId(all_cursor.getInt(all_cursor.getColumnIndex(BaseColumns._ID)));
                expense.setDescription(all_cursor.getString(all_cursor.getColumnIndex(KEY_DESC)));
                expense.setAmount(all_cursor.getFloat(all_cursor.getColumnIndex(KEY_AMOUNT)));
                expense.setDate(all_cursor.getInt(all_cursor.getColumnIndex(KEY_DATE)));
                allExpenses.add(expense);
                all_cursor.moveToNext();
            }
        }

        close();
        return allExpenses;
    }

    public float getTotalExpense() {
        open();
        Cursor sum_cursor = database.query(EXPENSES_TABLE, new String[]{"SUM("+KEY_AMOUNT+")"}, null,
                null, null, null, null);
        sum_cursor.moveToFirst();
        float total = sum_cursor.getFloat(0);
        sum_cursor.close();
        close();
        return total;
    }

    public long insertItem(String desc, Float amount, long date) {
        open();
        ContentValues args = new ContentValues();
        args.put(KEY_AMOUNT, amount);
        args.put(KEY_DESC, desc);
        args.put(KEY_DATE, date);

        long id = database.insert(EXPENSES_TABLE, null, args);
        close();
        return id;
    }

    public void deleteItem(long id) {
        open();
        database.delete(EXPENSES_TABLE, KEY_ID + "=" + id, null);
        close();
    }

    public void updateItem(Long rowId, String desc, Float amount, long date) {
        open();
        ContentValues args = new ContentValues();
        args.put(KEY_AMOUNT, amount);
        args.put(KEY_DESC, desc);
        args.put(KEY_DATE, date);

        database.update(EXPENSES_TABLE, args, KEY_ID + "=" + rowId, null);
        close();
    }

    public void deleteAllItems() {
        open();
        database.delete(EXPENSES_TABLE, null, null);
        close();
    }

    public void flipItem(long id) {
        open();
        Cursor amount_cursor = database.query(EXPENSES_TABLE, new String[]{KEY_AMOUNT},
                KEY_ID+"="+id, null, null, null, null);
        amount_cursor.moveToFirst();
        float amount = amount_cursor.getFloat(0);
        ContentValues args = new ContentValues();
        args.put(KEY_AMOUNT, -amount);
        database.update(EXPENSES_TABLE, args, KEY_ID + "=" + id, null);
        close();
    }

    public Expense getById(long rowId) {
        open();
        Cursor row_cursor = database.query(EXPENSES_TABLE, new String[]{BaseColumns._ID, KEY_DESC,
                KEY_AMOUNT, KEY_DATE}, KEY_ID + "=" + rowId , null, null, null, null);

        if(row_cursor.moveToFirst()) {
            Expense expense = new Expense();
            do {
                expense.setId(row_cursor.getInt(row_cursor.getColumnIndex(BaseColumns._ID)));
                expense.setDescription(row_cursor.getString(row_cursor.getColumnIndex(KEY_DESC)));
                expense.setAmount(row_cursor.getFloat(row_cursor.getColumnIndex(KEY_AMOUNT)));
                expense.setDate(row_cursor.getInt(row_cursor.getColumnIndex(KEY_DATE)));
            }  while(!row_cursor.isLast());
            close();
            return expense;
        }

        close();
        return null;
    }
}

我需要amount在我的 grafico 活动中传递值:

  public class grafico extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.piegrafico);
    PieGraph pg = (PieGraph)findViewById(R.id.piegraph);

    PieSlice slice = new PieSlice();
    slice.setColor(Color.parseColor("#99CC00"));
    slice.setValue(20);
    pg.addSlice(slice);
    slice = new PieSlice();
    slice.setColor(Color.parseColor("#FFBB33"));
    slice.setValue(10);
    pg.addSlice(slice);
    slice = new PieSlice();
    slice.setColor(Color.parseColor("#AA66CC"));
    slice.setValue(2);
    pg.addSlice(slice);     

    pg.setOnSliceClickedListener(new OnSliceClickedListener(){

        @Override
        public void onClick(int index) {

        }

    });


}

private void setUpViews() {

    Bundle extras = getIntent().getExtras();

        String desc = extras.getString(DBHelper.KEY_DESC);
        Float amount = extras.getFloat(DBHelper.KEY_AMOUNT);
        long date = extras.getLong(DBHelper.KEY_DATE);

}

}

如您所见,我已尝试编写:

private void setUpViews() {

        Bundle extras = getIntent().getExtras();

            String desc = extras.getString(DBHelper.KEY_DESC);
            Float amount = extras.getFloat(DBHelper.KEY_AMOUNT);
            long date = extras.getLong(DBHelper.KEY_DATE);

    }

但真的我不知道它是否正确..我需要amount在这里插入例如:

PieSlice slice = new PieSlice();
        slice.setColor(Color.parseColor("#99CC00"));
        slice.setValue(amount); //here

但我做不到..我该如何解决?谢谢

4

0 回答 0