Dinis Martins
Dinis Martins

Reputation: 3

How can I get a string to an array list from other activity?

I'm making a "To-do" type app, and I'm trying to make the user write the To-do task in a diferent activity then the main one. The problem is that I'm really new to android and can't seem to find the error. The app lauches, makes a toast (that I wrote to confirm the addition of a new task) and closes.

MAIN ACTIVITY CODE:

public class MainActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

private Button addBtn;
private ListView listaTarefas;

private ArrayList<String> tarefas;                      // Onde se guardam as tarefas enquanto a app está aberta
private ArrayAdapter<String> adapter;                   // Uma ferramenta que ajuda o android a prencher a lista

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

    addBtn = findViewById(R.id.addBtn);
    listaTarefas = findViewById(R.id.listaTarefas);

    tarefas = FileHelper.lerData(this);

    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, tarefas);
    listaTarefas.setAdapter(adapter);

    addBtn.setOnClickListener(this);

    listaTarefas.setOnItemClickListener(this);

    String tarefa = getIntent().getStringExtra("tarefa_key");
    adapter.add(tarefa);

    FileHelper.escreveData(tarefas, this);

    Toast.makeText(this, "Tarefa Adicionada", Toast.LENGTH_SHORT).show();

}

@Override
public void onClick(View view) {
    openAdd();
}


private void openAdd() {
    Intent intent = new Intent(this, addActivity.class);
    startActivity(intent);
}

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int posicao, long l) {
    tarefas.remove(posicao);
    adapter.notifyDataSetChanged();
    FileHelper.escreveData(tarefas, this);
    Toast.makeText(this, "Tarefa Eliminada", Toast.LENGTH_SHORT).show();
}

}

SECOND ACTIVITY CODE:

public class addActivity extends AppCompatActivity {

private EditText tarefasEdit;
private Button backBtn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add2);

    tarefasEdit = findViewById(R.id.tarefasEdit);
    backBtn = findViewById(R.id.backBtn);

    backBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.backBtn:
                Intent intent = new Intent(addActivity.this, MainActivity.class);
                String tarefa = tarefasEdit.getText().toString();
                intent.putExtra("tarefa_key", tarefa);
                startActivity(intent);
                tarefasEdit.setText("");
                break;
            }
        }
    });

}

}

FILEHELPER CODE TO SAVE DATA:

public class FileHelper {
public static final String FILENAME = "listadetarefas.dat";

public static void escreveData(ArrayList<String> tarefas, Context context){
    try {
        FileOutputStream fos = context.openFileOutput(FILENAME, Context.MODE_PRIVATE);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(tarefas);
        oos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

public static  ArrayList<String> lerData(Context context){
    ArrayList<String> listaTarefas = null;
    try {
        FileInputStream fis = context.openFileInput(FILENAME);
        ObjectInputStream ois = new ObjectInputStream(fis);
        listaTarefas = (ArrayList<String>) ois.readObject();
    } catch (FileNotFoundException e) {
        listaTarefas = new ArrayList<>();
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    return listaTarefas;
}

}

THE LOGCAT ERROR:

logcat error

Any help will be very much appreciated since you'll be like a teacher to me.

I FIGURED IT OUT!

I only needed one protection, not on the addActivity, but on the main when I call to add a Task

Upvotes: 0

Views: 65

Answers (2)

Hai Hack
Hai Hack

Reputation: 1018

Change to this:

 backBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.backBtn:
                Intent intent = new Intent(addActivity.this, MainActivity.class);
                if(tarefasEdit.getText()!=null){
                       intent.putExtra("tarefa_key", tarefasEdit.getText().toString());
                }else{
                       intent.putExtra("tarefa_key", "");
                }
               
                startActivity(intent);
                tarefasEdit.setText("");
                break;
            }
        }
    });

Upvotes: 0

Mateo Hervas
Mateo Hervas

Reputation: 585

From what I can see, you are launching your MainActivity first. In this scenario the getIntent().getStringExtra("tarefa_key"); is null and thats why it can't be casted to String. You could change it to this:

if(getIntent().getStringExtra("tarefa_key")!=null){

   String tarefa = getIntent().getStringExtra("tarefa_key");
   adapter.add(tarefa);

}

This way, only when your intent has the string extra "tarefa_key" it will add it to your adapter. Also, I am not sure what is the behaviour you are expecting but I think you could take a look into startActivityForResult so you can start your secondActivity and once you have the result you want yo can go back to your Main Activity. I think thats a cleaner way of doing it.

hope it helps :)

Upvotes: 1

Related Questions