Exemplo n.º 1
0
  /**
   * Novo m�todo para criar o painel din�mico para os bot�es de selec��o da turma
   *
   * @author Thiago
   */
  @SuppressLint("NewApi")
  private void makeTabela() {

    // Cria o objecto da base de dados
    LetrinhasDB db = new LetrinhasDB(this);
    // ************* Mudar este select de all para por ID de turma
    // (ALEXANDRE!!)
    List<Estudante> alunos = db.getAllStudentsByTurmaId(idTurma);
    // *******************************************************************************
    int nAlunos = alunos.size();
    int[] idAluno = new int[nAlunos];
    String nomeAluno[] = new String[nAlunos];
    String fotoAluno[] = new String[nAlunos];

    // preenche os arrays s� com a informa��o necess�ria
    for (int i = 0; i < nAlunos; i++) {
      idAluno[i] = alunos.get(i).getIdEstudante();
      nomeAluno[i] = alunos.get(i).getNome();
      fotoAluno[i] = alunos.get(i).getNomefoto();
    }

    for (Estudante cn : alunos) {
      String storage =
          cn.getNome()
              + ","
              + cn.getIdEstudante()
              + ","
              + cn.getNomefoto()
              + ","
              + cn.getEstado()
              + ","
              + cn.getIdTurma();
      Log.d("letrinhas-Alunos", storage.toString());
    }

    /** Scroll view com uma tabela de 4 colunas(max) */
    // tabela a editar
    TableLayout tabela = (TableLayout) findViewById(R.id.tblEscolheAl);
    // linha da tabela a editar
    TableRow linha = (TableRow) findViewById(R.id.escAllinha01);
    // 1� bot�o
    Button bt = (Button) findViewById(R.id.AlBtOriginal);
    bt.setText("teste alunos");

    // Contador de controlo
    int cont = 0;
    // criar o n� de linhas a dividir por 4 colunas
    for (int i = 0; i < nAlunos / 4; i++) {
      // nova linha da tabela
      TableRow linha1 = new TableRow(getBaseContext());
      // Copiar os parametros da 1� linha
      linha1.setLayoutParams(linha.getLayoutParams());
      // criar os 4 bot�es da linha
      for (int j = 0; j < 4; j++) {

        // **********************************
        // Nome, id e foto do aluno

        final String alumni = nomeAluno[cont];
        final int idAL = idAluno[cont];
        final String alunFot = fotoAluno[cont];
        // ***********************************

        // novo bot�o
        Button bt1 = new Button(bt.getContext());
        // copiar os parametros do bot�o original
        bt1.setLayoutParams(bt.getLayoutParams());

        // se o aluno tiver foto, vou busca-la
        if (fotoAluno[cont] != null) {
          String imageInSD =
              Environment.getExternalStorageDirectory().getAbsolutePath()
                  + "/School-Data/Students/"
                  + fotoAluno[cont];
          Bitmap bitmap = BitmapFactory.decodeFile(imageInSD);
          ImageView imageView = new ImageView(this);

          // ajustar o tamanho da imagem
          imageView.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 210, 200, false));
          // enviar para o bot�o
          bt1.setCompoundDrawablesWithIntrinsicBounds(null, imageView.getDrawable(), null, null);
        } else {
          // sen�o copia a imagem do bot�o original
          bt1.setCompoundDrawables(null, bt.getCompoundDrawablesRelative()[1], null, null);
        }

        // addicionar o nome
        bt1.setText(nomeAluno[cont]);
        // Defenir o que faz o bot�o ao clicar
        bt1.setOnClickListener(
            new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                // Entrar na activity
                Bundle wrap = new Bundle();
                wrap.putString("Escola", Escola);
                wrap.putInt("Escola_ID", idEscola);
                wrap.putString("Professor", Professor);
                wrap.putInt("Professor_ID", idProfessor);
                wrap.putString("Foto_professor", FotoProf);
                wrap.putString("Turma", Turma);
                wrap.putInt("Turma_ID", idTurma);
                wrap.putString("Aluno", alumni);
                wrap.putInt("Aluno_ID", idAL);
                wrap.putString("Foto_Aluno", alunFot);

                Intent it = new Intent(getApplicationContext(), EscModo.class);
                it.putExtras(wrap);

                startActivity(it);
              }
            });
        // inserir o bot�o na linha
        linha1.addView(bt1);
        // incrementar o contador de controlo
        cont++;
      }
      // inserir a linha criada
      tabela.addView(linha1);
    }

    // resto
    if (nAlunos % 4 != 0) {
      TableRow linha1 = new TableRow(getBaseContext());
      linha1.setLayoutParams(linha.getLayoutParams());
      for (int j = 0; j < nAlunos % 4; j++) {

        // **********************************
        // Nome, id e foto do aluno

        final String alumni = nomeAluno[cont];
        final int idAL = idAluno[cont];
        final String alunFot = fotoAluno[cont];
        // ***********************************

        // novo bot�o
        Button bt1 = new Button(bt.getContext());
        // copiar os parametros do bot�o original
        bt1.setLayoutParams(bt.getLayoutParams());

        // se o aluno tiver foto, vou busca-la
        if (fotoAluno[cont] != null) {
          String imageInSD =
              Environment.getExternalStorageDirectory().getAbsolutePath()
                  + "/School-Data/Students/"
                  + fotoAluno[cont];
          Bitmap bitmap = BitmapFactory.decodeFile(imageInSD);
          ImageView imageView = new ImageView(this);

          // ajustar o tamanho da imagem
          imageView.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 240, 240, false));
          // enviar para o bot�o
          bt1.setCompoundDrawablesWithIntrinsicBounds(null, imageView.getDrawable(), null, null);
        } else {
          // sen�o copia a imagem do bot�o original
          bt1.setCompoundDrawables(null, bt.getCompoundDrawablesRelative()[1], null, null);
        }

        // addicionar o nome
        bt1.setText(nomeAluno[cont]);
        // Defenir o que faz o bot�o ao clicar
        bt1.setOnClickListener(
            new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                // Entrar na activity
                Bundle wrap = new Bundle();
                wrap.putString("Escola", Escola);
                wrap.putInt("Escola_ID", idEscola);
                wrap.putString("Professor", Professor);
                wrap.putInt("Professor_ID", idProfessor);
                wrap.putString("Foto_professor", FotoProf);
                wrap.putString("Turma", Turma);
                wrap.putInt("Turma_ID", idTurma);
                wrap.putString("Aluno", alumni);
                wrap.putInt("Aluno_ID", idAL);
                wrap.putString("Foto_Aluno", alunFot);

                Intent it = new Intent(getApplicationContext(), EscModo.class);
                it.putExtras(wrap);

                startActivity(it);
              }
            });
        // inserir o bot�o na linha
        linha1.addView(bt1);
        // incrementar o contador de controlo
        cont++;
      }
      // inserir a linha criada
      tabela.addView(linha1);
    }

    // por fim escondo a 1� linha
    tabela.removeView(linha);
  }