-1

I managed to add text to the ListView, but I want to pass the ListView data to a TextView which is in another activity by clicking on the button 'Button'. I don't know how to use Serializable or Parcelable to pass data through Listview. Layout Screenshot

public void onClick(View v) {

        switch (v.getId ()) {
            case R.id.btnAdddata:

                String result = editText.getText ().toString ();
               arrayList.add(result); //add text from edittext to list
              adapter.notifyDataSetChanged (); //update arraylist and notify adapter
             break;

            case R.id.btnNext:

                 Intent i = new Intent (MainActivity.this, NextActivity.class);
                 i.putExtra("key", (Serializable) listView_lv);
                startActivity (i);
                break;

        }
    }
Arso
  • 11
  • 1

1 Answers1

0

This will still need you to implement parcelable (if using a custom object). There are guides on how to do this if you do a quick google search!

Here's an example just using an ArrayList of Strings!

ArrayList<String> myData = new ArrayList<>();
        for(int i = 0; i < listView.getAdapter().getCount(); i++) {
            myData.add(listView.getAdapter().getItem(i));
        }
        Intent intent = new Intent(CurrentActivity.this, NewActivity.class);
        intent.putStringArrayListExtra("myData", myData);
        // Or use this for a list of custom objects, but the custom objects need to implement Parcelable - Google this to find out more!
        intent.putParcelableArrayListExtra("myData", myData);
        startActivity(intent);
TomH
  • 2,581
  • 1
  • 15
  • 30