Ryan Kelly
Ryan Kelly

Reputation: 11

if statement inside a onClickListener trying to get value of androidtextwidget

I am trying to convert a value that has been passed through from another fragment. The convert method is inside an onClickListener which when clicked will make the conversion of the value passed through the fragment.

The values are currently being placed into TextViews on my second fragment. However when I try to make an if statement it won't enter the loop

Text Name is what my textView has been set to.

The code is here

button10.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {


        if (textName.equals("Miles") && textName2.equals("Kilometers")) {
            String str1 = editText1.getText().toString();
            double unittoConvert = Double.parseDouble(str1);
            double convertedUnit = unittoConvert * 1.6;
            String result = Double.toString(convertedUnit);
            textName3.setText(result);

        }
    }
});

This is the code for the methods that are setting the unit selected in scroller and passing it through to the text view which is then displaying the selected unit. When i try to extract these values it wont work

PageViewModel.getName().observe(requireActivity(), new Observer<String>() {
    @Override
    public void onChanged(@Nullable String s) {
        textName.setText(s);
    }
});

PageViewModel2.getName2().observe(requireActivity(), new Observer<String>() {
    @Override
    public void onChanged(@Nullable String s) {
        textName2.setText(s);

    }

});

Upvotes: 0

Views: 56

Answers (1)

Md. Asaduzzaman
Md. Asaduzzaman

Reputation: 15433

Use getText to extract text from TextView and then compare

textName.getText().equals("Miles") && textName2.getText().equals("Kilometers")

Upvotes: 1

Related Questions