(Android) How to pass ArrayList<String> via intent and how to get it in the next activation?

UPD

Put in Intent:

 public void get_masterList() { ... final String sferaOption; final ArrayList<String> midList = new ArrayList<>(); ... ... lvCatalog.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { TextView tvMid = (TextView) view.findViewById(R.id.tvMid); String mid = tvMid.getText().toString(); // Передаем id мастера в следующий Активити Intent intent = new Intent(catalog_activity.this, masterDetails2.class); intent.putExtra("mid", mid); intent.putExtra("sferaOption", sferaOption); intent.putExtra("midlist", midList); startActivity(intent); } }); } 

Checked through Debug - midList contains 3 items

We get in the following activation:

 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_master_details2); Toolbar toolbar = (Toolbar) findViewById(R.id.myToolBar); toolbar.setTitleTextColor(getResources().getColor(R.color.toolbar_title)); setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); pager = (ViewPager) findViewById(R.id.pager); pagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager()); pager.setAdapter(pagerAdapter); final String mid, sferaOption; ArrayList<String> midList; Bundle extras = getIntent().getExtras(); if (extras == null) { mid = null; sferaOption = null; midList = null; } else { mid = extras.getString("mid"); sferaOption = extras.getString("sferaOption"); } midList = (ArrayList<String>) getIntent().getSerializableExtra("midList"); Toast.makeText(this, midList.size(), Toast.LENGTH_SHORT).show(); 

Application is closed with an error, through Debug we see midList = null

  • 2
    putStringArrayListExtra() / getStringArrayListExtra() - Yura Ivanov
  • It was tried. Everything is empty .. - Newbie
  • You put the midlist , and look for the midList . The register matters. Use constants. - Yura Ivanov
  • Yes, sorry, I looked through - Beginner

1 answer 1

We put a leaf in intent in FirstActivity

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = new Intent(getApplicationContext(),SecondActvity.class); ArrayList<String> array = new ArrayList<>(); array.add("some text"); intent.putExtra("list", array); startActivity(intent); } 

We take out a sheet from intent in SecondActivity.java

  @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ArrayList<String> arrayFromIntent = (ArrayList<String>) getIntent().getSerializableExtra("list"); } 
  • getIntent (). getSerializableExtra ("list") - is it assigned to something? or how to continue to work with this? - Newcomer
  • Well, you have a task to transfer from one activity to another, i.e. you create an intent and put an ArrayList into it and with this intent you call the second activation, then in another activation you get the ArrayList from the incoming intent that you packed into the intent in the first activation - Kirill Stoianov
  • That's how it was with me // Pass the master id to the next Activity Intent intent = new Intent (catalog_activity.this, masterDetails2.class); intent.putExtra ("mid", mid); intent.putExtra ("sferaOption", sferaOption); intent.putExtra ("midlist", midList); startActivity (intent); - Newcomer
  • But at the reception, no matter how I accept midList = null - Beginner
  • 2
    @YuraIvanov answer does not get wrong from this - Kirill Stoianov