Mann
Mann

Reputation: 606

Issue with android TextWatcher with spinners

Layout has an edit text and spinner, when anything is typed/typing in the edit text the value is changing as i wanted but the problem is when when i change the spinner value, the text wont change until i type something in edit text. Actually what i want is when spinner item is changed, i want to show the results for the present value in the edit text.

I think coz i wrote my logic in onTextChanged function, i called same logic in beforeTextChanged too, but dit doesnt change until i type something..

code i tried:

 cc.input.addTextChangedListener(new TextWatcher() {

        public void afterTextChanged(Editable s) {


        }

        public void beforeTextChanged(CharSequence s, int start,
                                      int count, int after) {

            aLogic(cc);
        }

        public void onTextChanged(CharSequence s, int start,
                                  int before, int count) {
            aLogic(cc);
        }
    });



private void aLogic(uc) {
    ........
     if (subunit.equals("aa")) {
     //something           
    }
     if (subunit.equals("aaaaaa")) {
     //something           
    }if (subunit.equals("aaa")) {
     //something           
   }
}

Upvotes: 0

Views: 2040

Answers (2)

ldd
ldd

Reputation: 460

if I understand correctly: TextView edited -> Spinner changed Element chosen in the Spinner -> TextView changed.

So you need a listener (TextWatcher) for the textview and another listener for the spinner:

yourSpinner.setOnItemSelectedListener(yourListener);

with the textview.setText(yourSelectedValue) inside.

But I would warn you about your solution: behavior you described is that of the widget AutoCompleteTextView.

Otherwise, it is just:

yourSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            YourObject o = parent.getItemAtPosition(position);
            aLogic(....);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

Upvotes: 1

hrskrs
hrskrs

Reputation: 4490

When you change spinner value, it means that you trigger OnItemSelectedListener. So you have to do your logic on it:

    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
        // Here you change your value or do whatever you want
    }

    @Override
    public void onNothingSelected(AdapterView<?> parentView) {
        // Here comes when you didnt choose anything from your spinner logic
    }

});

Upvotes: 2

Related Questions