Pessoal,
Mais uma vez peço uma força para vocês, já perdi até a conta.
Estou tentando fazer um campo Spinner dinâmico com um método Dao.
A minha dúvida é como popular os meus campos Spinners de forma dinâmica neste bloco de código.
case 5:
/***COMO POPULAR OS MEUS CAMPOS SPINNERS? */
nomeInfoTipo5.setText(infos.get(i).getNomeInfo());
linearLayout.addView(nomeInfoTipo5);
LinearLayout teste = new LinearLayout(this);
teste.setOrientation(LinearLayout.VERTICAL);
escolha[i].setId(recuperaIdInfo);
linearLayout.addView(escolha[i]);
break;
Chamar o método desta forma
opcoesInfo = coletaDao.listaDadosSpinners(String.valueOf(recuperaIdInfo));
no for está certo?
Segue meu Dao. (Método listaDadosSpinners)
package br.com.mylims.model;
import java.util.ArrayList;
import java.util.List;
import br.com.mylims.bean.Info;
import br.com.mylims.bean.InfoAmostra;
import br.com.mylims.bean.OpcoesInfo;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class ColetaDao extends SQLiteOpenHelper{
private static final String CATEGORIA = "coleta";
private static final String NOME_BANCO = "teste_usu";
private static final int VERSAO = 1;
private Info infoBean;
private OpcoesInfo opcoesInfo;
public Info getInfoBean() {
return infoBean;
}
public OpcoesInfo getOpcoesInfo() {
return opcoesInfo;
}
public void setOpcoesInfo(OpcoesInfo opcoesInfo) {
this.opcoesInfo = opcoesInfo;
}
public void setInfoBean(Info infoBean) {
this.infoBean = infoBean;
}
public ColetaDao(Context context) {
super(context, NOME_BANCO, null, VERSAO);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
public List<Info> listaColeta(String idAmostra){
try {
String[] args = new String[] {idAmostra};
List<Info> lista = new ArrayList<Info>();
Cursor c2 = getWritableDatabase().rawQuery("SELECT INFO.CDINFO, INFO.NMINFO, INFO.CDTIPODADO " +
"FROM INFO " +
"INNER JOIN AMOSTRA ON (INFOSAMOSTRA.CDAMOSTRA = AMOSTRA.CDAMOSTRA) " +
"INNER JOIN INFOSAMOSTRA ON (INFO.CDINFO = INFOSAMOSTRA.CDINFO) " +
"WHERE AMOSTRA.CDAMOSTRA = ?", args);
while(c2.moveToNext()){
setInfoBean(new Info());
getInfoBean().setCodInfo(c2.getInt(0));
getInfoBean().setNomeInfo(c2.getString(1));
getInfoBean().setCodTipoDado(c2.getInt(2));
lista.add(infoBean);
infoBean.setTotalInfo(c2.getCount());
}
c2.close();
return lista;
} catch (SQLException e) {
Log.i(CATEGORIA, "Erro SQLException TESTE: " + e);
return null;
}
}
/**REALIZA QUERY PARA POPULAR OS CAMPOS SPINNERS DINAMICAMENTE**/
public List<OpcoesInfo> listaDadosSpinners(String idInfo){
List<OpcoesInfo> listaSpinner = new ArrayList<OpcoesInfo>();
try {
String[] args = new String[]{idInfo};
Cursor c = getWritableDatabase().rawQuery("SELECT OPCOESINFO.CDOPCAO, OPCOESINFO.NMOPCAO FROM OPCOESINFO " +
"INNER JOIN INFO ON (OPCOESINFO.CDINFO = INFO.CDINFO) " +
"WHERE INFO.CDINFO = ?", args);
while(c.moveToNext()){
setOpcoesInfo(new OpcoesInfo());
getOpcoesInfo().setCodOpcao(c.getInt(0));
getOpcoesInfo().setNomeOpcao(c.getString(1));
listaSpinner.add(opcoesInfo);
}
c.close();
return listaSpinner;
} catch (SQLException e) {
Log.i(CATEGORIA, "Erro SQLException listaDadosSpinenr: " + e);
return null;
}
}
}
Meu formulário
package br.com.mylims.controller;
import java.util.List;
import br.com.mylims.bean.Info;
import br.com.mylims.bean.OpcoesInfo;
import br.com.mylims.model.ColetaDao;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.TextView;
public class TesteCadastroColeta_2 extends Activity{
private static final String CATEGORIA = "coleta";
private TextView nomeInfoTipo1;
private TextView nomeInfoTipo2;
private TextView nomeInfoTipo3;
private TextView nomeInfoTipo4;
private TextView nomeInfoTipo5;
private EditText[] editText;
private EditText[] texto;
private EditText[] numerico;
private EditText[] numericoFracionario;
private EditText[] dataHora;
private Spinner[] escolha;
private List<OpcoesInfo> opcoesInfo;
ColetaDao coletaDao = new ColetaDao(this);
public void onCreate(Bundle icicle){
super.onCreate(icicle);
Bundle extras = getIntent().getExtras();
final String idAmostra = extras.getString("idAmostra");
Log.i(CATEGORIA, "idamostra TESTE: " + idAmostra);
final List<Info> infos = coletaDao.listaColeta(idAmostra);
ScrollView s = new ScrollView(this);
s.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
final int totalDados = coletaDao.listaColeta(idAmostra).size();
editText = new EditText[totalDados];
texto = new EditText[totalDados];
numerico = new EditText[totalDados];
dataHora = new EditText[totalDados];
numericoFracionario = new EditText[totalDados];
escolha = new Spinner[totalDados];
for (int i = 0; i < totalDados; i++){
try{
int recuperaTipoDado = infos.get(i).getCodTipoDado(); /**RECUPERA O ID DO TIPODADO**/
int recuperaIdInfo = infos.get(i).getCodInfo(); /**RECUPERA O CDINFO**/
/**CHAMA O METODO PARA POPULAR OS CAMPOS SPINNERS**/
opcoesInfo = coletaDao.listaDadosSpinners(String.valueOf(recuperaIdInfo));
Log.i(CATEGORIA, "TOTAL INFO: " + opcoesInfo);
nomeInfoTipo1 = new TextView(this);
nomeInfoTipo2 = new TextView(this);
nomeInfoTipo3 = new TextView(this);
nomeInfoTipo4 = new TextView(this);
nomeInfoTipo5 = new TextView(this);
editText[i] = new EditText(this); /**CAMPO DO TIPO TEXTO QUE RECEBERA OS SEGUINTES CAMPOS (1-TEXTO, 2-NUMERICO, 3-NUMERICO FRACIONARIO, 4-DATA E HORA)**/
texto[i] = new EditText(this); /**CAMPO DO TIPO TEXTO**/
numerico[i] = new EditText(this); /**CAMPO DO TIPO NUMERICO**/
numericoFracionario[i] = new EditText(this); /**CAMPO DO TIPO NUMERICO FRACIONARIO**/
dataHora[i] = new EditText(this); /**CAMPO DO TIPO **/
escolha[i] = new Spinner(this);
texto[i].setWidth(50);
numerico[i].setWidth(50);
numericoFracionario[i].setWidth(50);
dataHora[i].setWidth(50);
switch (recuperaTipoDado) {
case 1:
nomeInfoTipo1.setText(infos.get(i).getNomeInfo());
linearLayout.addView(nomeInfoTipo1);
editText[i] = texto[i];
editText[i].setText("");
editText[i].setHeight(40);
editText[i].setInputType(android.text.InputType.TYPE_CLASS_TEXT);
editText[i].setId(recuperaIdInfo);
linearLayout.addView(editText[i]);
break;
case 2:
nomeInfoTipo2.setText(infos.get(i).getNomeInfo());
linearLayout.addView(nomeInfoTipo2);
editText[i] = numerico[i];
editText[i].setText("");
editText[i].setHeight(40);
editText[i].setInputType(android.text.InputType.TYPE_CLASS_NUMBER);
editText[i].setId(recuperaIdInfo);
linearLayout.addView(editText[i]);
break;
case 3:
nomeInfoTipo3.setText(infos.get(i).getNomeInfo());
linearLayout.addView(nomeInfoTipo3);
editText[i] = numericoFracionario[i];
editText[i].setText("");
editText[i].setHeight(40);
editText[i].setInputType(android.text.InputType.TYPE_NUMBER_FLAG_DECIMAL);
editText[i].setId(recuperaIdInfo);
linearLayout.addView(editText[i]);
break;
case 4:
nomeInfoTipo4.setText(infos.get(i).getNomeInfo());
linearLayout.addView(nomeInfoTipo4);
editText[i] = dataHora[i];
editText[i].setText("");
editText[i].setHeight(40);
editText[i].setInputType(android.text.InputType.TYPE_CLASS_DATETIME);
editText[i].setId(recuperaIdInfo);
linearLayout.addView(editText[i]);
break;
case 5:
/***COMO POPULAR OS MEUS CAMPOS SPINNERS? */
nomeInfoTipo5.setText(infos.get(i).getNomeInfo());
linearLayout.addView(nomeInfoTipo5);
escolha[i].setId(recuperaIdInfo);
linearLayout.addView(escolha[i]);
break;
} // fim swith
}catch (Exception ex) {
Log.i(CATEGORIA, "Exception ex" + ex + "FOR: " + (i));
}
} // fim for
LinearLayout linearLayout2 = new LinearLayout(this);
linearLayout2.setOrientation(LinearLayout.HORIZONTAL);
Button botaoCadastrar = new Button(this);
botaoCadastrar.setText("CADASTRAR");
linearLayout2.addView(botaoCadastrar);
Button botalVoltar = new Button(this);
botalVoltar.setText("VOLTAR");
linearLayout2.addView(botalVoltar);
/**BOTAO CADASTRAR**/
botaoCadastrar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
for (int j = 0; j <totalDados; j++)
{
int recuperaTipoDado = infos.get(j).getCodTipoDado(); /**RECUPERA O ID DO TIPODADO**/
int recuperaIdInfo = infos.get(j).getCodInfo(); /**RECUPERA O CDINFO**/
switch (recuperaTipoDado) {
case 1:
if (texto[j].getText().toString().equals("")) {
Toast.makeText(TesteCadastroColeta_2.this, "DIGITE UM TEXTO!", Toast.LENGTH_SHORT).show();
Log.i(CATEGORIA, "DIGITE UM TEXTO!");
}
break;
case 2:
if(numerico[j].getText().toString().equals("")){
Toast.makeText(TesteCadastroColeta_2.this, "DIGITE UM NÚMERO INTEIRO!", Toast.LENGTH_SHORT).show();
Log.i(CATEGORIA, "DIGITE UM NÚMERO INTEIRO!");
}
break;
case 3:
if (numericoFracionario[j].getText().toString().equals("")) {
Toast.makeText(TesteCadastroColeta_2.this, "DIGITE UM NÚMERO FRACIONÁRIO!", Toast.LENGTH_SHORT).show();
Log.i(CATEGORIA, "DIGITE UM NÚMERO FRACIONÁRIO!");
}
break;
case 4:
if (dataHora[j].getText().toString().equals("")) {
Toast.makeText(TesteCadastroColeta_2.this, "DIGITE UMA DATA!", Toast.LENGTH_SHORT).show();
Log.i(CATEGORIA, "DIGITE UMA DATA!");
}
break;
case 5:
if (escolha[j].equals("")) {
Toast.makeText(TesteCadastroColeta_2.this, "SELECIONE UMA OPÇÃO!", Toast.LENGTH_SHORT).show();
}
break;
}
Log.i(CATEGORIA, "ID INFO: " + recuperaIdInfo);
}
} catch (Exception e) {
Log.i(CATEGORIA, "Erro grave botao cadastroColeta" + e);
}
}
});
botalVoltar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//telaListaAmostra();
Log.i(CATEGORIA, "teste btn voltar");
}
});
linearLayout.addView(linearLayout2);
s.addView(linearLayout);
setContentView(s);
}
/**voltar para a tela de listagem das amostras**/
/*private void telaListaAmostra() {
Intent it = new Intent(this, CadastroUsuarioController.class);
startActivity(it);
}*/
}