Padelis Theodosiou Padelis Theodosiou - 5 months ago 16
Android Question

Store output text in array list and display it in a ListView Android

I'm trying to build my first app for android and i came up with a "problem".
I want to store the output text in an arrayList and the display it in a listView that this listView is on another page.

MainActivity class :

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TextView textView = (TextView) findViewById(R.id.textView);
final TextView textView2 = (TextView) findViewById(R.id.textView2);
final SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
Button button = (Button) findViewById(R.id.button);
final RadioButton radioButton = (RadioButton) findViewById(R.id.radioButton);
final RadioButton radioButton2 = (RadioButton) findViewById(R.id.radioButton2);
final RadioButton radioButton3 = (RadioButton) findViewById(R.id.radioButton3);
final RadioButton radioButton4 = (RadioButton) findViewById(R.id.radioButton4);
final TextView textView3 = (TextView) findViewById(R.id.textView3);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ArrayList<String> addArray = new ArrayList<String>();
TextView textView3 = (TextView)findViewById(R.id.textView3);
ListView listView = (ListView)findViewById(R.id.listView);

String getInput = textView3.getText().toString();

if(addArray.contains(getInput)){
Toast.makeText(getBaseContext(),"Password already exists!",Toast.LENGTH_LONG).show();
}
else if (getInput==null){
Toast.makeText(getBaseContext(), "Input field is empty!", Toast.LENGTH_SHORT).show();
}
else {
addArray.add(getInput);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,addArray);
listView.setAdapter(adapter);
((TextView)findViewById(R.id.textView3)).setText("");
}

Snackbar.make(view, "Password saved!", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});


At this point my code runs with no errors... But I want the listView to be in another activity named: saved_passwords. When I edit this

ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,addArray);
to this

ArrayAdapter<String> adapter = new ArrayAdapter<String>(saved_passwords, android.R.layout.simple_list_item_1,addArray);
my code wont run.

Thanks in advanced guys.

Answer

You have to pass the ArrayList when you start the SavedPassword activity, like below:

Intent intent = new Intent(MainActivity.this,
            SavedPasswordActivity.class);
data.putStringArrayList("SavedPassword",
            addArray);
intent.putExtras(data);
startActivity(intent);

And inside onCreate method of your SavedPasswordActivity you have to initialize the ListView something like below:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_saved_password);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    ArrayList<String> addArray getIntent().getExtras().getStringArrayList("SavedPassword");
    ListView listView = (ListView)findViewById(R.id.listView);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,addArray);
    listView.setAdapter(adapter);
}

And yeah don't forget to include a ListView inside you activity_saved_password.xml file.

Comments