call a screen with only buttons and methods

-1

I need to create a screen that calls another one, but I want to do this using only methods, and in the code I made, it is not calling the other one.

On the first screen I have a list and a include button. When you click on the include button, you have to see another screen to enter the name, then I click on the ok button to include it and it returns to the first screen with the lists showing the new contact adds.

package com.example.pamelaelias.contatos;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends Activity {
    ListView lista;
    List lista2 = new ArrayList();
    Button btnIncluir, btnOkIncluir;
    EditText txtNome;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lista = (ListView) findViewById(R.id.idLista);
        lista2.add("Pamela");
        lista2.add("Brenda");
        lista2.add("Gabriel");
        final ArrayAdapter<String> adp = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lista2);
        lista.setAdapter(adp);
    }
    public void TelaInicial() {
        btnIncluir.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setContentView(R.layout.incluir);
                btnOkIncluir = (Button) findViewById(R.id.btnOkIncluir);
                txtNome = (EditText) findViewById(R.id.txtNome);
                Incluir();
            }
        });
    }

    public void Incluir() {
         final ArrayAdapter<String> st = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, lista2);
        btnOkIncluir.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setContentView(R.layout.activity_main);
                btnIncluir = (Button) findViewById(R.id.btnIncluir);
                lista = (ListView) findViewById(R.id.idLista);
                lista2.add(txtNome.getText());
                lista.setAdapter(st);
                TelaInicial();
            }
        });
    }
}
    
asked by anonymous 10.05.2017 / 02:27

1 answer

0

Let me see if I can understand your doubts ...

You do not want to create another activity but to change the layout of the current one without needing other classes?

Anyway in OnCreate you have not set any action for the buttons so even you have created the methods the button will not do anything (unless you have linked the method via xml)

If you really want to do this, use the following code:

package com.example.pamelaelias.contatos;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;


public class MainActivity extends Activity {
    ListView lista;
    List lista2;
    Button btnIncluir;
    Button btnOkIncluir;
    EditText txtNome;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        lista2 = new ArrayList();
        lista2.add("Pamela");
        lista2.add("Brenda");
        lista2.add("Gabriel");
        carregaPrimeiraTela();
    }

    private void carregaPrimeiraTela(){
        setContentView(R.layout.activity_main);
        btnIncluir = (Button) findViewById(R.id.btnIncluir);
        btnIncluir.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                carregaSegundaTela();
            }
        });
        atualisaLista();
    }

    private void carregaSegundaTela(){
        setContentView(R.layout.incluir);
        btnOkIncluir = (Button) findViewById(R.id.btnOkIncluir);
        btnOkIncluir.setOnClickListener(acaoBtnOkIncluir());
        txtNome = (EditText) findViewById(R.id.txtNome);
    }

    private void atualisaLista() {
        lista = (ListView) findViewById(R.id.idLista);
        ArrayAdapter adp = new ArrayAdapter(this, android.R.layout.simple_list_item_1, lista2);
        lista.setAdapter(adp);
    }

    public View.OnClickListener acaoBtnOkIncluir() {
        return new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setContentView(R.layout.activity_main);
                btnIncluir = (Button) findViewById(R.id.btnIncluir);
                lista2.add(txtNome.getText());
                atualisaLista();
                carregaPrimeiraTela();
            }
        };
    }
}
    
11.05.2017 / 02:49