【问题标题】:How to create a Spinner dynamically in android?如何在 android 中动态创建 Spinner?
【发布时间】:2011-06-29 15:53:39
【问题描述】:

我从这里 (http://developer.android.com/resources/tutorials/views/hello-tabwidget.html) 使用 HelloTabWidget 作为起点。

现在我为第一个选项卡编辑了 onCreate:

// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("tab0").setIndicator("Tab0", res.getDrawable(R.drawable.ic_tab_artists));
spec.setContent(new MyTabContentFactory(this, R.layout.tab0));
tabHost.addTab(spec);

MyTabContentFactory:

public class MyTabContentFactory implements TabContentFactory {

    private Activity parent = null;
    private int layout = -1;

    public MyTabContentFactory(Activity parent, int layout) {
        this.parent = parent;
        this.layout = layout;
    }


    @Override
    public View createTabContent(String tag) {
        View inflatedView = View.inflate(parent, layout, null);//using parent.getApplicationContext() threw a android.view.WindowManager$BadTokenException when clicking ob the Spinner.
        //initialize spinner
        CharSequence array[] = new CharSequence[4];
        for (int i = 0; i < array.length; i++) {
            array[i] = "Element "+i;
        }
        ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(parent, android.R.layout.simple_spinner_item, array);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        View view = parent.findViewById(layout);
        if(view != null) {
            ArrayList<View> touchables = view.getTouchables();
            for (View b : touchables) {
                if (b instanceof Spinner) {
                    ((Spinner) b).setAdapter(adapter);
                }
            }
        }

        return inflatedView;
    }
}

tab0.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Spinner
        android:id="@+id/entry1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:prompt="@string/brand_prompt"
        />
</RelativeLayout>

MyTabContentFactory 应该初始化 Spinner,但 createTabContent 中的视图始终为 null。为什么呢?如何找到 Spinner 以对其进行初始化?

【问题讨论】:

    标签: android spinner


    【解决方案1】:

    这一行

     View view = parent.findViewById(layout);
    

    没有任何意义,我明白你在做什么,但它就是不能那样工作。您无法从您的活动中获取视图,您必须引用膨胀的 XML 视图。

    我认为你想要做的是:

     ArrayList<View> touchables = inflatedView.getTouchables();
            for (View b : touchables) {
                if (b instanceof Spinner) {
                    ((Spinner) b).setAdapter(adapter);
                }
            }
    

    但是你甚至不需要这样做,你应该这样做:

     Spinner spinner = (Spinner) inflatedView.findViewById(R.id.entry1);
     spinner.setAdapter(adapter);
    

    【讨论】:

    • 谢谢。我明天试试。顺便提一句。你的第二种方法对我不起作用。我不知道视图中有多少个 Spinner。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-28
    • 2015-11-08
    • 2019-11-25
    • 2018-09-17
    • 2016-06-23
    相关资源
    最近更新 更多