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();
}
}
});
}
Mine is making an error in startActivity(new Intent(this, Mainactivity.class).putExtra("nameQualquer",cat)); /between this and Mainactivity.class don’t know why
– Aline
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.
– Cleiton Oliveira
startActivity(new Intent(Mainactivity2.this, Mainactivity.class).putExtra("nameQualquer",cat)); This solves the problem.
– Aline