public UserManagement() {
   this.dbUsers = DBHelper.getDB().getCollection(DBHelper.USERS);
 }
Beispiel #2
0
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.category_layout);

    db = new DBHelper(this);
    category_db = new Category(db.getDB());

    add_btn = (Button) findViewById(R.id.btn_add_category);
    delete_btn = (Button) findViewById(R.id.btn_delete_category);

    add_btn.setText("Add Category");
    delete_btn.setText("Delete");

    cursor = category_db.getAllLabelsCursor();

    adapter =
        new SimpleCursorAdapter(
            this,
            android.R.layout.simple_list_item_checked,
            cursor,
            new String[] {
              Category.T.COL_NAME
            }, // "name" is the column in your database that I describe below
            new int[] {android.R.id.text1});

    listview = (ListView) findViewById(R.id.categories_list);
    listview.setAdapter(adapter);
    listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

    delete_btn.setOnClickListener(
        new View.OnClickListener() {
          public void onClick(View v) {
            long[] checkedIds = listview.getCheckedItemIds();
            for (long id : checkedIds) category_db.delete(id);
            listview.clearChoices();
            adapter.changeCursor(category_db.getAllLabelsCursor());
          }
        });

    add_btn.setOnClickListener(
        new View.OnClickListener() {
          public void onClick(View v) {
            // Preparing views
            LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
            View layout = inflater.inflate(R.layout.category_dialog_layout, null);

            // layout_root should be the name of the "top-level" layout node in the
            // dialog_layout.xml file.
            final EditText category_name = (EditText) layout.findViewById(R.id.category_name);
            category_name.setHint("Name");
            final Spinner category_method = (Spinner) layout.findViewById(R.id.category_method);
            category_method.setPrompt("Method");
            final Spinner category_parent = (Spinner) layout.findViewById(R.id.category_parent);
            category_parent.setPrompt("Parent");

            List<String> methodList = new ArrayList<String>();
            methodList.add("Expense");
            methodList.add("Income");
            ArrayAdapter<String> methodAdapter =
                new ArrayAdapter<String>(
                    CategoryActivity.this, android.R.layout.simple_spinner_item, methodList);
            methodAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            category_method.setAdapter(methodAdapter);

            // build category list
            List<String> categoryList = category_db.getAllLabelsRoot(Category.EXPENSE, 0);
            ArrayAdapter<String> categoryAdapter =
                new ArrayAdapter<String>(
                    CategoryActivity.this, android.R.layout.simple_spinner_item, categoryList);
            categoryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            category_parent.setAdapter(categoryAdapter);

            category_method.setOnItemSelectedListener(
                new AdapterView.OnItemSelectedListener() {
                  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                    // build sub category list
                    List<String> subcategoryList2;
                    int method = Category.EXPENSE;
                    if (parent.getItemAtPosition(pos).toString() == "Income") {
                      method = Category.INCOME;
                    }
                    subcategoryList2 = category_db.getAllLabelsRoot(method, 0);
                    ArrayAdapter<String> subcategoryAdapter2 =
                        new ArrayAdapter<String>(
                            CategoryActivity.this,
                            android.R.layout.simple_spinner_item,
                            subcategoryList2);
                    subcategoryAdapter2.setDropDownViewResource(
                        android.R.layout.simple_spinner_dropdown_item);
                    category_parent.setAdapter(subcategoryAdapter2);
                  }

                  public void onNothingSelected(AdapterView<?> parent) {
                    // Do nothing, just another required interface callback
                  }
                });

            // Building dialog
            AlertDialog.Builder builder = new AlertDialog.Builder(CategoryActivity.this);
            builder.setView(layout);
            builder.setPositiveButton(
                "Save",
                new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog, int which) {
                    String name = category_name.getText().toString();
                    int method = Category.EXPENSE;
                    if (category_method.getSelectedItem().toString() == "Income") {
                      method = Category.INCOME;
                    }
                    String c = category_parent.getSelectedItem().toString();
                    int parent = 0;
                    if (c != "Root") {
                      parent =
                          category_db.getCategoryIDFromName(
                              category_parent.getSelectedItem().toString());
                    }
                    long id;
                    id = category_db.add(name, method, parent);
                    if (id > 0) {
                      Toast.makeText(
                              CategoryActivity.this,
                              "The Category Has Been Saved",
                              Toast.LENGTH_LONG)
                          .show();
                      Intent i = new Intent(getApplicationContext(), CategoryActivity.class);
                      startActivity(i);
                    } else {
                      Toast.makeText(
                              CategoryActivity.this,
                              "The Category Hasn't Been Saved, Please Try Again",
                              Toast.LENGTH_LONG)
                          .show();
                    }
                    dialog.dismiss();
                    // save info where you want it
                  }
                });
            builder.setNegativeButton(
                "Cancel",
                new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                  }
                });
            // AlertDialog dialog = builder.create();
            builder.show();
          }
        });

    this.home();
  }