@Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   super.onActivityResult(requestCode, resultCode, data);
   if (resultCode == RESULT_OK) {
     switch (requestCode) {
       case NEW_ATTRIBUTE_REQUEST:
         {
           long attributeId = data.getLongExtra(AttributeColumns.ID, -1);
           if (attributeId != -1) {
             Attribute a = db.getAttribute(attributeId);
             addAttribute(a);
           }
         }
         break;
       case EDIT_ATTRIBUTE_REQUEST:
         {
           long attributeId = data.getLongExtra(AttributeColumns.ID, -1);
           if (attributeId != -1) {
             Attribute a = db.getAttribute(attributeId);
             attributeCursor.requery();
             updateAttribute(attributesLayout, a);
             updateAttribute(parentAttributesLayout, a);
           }
         }
         break;
     }
   }
 }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.category);

    types = getResources().getStringArray(R.array.attribute_types);

    scrollView = (ScrollView) findViewById(R.id.scroll);

    categoryTitle = new EditText(this);
    categoryTitle.setSingleLine();

    Intent intent = getIntent();
    if (intent != null) {
      long id = intent.getLongExtra(CATEGORY_ID_EXTRA, -1);
      if (id != -1) {
        category = db.getCategory(id);
      }
    }

    attributeCursor = db.getAllAttributes();
    startManagingCursor(attributeCursor);
    attributeAdapter =
        new SimpleCursorAdapter(
            this,
            android.R.layout.simple_spinner_dropdown_item,
            attributeCursor,
            new String[] {AttributeColumns.NAME},
            new int[] {android.R.id.text1});

    if (category.id == -1) {
      categoryCursor = db.getCategories(true);
    } else {
      categoryCursor = db.getCategoriesWithoutSubtree(category.id);
    }
    startManagingCursor(categoryCursor);

    LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
    parentCategoryText =
        x.addListNode(layout, R.id.category, R.string.parent, R.string.select_category);

    LinearLayout titleLayout = new LinearLayout(this);
    LayoutInflater layoutInflater = LayoutInflater.from(this);
    layoutInflater.inflate(R.layout.category_title, titleLayout, true);
    incomeExpenseButton = (ToggleButton) titleLayout.findViewById(R.id.toggle);
    categoryTitle = (EditText) titleLayout.findViewById(R.id.primary);
    x.addEditNode(layout, R.string.title, titleLayout);

    attributesLayout =
        (LinearLayout)
            x.addTitleNodeNoDivider(layout, R.string.attributes).findViewById(R.id.layout);
    x.addInfoNodePlus(
        attributesLayout, R.id.new_attribute, R.id.add_attribute, R.string.add_attribute);
    addAttributes();
    parentAttributesLayout =
        (LinearLayout)
            x.addTitleNodeNoDivider(layout, R.string.parent_attributes).findViewById(R.id.layout);
    addParentAttributes();

    categoryAdapter =
        new CategoryListAdapter(
            db, this, android.R.layout.simple_spinner_dropdown_item, categoryCursor);

    Button bOk = (Button) findViewById(R.id.bOK);
    bOk.setOnClickListener(
        new OnClickListener() {
          @Override
          public void onClick(View view) {
            if (checkEditText(categoryTitle, "title", true, 100)) {
              category.title = text(categoryTitle);
              setCategoryType(category);
              int count = attributesLayout.getChildCount();
              ArrayList<Attribute> attributes = new ArrayList<Attribute>(count);
              for (int i = 0; i < count; i++) {
                View v = attributesLayout.getChildAt(i);
                Object o = v.getTag();
                if (o instanceof Attribute) {
                  attributes.add((Attribute) o);
                }
              }
              long id = db.insertOrUpdate(category, attributes);
              Intent data = new Intent();
              data.putExtra(DatabaseHelper.CategoryColumns._id.name(), id);
              setResult(RESULT_OK, data);
              finish();
            }
          }
        });

    Button bCancel = (Button) findViewById(R.id.bCancel);
    bCancel.setOnClickListener(
        new OnClickListener() {
          @Override
          public void onClick(View view) {
            setResult(RESULT_CANCELED, null);
            finish();
          }
        });

    editCategory();
  }