Send data from EditText to a ListView in another activity

0

I would like to know a way to send a user-entered data into an edittext and send it to another activity and update a listView with already inserted data. I have a class called Edit board and another MainActivity that extends FragmentActivity. In the main class I have an arrayList that has some data already inserted into it, but when I try to send the data from editText to main and insert in this array does not work. Is there any way to do this? Main class:

public class MainActivity extends FragmentActivity {
FragmentManager fm = getSupportFragmentManager();
private EditarPrancha editarPrancha;
private ListView listItemView;
private ArrayList<String> lista = new ArrayList<String>();



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    inserirArray("INICIO");
    inserirArray("PERGUNTAS");
    inserirArray("RESPOSTAS");

    exibeListaDeCategorias();
    transitarEntreFragmentos();
}
public void inserirArray (String valor){
    lista.add(valor);
}

public ArrayList<String> retornaLista() {

    return lista;
}


//exibe lista com categorias
public void exibeListaDeCategorias(){
    listItemView = (ListView) findViewById(R.id.listView1);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_2, android.R.id.text1, retornaLista());
    listItemView.setAdapter(adapter);
    listItemView.invalidate();
}

Class Edit:

public class EditarPrancha extends FragmentActivity{

private MainActivity mainActivity;
private ListView listItemView;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_editar_prancha);
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    adicionarNovaCategoria();
    selecionaCategoriaParaAlterar();
    botaoSelecionarImagem();
}

public void adicionarNovaCategoria(){
    Button btAdicionar = (Button) findViewById(R.id.salvar);
    btAdicionar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            EditText txtCat = (EditText) findViewById(R.id.cadastrar_categoria);
            //capta o valor do txtCat

            String cat = txtCat.getText().toString();
            Toast.makeText(EditarPrancha.this, "" +cat, Toast.LENGTH_SHORT).show();

                if (cat.length() > 0) {
                txtCat.setText("");
                txtCat.findFocus();
                mainActivity.inserirArray(cat);
            } else {
                Toast.makeText(EditarPrancha.this, "Digite o nome da categoria", Toast.LENGTH_SHORT).show();
            }
        }
    });
}
    
asked by anonymous 26.11.2016 / 00:07

1 answer

1

You can not instantiate an activity as you are doing and simply assign values as you did in: mainActivity.inserirArray(cat); , Android does not work like this for security reasons. For this, you need to use an intent.

In the Edit class, do the following:

if (cat.length() > 0) {

  //Cria um intent com os parâmetros indicando de onde ele vem e para onde ele vai.
  Intent intent = new Intent(this,MainAcivity.class);

  //insere a string no intent com um nome qualquer.
  intent.putExtra("nomeQualquer",cat);

  //carrega a outra tela levando o intent junto.
  startActivity(intent);

  //todos os 3 resumidos a uma linha
  startActivity(new Intent(this,MainActivity.class).putExtra("nomeQualquer",cat));
}

On Main, within onCreate:

String cat;
Intent intent = getIntent();
if(intent.hasExtra("nomeQualquer")){
  cat = intent.getStringExtra("nomeQualquer");
}

When you do hasExtra and getStringExtra in the "AnyName", although it can be any name, it must be the same as you created in putExtra.

putExtra accepts all types of variables but not integer objects. For this you need to use a Bundle, save the object inside it and then save the Bundle in putExtra.

    
26.11.2016 / 03:05