I have two problems with this program ... the first is that when registering it does not enter the first loop of msm being td right ... it jumps and displays the message "no data", the second problem is that dps of clicking the register button it inserts the data in the DB but does not display them in the listView ... I reviewed the code I made some changes and could not solve.
Thank you in advance for the help ...
// User Class
public class Usuarios {
private String nome;
private String Data;
private String Rg;
private String Cpf;
private String Endereco;
private String Doenca;
private String Profissao;
public void Usuarios(){
}
public String getCpf() {
return Cpf;
}
public void setCpf(String cpf) {
Cpf = cpf;
}
public String getData() {
return Data;
}
public void setData(String data) {
Data = data;
}
public String getDoenca() {
return Doenca;
}
public void setDoenca(String doenca) {
Doenca = doenca;
}
public String getEndereco() {
return Endereco;
}
public void setEndereco(String endereco) {
Endereco = endereco;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getProfissao() {
return Profissao;
}
public void setProfissao(String profissao) {
Profissao = profissao;
}
public String getRg() {
return Rg;
}
public void setRg(String rg) {
Rg = rg;
}
}
// BD Firebase class
public class FireHelper { DatabaseReference bd; boolean saved; ArrayList users = new ArrayList
public FireHelper(DatabaseReference bd) {
this.bd = bd;
}
public Boolean save(Usuarios usuarios){
if(usuarios == null){
saved = true;
}
else
{
try{
bd.child("Usuarios").push().setValue(usuarios);
}catch (DatabaseException e){
e.printStackTrace();
saved = true;
}
}
return saved;
}
private void fetchData(DataSnapshot dataSnapshot){
usuarioss.clear();
for(DataSnapshot ds : dataSnapshot.getChildren()){
Usuarios usuarios = ds.getValue(Usuarios.class);
usuarioss.add(usuarios);
}
}
public ArrayList<Usuarios> retorno(){
bd.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot);
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return usuarioss;
}
}
// Main class where you will display the data
public class MainActivity extends AppCompatActivity {
DatabaseReference bd;
FireHelper helper;
AdapterCustom adapter;
ListView lv;
EditText textViewNome, textViewData, textViewRg, textViewCpf, textViewEndereco, textViewDoenca, textViewProfissao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Banco de dados
bd = FirebaseDatabase.getInstance().getReference();
helper = new FireHelper(bd);
//layout
lv = (ListView) findViewById(R.id.lv);
//Adaptador
adapter = new AdapterCustom(this, helper.retorno());
lv.setAdapter(adapter);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
displayInputDialog();
}
});
}
private void displayInputDialog() {
Dialog d = new Dialog(this);
d.setTitle("Salvar no banco de dados");
d.setContentView(R.layout.input_dialog);
//Declarando objetos
final TextView textViewNome = (TextView) d.findViewById(R.id.editTextNome);
final TextView textViewData = (TextView) d.findViewById(R.id.editTextData);
final TextView textViewRg = (TextView) d.findViewById(R.id.editTextRg);
final TextView textViewCpf = (TextView) d.findViewById(R.id.editTextCpf);
final TextView textViewEndereco = (TextView) d.findViewById(R.id.editTextEndereco);
final TextView textViewDoenca = (TextView) d.findViewById(R.id.editTextDoenca);
final TextView textViewProfissao = (TextView) d.findViewById(R.id.editTextProfissao);
Button saveButton = (Button) d.findViewById(R.id.savebutton);
//Salvar...
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String nome = textViewNome.getText().toString();
String data = textViewData.getText().toString();
String rg = textViewRg.getText().toString();
String cpf = textViewCpf.getText().toString();
String endereco = textViewEndereco.getText().toString();
String doenca = textViewDoenca.getText().toString();
String profissao = textViewProfissao.getText().toString();
Usuarios u = new Usuarios();
u.setNome(nome);
u.setData(data);
u.setRg(rg);
u.setCpf(cpf);
u.setEndereco(endereco);
u.setDoenca(doenca);
u.setProfissao(profissao);
if (nome != null && nome.length() > 0) {
if (helper.save(u)) {
textViewNome.setText("");
textViewData.setText("");
textViewRg.setText("");
textViewCpf.setText("");
textViewEndereco.setText("");
textViewDoenca.setText("");
textViewProfissao.setText("");
adapter = new AdapterCustom(MainActivity.this, helper.retorno());
lv.setAdapter(adapter);
} else {
Toast.makeText(MainActivity.this, "sem dados", Toast.LENGTH_SHORT).show();
}
}
}
});
d.show();
}
}
// Adapter Class
public class AdapterCustom extends BaseAdapter {
Context c;
ArrayList<Usuarios> usuarioss;
public AdapterCustom(Context c, ArrayList<Usuarios> usuarioss) {
this.c = c;
this.usuarioss = usuarioss;
}
@Override
public int getCount() {
return usuarioss.size();
}
@Override
public Object getItem(int position) {
return usuarioss.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView== null){
convertView = LayoutInflater.from(c).inflate(R.layout.model,parent,false);
}
//Declarando objetos
TextView textViewNome = (TextView)convertView.findViewById(R.id.textViewNome);
TextView textViewData = (TextView)convertView.findViewById(R.id.textViewData);
TextView textViewRg = (TextView)convertView.findViewById(R.id.textViewRg);
TextView textViewCpf = (TextView)convertView.findViewById(R.id.textViewCpf);
TextView textViewEndereco = (TextView)convertView.findViewById(R.id.textViewEndereco);
TextView textViewDoenca = (TextView)convertView.findViewById(R.id.textViewDoenca);
TextView textViewProfissao = (TextView)convertView.findViewById(R.id.textViewProfissao);
final Usuarios u = (Usuarios) this.getItem(position);
textViewNome.setText(u.getNome());
textViewData.setText(u.getData());
textViewRg.setText(u.getRg());
textViewCpf.setText(u.getCpf());
textViewEndereco.setText(u.getEndereco());
textViewDoenca.setText(u.getDoenca());
textViewProfissao.setText(u.getProfissao());
//Função onclick
convertView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(c, u.getNome(), Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
}