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