I can’t see Listview

Asked

Viewed 49 times

1

Primeiro codigo

#
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        Button botaoespecialidades = (Button) findViewById(R.id.botaoespecialidades);

        botaoespecialidades.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {

                CarregaTela2();
            }
        });
    }

    public void CarregaTela2() {
        setContentView(R.layout.activity_especialidades);

    }
}
#

Segunda Tela

public class Especialidades extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_especialidades);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

    String[] atividades = new String[]{"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();

    }
}

1 answer

3


In the method CarregaTela2() should create a Intent to launch Activity Specialties.
What you’re doing is just changing the layout which Mainactivity presents.

Change the method like this:

public void CarregaTela2() {
    intent = new Intent(this, Especialidades.class);
    startActivity(intent);
}
  • Thanks for the ramaral help, it worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.