-3

I want to know how to do it to make a listview in second activity

Person person = new Person(nome, idade, numero);

        this.list.add(person);

    int number= this.list.size();
        this.numero.setText(String.valueOf(number));

        this.item.setText("");
    }
        break;

    case R.id.btn_activity2:
        Intent intent = new Intent(MainActivity.this, ListActivity.class);
        intent.put(...here is the problem)
        startActivity(intent);
        break;

2 Answers2

2

One way that you can send an ArrayList to the next activity is by creating a bundle like this:

Bundle bundle = new Bundle();
bundle.putParcelableArrayList("Object", listOfObject);
intent.putExtras(bundle);
startActivity(intent);

listOfObject should be replaced with the name of your ArrayList. Then, to receive the intent in your ListActivity.class you can do:

List<Person> myList= this.getIntent().getExtras().getParcelableArrayList("Object");

myList now is a list of that object. If you receive any errors, just comment below.

Ishaan Javali
  • 1,711
  • 3
  • 13
  • 23
  • Theres an error when I put the arraylist name :( – user10751746 Dec 20 '18 at 23:08
  • What is the error? A possible reason may be with the Object's class. Try adding `implements Parceable` to the class. So it should look like: `public class MyObject implements Parceable`. – Ishaan Javali Dec 20 '18 at 23:20
  • yeah, I do it, but when I will start the second activity theres an exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mycompany.ListView/com.mycompany.ListView.ListActivity}: android.os.BadParcelableException: Parcelable protocol requires a Parcelable.Creator object called CREATOR on class com.mycompany.ListView.Perso – user10751746 Dec 20 '18 at 23:28
0

You can use intent.putStringArrayListExtra(your-key, ArrayList) then get it with

String[] arrayList = extras.getStringArrayListExtra(your-key);

Haroun Hajem
  • 5,223
  • 3
  • 26
  • 39