I can not see the ListView

1

#
importandroid.os.Bundle;importandroid.support.v7.app.AppCompatActivity;importandroid.support.v7.widget.Toolbar;importandroid.view.View;importandroid.widget.Button;publicclassMainActivityextendsAppCompatActivity{@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Toolbartoolbar=(Toolbar)findViewById(R.id.toolbar);setSupportActionBar(toolbar);Buttonbotaoespecialidades=(Button)findViewById(R.id.botaoespecialidades);botaoespecialidades.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewarg0){CarregaTela2();}});}publicvoidCarregaTela2(){setContentView(R.layout.activity_especialidades);}}
#

publicclassEspecialidadesextendsAppCompatActivity{@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_especialidades);Toolbartoolbar=(Toolbar)findViewById(R.id.toolbar);setSupportActionBar(toolbar);String[]atividades=newString[]{"Especialidades 1", "Especialidades 2", "Especialidades 3"};

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, atividades);

    ListView lv = (ListView) findViewById(R.id.lv);
    lv.setAdapter(adapter);
    lv.setOnItemClickListener(chamaAtividades(this));
}

    public AdapterView.OnItemClickListener chamaAtividades(final Context context){
        return (new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> av, View v, int position, long id) {

                Intent intent;
                switch(position){
                    case 0:
                        intent = new Intent(context, Atividade1.class);
                        startActivity(intent);
                        break;
                    case 1:
                        intent = new Intent(context, Atividade2.class);
                        startActivity(intent);
                        break;
                }


            }
        });
    }

    public void  btsair(View view){
        finish();

    }
}
    
asked by anonymous 22.10.2015 / 01:46

1 answer

3

In the CarregaTela2() method, you must create an Intent to launch the Activity Specialties. What you're doing is just changing the layout that MainActivity presents.

Change method like this:

public void CarregaTela2() {
    intent = new Intent(this, Especialidades.class);
    startActivity(intent);
}
    
22.10.2015 / 10:35