How do I take a Long value in an Edittext and insert it into Firebase?(Android Studio)

Asked

Viewed 29 times

-1

I need to take a long value and insert it into Firebase and then show it in a Listview. I created a Pessoa class and with the other attributes I was able to insert and show it in a good one. Only the Long attribute I can’t, it’s a mistake. I am receiving the values by Plain Text(Edittext) My class Person is like this:

`public class Pessoa {
private String latitude,longitude;
private String uid;
private String cnpj;
private String razao;
private Long  codigo;

public Pessoa() {
}...



public Long getCodigo() {

    return codigo;
}

public void setCodigo(Long codigo) {
    this.codigo = codigo;
}

@Override
public String toString() {
    return
            "Código Cliente:" +codigo +"\n"+
            "CNPJ:" +cnpj +"\n"+
            "Razão Social:" +razao +"\n"+
            "Latitude:" +latitude +"\n"+
            "Longitude:" +longitude +"\n"

    ;
}

} ` Here’s my Mainactivity, you can see I’m trying to get the Long id and put it in Firebase and the error already appears on the line where I insert Long:

The code follows to return in a Listview but it is not necessary to show the rest, the same error is already present in that part of the code

public class MainActivity2 extends AppCompatActivity {
private EditText cnpj1,razao1, latitude1, longitude1,codigo1;
private ListView list;
private Button botao2;
private DatabaseReference referencia = FirebaseDatabase.getInstance().getReference();

private List<Pessoa> listPessoa = new ArrayList<Pessoa>();
private ArrayAdapter<Pessoa> arrayAdapterPessoa;
Pessoa pessoaSelecionada;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    codigo1 = findViewById(R.id.CodigoCliente);
    cnpj1 = findViewById(R.id.CnpjCliente);
    razao1 = findViewById(R.id.RazaoCliente);
    latitude1 = findViewById(R.id.LatitudeCliente);
    longitude1 = findViewById(R.id.LongitutudeCliente);
    botao2 = findViewById(R.id.buttonCliente);
    list = findViewById(R.id.ListaCliente);



    botao2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Pessoa p = new Pessoa();
            p.setUid(UUID.randomUUID().toString());

            p.setCnpj(cnpj1.getText().toString());
            p.setRazao(razao1.getText().toString());
            p.setLatitude(latitude1.getText().toString());
            p.setLongitude(longitude1.getText().toString());
            p.setCodigo(Long.parseLong(codigo1.getText().toString()));




            referencia.child("Cliente").child(p.getUid()).setValue(p);











        }
    });
    eventoDatabase1();
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            pessoaSelecionada = (Pessoa) parent.getItemAtPosition(position);


            codigo1.setText(pessoaSelecionada.getCodigo()));// Aqui é onde dá o erro, desse jeito fica a linha em vermelho, se coloco o toString, o app para.

            cnpj1.setText(pessoaSelecionada.getCnpj());
            razao1.setText(pessoaSelecionada.getRazao());
            latitude1.setText(pessoaSelecionada.getLatitude());
            longitude1.setText(pessoaSelecionada.getLongitude());

        }
    });


}
private void eventoDatabase1(){
    referencia.child("Cliente").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            listPessoa.clear();
            for(DataSnapshot objSnapshot:dataSnapshot.getChildren()){
                Pessoa c = objSnapshot.getValue(Pessoa.class);
                listPessoa.add(c);

            }
            arrayAdapterPessoa = new ArrayAdapter<Pessoa>(MainActivity2.this, android.R.layout.simple_list_item_1, listPessoa);
            list.setAdapter(arrayAdapterPessoa);
        }

        @Override
        public void onCancelled(DatabaseError error) {

        }
    });
}
public boolean onCreateOptionsMenu(Menu menu){
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item){
    int id = item.getItemId();
    if(id== R.id.menu_novo){
        Pessoa v = new Pessoa();
        v.setCodigo(Long.parseLong(codigo1.getText().toString()));

        v.setCnpj(cnpj1.getText().toString());
        v.setLatitude(latitude1.getText().toString());
        v.setRazao(razao1.getText().toString());
        v.setLongitude(longitude1.getText().toString());
        v.setUid(UUID.randomUUID().toString());
        referencia.child("Cliente").child(v.getUid()).setValue(v);
        limparcampos();
    }else if(id == R.id.menu_atualiza){
        Pessoa v = new Pessoa();
        v.setUid(pessoaSelecionada.getUid());
        v.setCnpj(cnpj1.getText().toString());
        v.setRazao(razao1.getText().toString());
        v.setLatitude(latitude1.getText().toString());
        v.setLongitude(longitude1.getText().toString());
        v.setCodigo(Long.parseLong(codigo1.getText().toString().trim()));
        referencia.child("Cliente").child(v.getUid()).setValue(v);
        limparcampos();
    }else if( id == R.id.menu_deleta){
        Pessoa v = new Pessoa();
        v.setUid(pessoaSelecionada.getUid());
        referencia.child("Cliente").child(v.getUid()).removeValue();
        limparcampos();
    }
    return true;
}

public void limparcampos(){
    codigo1.setText("");
    cnpj1.setText("");
    razao1.setText("");

    latitude1.setText("");
    longitude1.setText("");

}

1 answer

0

Let’s look at the method setCodigo:

public void setCodigo(Long codigo) {
    this.codigo = codigo;
}

See that he gets one Long as a parameter.

Let’s look how you’re calling the method setCodigo:

p.setCodigo(codigo1.getText().toString());

You are calling the method toString(), so you’re passing a String for a method that receives a Long, error. To correct the error, just use the method Long.parseLong:

p.setCodigo(Long.parseLong(codigo1.getText().toString()));
  • That part worked! I have a problem in the same Long in another block now, I tried Long.parseLong and it didn’t work. below the block: list.setOnItemClickListener(new Adapterview.Onitemclicklistener() { @Override public void onItemClick(Adapterview<? > Parent, View view, int position, long id) {&#xA; pessoaSelecionada = (Pessoa) parent.getItemAtPosition(position);&#xA; &#xA; codigo1.setText(pessoaSelecionada.getCodigo()); // o erro está aqui&#xA; &#xA; cnpj1.setText(pessoaSelecionada.getCnpj());

  • code1.setText(peoplesSelected.getCodigo(). toString());

  • I put this code in, compiled it without a hitch, when I open the Activity that this code is, it just closes. As if the code is with some bug. Oh when I change the Long attribute in the Person class to String and do the same as the others, it works. Only when I switch to Long does this problem happen. There would be something else that can be done?

  • It’s hard to say, maybe there might be a Nullpointerexception occurring, like some person’s code not being defined, for example. You can try to edit your question including the relevant code if it is not too large. Otherwise, you can use a debugger.

  • I included the code of my entire Mainactivity, put marked with // the line I think is giving the error.

  • I can’t see a possible cause without the stacktrace gets a little complicated. You can add that plugin to your application that it will show on a new screen the error caused. For now, a possible solution: try instead of code1.setText(personSelected.getCodigo().toString()) put code1.setText(Objects.toString(personSelected.getCodigo())) and add the line to the top of your file import java.util.Objects;.

  • Okay, I appreciate the attention and the help. Thanks

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.