call a screen with only buttons and methods

Asked

Viewed 612 times

-1

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

On the first screen I have a list and a button include. When you click the include button, another screen has to appear to enter the name, then I click the ok button include and it goes back 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();
            }
        });
    }
}
  • https://developer.android.com/training/basics/firstapp/starting-activity.html?hl=pt-br

  • https://developer.android.com/training/basics/intents/result.html?hl=pt-br

  • Honestly I didn’t understand almost anything you explained, it was even difficult to edit by this. I suggest you edit the question, and explain better what you want to do, it’s very confusing to understand your text.,

  • kkkkkkk ai mds I don’t even know how to explain here, that’s how.. I need to make a screen that has a boot called include and a list, when you click this 'include' button it goes to another screen that has another button called 'ok include' where I will enter a name and when I click this 'ok include' button it goes back to the first screen showing the list with the new name added.

1 answer

0

let me see if I understand your doubt...

Voce does not want to create another Activity but to change the layout of the current one without needing other classes?

Anyway in Oncreate Voce did not set any action for the buttons so even Voce having created the methods the boot will do nothing(unless Voce has linked the method via xml)

If you really want to do it this way use the code below:

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();
            }
        };
    }
}

Browser other questions tagged

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