打开新的内容视图

时间:2014-03-10 14:43:47

标签: java android

在我的Main.class我创建一个动态ScrollView并使用setContentView(sv)启动它会显示SpinnerButton

用户从Spinner中选择一个选项,然后点击Button。我已将setOnClickListener设置为Button

onClick方法中,我尝试启动新的ContentView。使用与Main.class相同类型的布局调用新的类文件。不知道怎么做这个。我在finish()方法中添加了onClick,它关闭了原始窗口,但不确定如何打开新窗口。

以下是Main.class代码:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.messages = new Messages(this);
    this.datasource = new FacilitiesDataSource(this);
    this.datasource.open();

    ScrollView sv = new ScrollView(this);
    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);
    sv.addView(ll);     

    Spinner s = new Spinner(this);
    s.setId(SPINNER_FACILITIES);
    s.setLayoutParams(new Spinner.LayoutParams(-2,-1));
    final List<SpinnerObject> list = this.datasource.getFacilitiesList();
    final ArrayAdapter<SpinnerObject> adapter = new ArrayAdapter<SpinnerObject>(this, android.R.layout.simple_spinner_dropdown_item, list);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    s.setAdapter(adapter);
    ll.addView(s);

    Button b = new Button(this);
    b.setText("Submit");
    b.setLayoutParams(new LayoutParams(-2,-1));
    b.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0) {
            Spinner spin = (Spinner)findViewById(SPINNER_FACILITIES);
            Log.v("option picked",  Integer.toString(( (SpinnerObject) spin.getSelectedItem () ).getId () ));
            /*
             * NEED HELP HERE 
             * NEED HELP HERE 
             * NEED HELP HERE 
             */
            //finish();
        }

    });
    ll.addView(b);
    setContentView(sv);
}

这是我的Vehicles.class

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.v("Started onCreate", "Vehicle");
    ScrollView sv = new ScrollView(this);
    setContentView(sv);
}

1 个答案:

答案 0 :(得分:2)

我认为您的意思是要开始新的Activity(从MainVehicles)。在这种情况下,您可以使用以下代码:

Intent intent = new Intent(Main.this, Vehicle.class);
startActivity(intent);

确保在Activity标记内的Android Manifest文件中声明所有<application>类,如下所示:

<activity android:name=".Vehicles" />