Get value of selected View in ViewSwitcher

387 Views Asked by At

I'm using a ViewSwitcher to switch between two Views (as it is supposed to be used).

Here's the ViewSwitcher code :

final ViewSwitcher viewSwitcher = new ViewSwitcher(getApplicationContext());

                TextView t = new TextView(this);
                [...]
                t.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        viewSwitcher.showNext();
                    }
                });

                viewSwitcher.addView(t,
                        new TableRow.LayoutParams(
                                TableRow.LayoutParams.MATCH_PARENT,
                                TableRow.LayoutParams.MATCH_PARENT, 1f));

                final String key = (String)map.keySet().toArray()[n];

                if(key.contains("EditText"))
                {
                    EditText e = new EditText(this);
                    [...]
                    e.setOnLongClickListener(new View.OnLongClickListener() {
                        @Override
                        public boolean onLongClick(View view) {
                            viewSwitcher.showPrevious();
                            return true;
                        }
                    });

                    viewSwitcher.addView(e,
                            new TableRow.LayoutParams(
                                    TableRow.LayoutParams.MATCH_PARENT,
                                    TableRow.LayoutParams.MATCH_PARENT, 1f));
                }
                else if (key.contains("Spinner"))
                {
                            Spinner spin = new Spinner(getApplicationContext());
                            [...]
                            spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                                @Override
                                public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                                    if(adapterView.getSelectedItemPosition() == 0)
                                        viewSwitcher.showPrevious();
                                }

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

                                }
                            });

                            viewSwitcher.addView(spin,
                                    new TableRow.LayoutParams(
                                            TableRow.LayoutParams.MATCH_PARENT,
                                            TableRow.LayoutParams.MATCH_PARENT, 1f));
                }

The fact is that I tried to put the ViewSwitcher in a List to get the values later.

(As it worked before when I was only using EditText), But it don't really work Because I can't get the values of what is written in the Views of the ViewSwitcher.

So here's my question : Do you know how to get the value of the view that is currently selected in the ViewSwitcher so I can add it to a List to use it later ?

Thank you in advance.

1

There are 1 best solutions below

4
Abhay Koradiya On BEST ANSWER

You can use getCurrentView() method to get currently displayed view.

To get Value from view, Cast the view and get text from that view.. Like,

EditText temp = (EditText) viewSwitcher.getCurrentView();
String s = temp.getText().toString();

you can check type of current view and than get property what you want.

View v = viewSwitcher.getCurrentView();
if (v instanceof EditText) {
     //code...
} else if (v instanceof Spinner) {
     //code...
}