When sending Java object from an android app to the database one of the attributes goes as null

Asked

Viewed 53 times

1

Produto produto = new Produto();

produto.setNome(edtNome.getText().toString());
produto.setPreco(Double.parseDouble(edtPreco.getText().toString().replace(",", ".")));
produto.setUnidMedida(txtUnidMedida.getText().toString());

I am trying to send the product object with the above data but when sending to save Android Studio prints the following message and in the database saves name and price but the measurement drive goes as null.

Inputmethodmanagerservice: Window already focused, ignoring Focus Gain of: com.android.Internal.view.Iinputmethodclient$Stub$Proxy@5283aa8c attribute=null, token = android.os.Binderproxy@52903534

//Textview Code

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_produto);

    txtUnidMedida = (TextView) findViewById(R.id.txtUnidMedida);

            txtUnidMedida.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ad.show();
                }
            });
            //Criando o AlertDialog para a lista de unidades
            unidades = getResources().getStringArray(R.array.unidades_medida);
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle(R.string.selecione_unid_med);
            builder.setItems(unidades, this);
            builder.setNegativeButton(R.string.cancelar, null);
            ad = builder.create();
    }

    @Override
        public void onClick(DialogInterface dialog, int position) {
            String itemSelecionado = unidades[position];
            txtUnidMedida.setText(itemSelecionado);

        }
  • If you can provide more parts of your code, we could help you better!

  • 1

    Is the txtUnidMedida variable an Edittext? Before saving your data use Log. i("variable", "value"); to verify that past values are correct.

  • is a Textview with style of Edittext style="@android:style/Widget.Edittext" when clicking on it opens a Dialog to select the unit of measure and left the field as enable="false"

  • The problem will indeed be in that TextView. Put the code that opens this Dialog and which attributes the value to TextView

  • I did the following, put a Log. d to get the value of the unit of measure of the product Log object. d("NGVL", "product---"+product.getUnidMedida()); and returned the following: com.app.retrofit D/NGVL: product---Package

  • This is for you to do before saving, if you put this before saving and the printed message was "com.app.retrofit D/NGVL: product--Package", then it means that your variable is no longer null but "Package".

  • Yes, that’s the result I got, but when I call the method to insert a print in the database, I put it in the same way and it returns me in the product.getUnidMedida() null

Show 2 more comments
No answers

Browser other questions tagged

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