Send Edittext data to a Listview in another Activity

Asked

Viewed 830 times

0

I would like to know a way to send a data typed by the user in an edittext and send to another Activity and update a listview with data already entered. 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 in it, but when I try to send the data of editText to the 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();
            }
        }
    });
}

1 answer

1


You cannot 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, inside the onCreate:

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

When you do hasExtra and getStringExtra in the "nameQualquer", although it may be any name, it needs to be the same one you created in putExtra.

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

  • Mine is making an error in startActivity(new Intent(this, Mainactivity.class).putExtra("nameQualquer",cat)); /between this and Mainactivity.class don’t know why

  • 1

    It can be the "extends" of the class. If it is the main one, try extending Activity or Appcompatactivity. The Intent parameters are "where it comes from" and "where it goes", the first part of which needs to be the class you are currently in. The "where it comes from" is the current context, that is, you can pass this indicating that it is the class itself, or maybe Mainactivity.this. Try to do it separately and post the bug, if I can help I will do it.

  • startActivity(new Intent(Mainactivity2.this, Mainactivity.class).putExtra("nameQualquer",cat)); This solves the problem.

Browser other questions tagged

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