Reputation: 3121
i have a problem with the spinner in a tab host:
in fact i successed to make dynamically using
Spinner spinner = new Spinner(isChild() ? getParent() : this);
but i want it defined by the XML file. like this
Spinner spinner = (Spinner) this.findViewById(R.id.widget10);
I got the problem when i use (OnItemSelectedListener())
when opening the the dialogue of selection??
please i need a help,
think u.
Upvotes: 0
Views: 2082
Reputation: 4255
Let us suppose, there are two class1 and class2
In class1 define a method
public static View makeSpinner(Context context) {
View v = LayoutInflater.from(context).inflate(R.layout.spinner, null);
Spinner spinner = (Spinner) v.findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item);
adapter.add("one");
adapter.add("two");
adapter.add("three");
spinner.setAdapter(adapter);
return v;
}
and in the class2 where u need to access the spinner define it's setContentView as below:-
setContentView(MainActivity.makeSpinner(getParent()));
Upvotes: 2
Reputation: 1
I found a different solution.
My problem was that I had a MapView in the same activity as the spinner. Because of this I couldn't use the described technique.
And actually I didn't wanted to redesign my whole project so I just replaced my spinner in the XML-File with an FrameLayout.
Now my code looks like this:
Spinner spinner = new Spinner(isChild() ? getParent() : this);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getParent(),
android.R.layout.simple_spinner_item, data);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
FrameLayout frame = (FrameLayout)findViewById(R.id.frameLayoutSpinnerTarget);
frame.addView(spinner);
Upvotes: 0