I can’t make a setOnItemClickListenner with a Long (Android Studio)

Asked

Viewed 26 times

0

I have a Person class with the attribute "code" type Long. I have an error in this code block. Code1 is where I got the ID.

 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()); // O erro está aqui, tentei usar o Long.parseLong antes do pessoaSelecionada e não deu certo.


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

        }
    });

1 answer

0

Your problem must be in the return of your method personal Taught.getCodigo(). It is returning a long value in your case. Try to do this:

codigo1.setText(String.valueOf(pessoaSelecionada.getCodigo()));

The method setText needs to receive a string, so your code gives error. This question here from String.valueOf() should also be added in the other methods that return numbers, so that it works correctly.

  • 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?

  • @Matheus are you ever assigning a value to that attribute, even if it is zero, in your class? Because if you’re not, that could be the problem...

  • Maybe, if your question is very urgent, it is better to leave the field as String and when you need the numeral value, do the conversion to Long. Then you leave this question to be resolved when you’re calmer.

  • Did not assign any value to Long no, even because the user who will put the value, in case I have to assign anyway? It would be something like Long code = 0; ?

  • private edittext cnpj1,razao1, latitude1, longitude1,code1; So it is in my Mainactivity, as the code is caught by editText, set it along with all others.

  • p.setCodigo(Long.parseLong(code1.gettext().toString())); // I made this way to insert the data in firebase, it is inside the insertion block. What I’m having trouble with is the way and time to return this data in a listview,

Show 1 more comment

Browser other questions tagged

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