示例#1
0
  private void getProjects() {
    projects = new ArrayList<String>();
    mProjects = new ArrayList<Project>();
    dbhelper.open();
    Cursor c = dbhelper.getAllProjects();
    if (c.moveToFirst()) {
      do {
        Project tmp = new Project();
        tmp.setCount(c.getInt(c.getColumnIndex(DbAdapter.KEY_COUNTER)));
        tmp.setRowId(c.getLong(c.getColumnIndex(DbAdapter.ROW_ID)));
        tmp.setName(c.getString(c.getColumnIndex(DbAdapter.KEY_PROJECTNAME)));
        mProjects.add(tmp);
        projects.add(tmp.getName());

      } while (c.moveToNext());
    }
    c.close();
    dbhelper.close();
  }
    @Override
    protected ArrayList<Project> doInBackground(Integer... params) {
      ArrayList<Project> list = new ArrayList<Project>();

      dbadapter.open();
      Cursor c = dbadapter.getAllProjects();
      Project tmp = new Project();
      if (c.moveToFirst()) {
        do {
          tmp = new Project();
          tmp.setRowId(c.getLong(c.getColumnIndex(DbAdapter.ROW_ID)));
          tmp.setName(c.getString(c.getColumnIndex(DbAdapter.KEY_PROJECTNAME)));
          tmp.setCount(c.getInt(c.getColumnIndex(DbAdapter.KEY_COUNTER)));

          list.add(tmp);

        } while (c.moveToNext());
      }
      c.close();
      dbadapter.close();
      return list;
    }
 private void deleteProject(Project project, int id) {
   Log.d("del", "Delete id:" + id);
   dbhelper.open();
   if (dbhelper.deleteProject(project.getRowId())) {
     Toast.makeText(ManageProjectsActivity.this, R.string.deletion_completed, Toast.LENGTH_SHORT)
         .show();
     mList.remove(id);
     mAdapter.notifyDataSetChanged();
   } else {
     Toast.makeText(ManageProjectsActivity.this, R.string.deletion_failed, Toast.LENGTH_SHORT)
         .show();
   }
   dbhelper.close();
 }
  private void editProject(final Project project, final int position) {
    final AlertDialog.Builder alert = new AlertDialog.Builder(this);
    final EditText input = new EditText(this);

    input.setText(project.getName());
    alert.setView(input);
    alert.setPositiveButton(
        "Save",
        new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int whichButton) {
            String value = input.getText().toString().trim();
            if (value.length() > 0) {
              dbhelper.open();
              if (dbhelper.updateProject(value, project.getCount(), project.getRowId())) {
                Toast.makeText(
                        ManageProjectsActivity.this, R.string.renamed_completed, Toast.LENGTH_SHORT)
                    .show();
              } else {
                Toast.makeText(
                        ManageProjectsActivity.this,
                        R.string.failed_rename_project,
                        Toast.LENGTH_SHORT)
                    .show();
              }
              Project tmp = new Project();
              tmp.setName(value);
              tmp.setRowId(project.getRowId());
              tmp.setCount(project.getCount());
              mList.set(position, tmp);
              mAdapter.notifyDataSetChanged();
              dbhelper.close();
            } else {
              Toast.makeText(ManageProjectsActivity.this, R.string.rename_value, Toast.LENGTH_SHORT)
                  .show();
            }
          }
        });

    alert.setNegativeButton(
        "Cancel",
        new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int whichButton) {
            dialog.cancel();
          }
        });
    alert.setTitle(R.string.rename);
    alert.show();
  }