开发者

Spinner within a tab

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.findView开发者_StackOverflow中文版ById(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.


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())); 


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜